Skip to content

Commit 50942e0

Browse files
committed
Add a few more tools
1 parent fa79045 commit 50942e0

File tree

2 files changed

+45
-19
lines changed

2 files changed

+45
-19
lines changed

jupyterlab_commands_toolkit/__init__.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,4 @@ def _jupyter_server_extension_points():
2828
def _load_jupyter_server_extension(serverapp: ServerApp):
2929
schema_path = pathlib.Path(__file__).parent / "events" / "jupyterlab-command.yml"
3030
serverapp.event_logger.register_event_schema(schema_path)
31-
serverapp.log.info("jupyterlab_commands_toolkit extension loaded.")
32-
33-
34-
async def _start_jupyter_server_extension(serverapp: ServerApp):
35-
registry = serverapp.web_app.settings["toolkit_registry"]
36-
if registry:
37-
tools = ToolSet(TOOLS)
38-
registry.register_toolkit(
39-
Toolkit(
40-
name="jupyterlab_commands", tools=tools
41-
)
42-
)
31+
serverapp.log.info("jupyterlab_commands_toolkit extension loaded.")

jupyterlab_commands_toolkit/tools.py

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from typing import Literal, Optional
22
from jupyter_server.serverapp import ServerApp
33
from jupyter_server_ai_tools.models import Tool
4+
from jupyter_server.base.call_context import CallContext
5+
6+
handler = CallContext.get(CallContext.JUPYTER_HANDLER)
7+
48

59

610
def emit(data):
@@ -11,11 +15,6 @@ def emit(data):
1115
schema_id="https://events.jupyter.org/jupyterlab_command_toolkit/lab_command/v1",
1216
data=data
1317
)
14-
# server.event_logger.emit(
15-
# schema_id="https://events.jupyter.org/jupyterlab_command_toolkit/lab_command/v1",
16-
# data=data
17-
# )
18-
print("SEEN!")
1918

2019

2120
INSERT_MODE = Literal['split-top', 'split-left', 'split-right', 'split-bottom', 'merge-top', 'merge-left', 'merge-right', 'merge-bottom', 'tab-before', 'tab-after']
@@ -127,7 +126,7 @@ def open_markdown_file_in_preview_mode(relative_path: str, mode: Optional[INSERT
127126
})
128127

129128

130-
def clear_all_outputs_in_notebook() -> None:
129+
def clear_all_outputs_in_notebook(run: bool) -> None:
131130
"""
132131
Clear all outputs in the active notebook.
133132
@@ -136,6 +135,9 @@ def clear_all_outputs_in_notebook() -> None:
136135
cleaning up notebook outputs before sharing or when outputs are no longer
137136
needed.
138137
138+
Args:
139+
run (bool): Run this command.
140+
139141
Returns:
140142
None: This function doesn't return a value. It emits an event to JupyterLab
141143
to trigger the clear all outputs action.
@@ -156,10 +158,45 @@ def clear_all_outputs_in_notebook() -> None:
156158
})
157159

158160

161+
def show_diff_of_current_notebook(run: bool) -> None:
162+
"""
163+
Show git diff of the current notebook in JupyterLab.
164+
165+
This function displays the git differences for the currently active notebook
166+
by emitting an 'nbdime:diff-git' command. It uses nbdime (Jupyter notebook
167+
diff tool) to show a visual comparison between the current notebook state
168+
and the last committed version in git.
169+
170+
Args:
171+
run (bool): Run this command.
172+
173+
Returns:
174+
None: This function doesn't return a value. It emits an event to JupyterLab
175+
to trigger the notebook diff display.
176+
177+
Examples:
178+
>>> show_diff_of_current_notebook(True) # Show git diff for current notebook
179+
180+
Note:
181+
- This function only works when a notebook is currently active/focused
182+
- Requires the nbdime extension to be installed and enabled in JupyterLab
183+
- The notebook must be in a git repository for diffs to be meaningful
184+
- Shows differences between current state and last git commit
185+
- Displays both content and output differences in a visual format
186+
- Useful for reviewing changes before committing notebook modifications
187+
"""
188+
emit({
189+
"name": "nbdime:diff-git",
190+
"args": {}
191+
})
192+
193+
194+
159195
TOOLS = {
160196
Tool(callable=open_document, read=True),
161197
Tool(callable=open_markdown_file_in_preview_mode, read=True),
162-
# Tool(callable=clear_all_outputs_in_notebook, read=True)
198+
Tool(callable=clear_all_outputs_in_notebook, read=True),
199+
Tool(callable=show_diff_of_current_notebook, read=True),
163200
}
164201

165202

0 commit comments

Comments
 (0)