1
1
from typing import Literal , Optional
2
2
from jupyter_server .serverapp import ServerApp
3
3
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
+
4
8
5
9
6
10
def emit (data ):
@@ -11,11 +15,6 @@ def emit(data):
11
15
schema_id = "https://events.jupyter.org/jupyterlab_command_toolkit/lab_command/v1" ,
12
16
data = data
13
17
)
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!" )
19
18
20
19
21
20
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
127
126
})
128
127
129
128
130
- def clear_all_outputs_in_notebook () -> None :
129
+ def clear_all_outputs_in_notebook (run : bool ) -> None :
131
130
"""
132
131
Clear all outputs in the active notebook.
133
132
@@ -136,6 +135,9 @@ def clear_all_outputs_in_notebook() -> None:
136
135
cleaning up notebook outputs before sharing or when outputs are no longer
137
136
needed.
138
137
138
+ Args:
139
+ run (bool): Run this command.
140
+
139
141
Returns:
140
142
None: This function doesn't return a value. It emits an event to JupyterLab
141
143
to trigger the clear all outputs action.
@@ -156,10 +158,45 @@ def clear_all_outputs_in_notebook() -> None:
156
158
})
157
159
158
160
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
+
159
195
TOOLS = {
160
196
Tool (callable = open_document , read = True ),
161
197
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 ),
163
200
}
164
201
165
202
0 commit comments