11from typing import Literal , Optional
22from jupyter_server .serverapp import ServerApp
33from 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
610def 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
2120INSERT_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+
159195TOOLS = {
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