Skip to content

Commit 86bdbf2

Browse files
committed
Update to new toolkit API
1 parent 50942e0 commit 86bdbf2

File tree

5 files changed

+113
-21
lines changed

5 files changed

+113
-21
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@ This extension is composed of a Python package named `jupyterlab_commands_toolki
88
for the server extension and a NPM package named `jupyterlab-commands-toolkit`
99
for the frontend extension.
1010

11+
## Features
12+
13+
- **Document Management**: Open documents in JupyterLab with various layout modes
14+
- **Markdown Preview**: Open markdown files in rendered preview mode
15+
- **Notebook Operations**: Clear outputs and show diffs for notebooks
16+
- **Jupyter AI Integration**: Tools available for use with Jupyter AI
17+
1118
## Requirements
1219

1320
- JupyterLab >= 4.0.0
21+
- jupyter-ai (for AI toolkit functionality)
1422

1523
## Install
1624

@@ -20,6 +28,49 @@ To install the extension, execute:
2028
pip install jupyterlab_commands_toolkit
2129
```
2230

31+
## Usage
32+
33+
### With Jupyter AI
34+
35+
The extension provides a toolkit for Jupyter AI with the following tools:
36+
37+
1. **open_document_tool**: Open documents in JupyterLab with various layout modes
38+
2. **open_markdown_preview_tool**: Open markdown files in rendered preview mode
39+
3. **clear_notebook_outputs_tool**: Clear all outputs in the active notebook
40+
4. **show_notebook_diff_tool**: Show git diff for the current notebook using nbdime
41+
42+
```python
43+
# Access the AI toolkit
44+
from jupyterlab_commands_toolkit import ai_toolkit
45+
46+
# The toolkit is automatically available to Jupyter AI when installed
47+
```
48+
49+
### Direct Usage
50+
51+
You can also use the commands directly:
52+
53+
```python
54+
from jupyterlab_commands_toolkit.tools import (
55+
open_document,
56+
open_markdown_file_in_preview_mode,
57+
clear_all_outputs_in_notebook,
58+
show_diff_of_current_notebook
59+
)
60+
61+
# Open a document
62+
open_document("notebook.ipynb", mode="split-right")
63+
64+
# Open markdown in preview
65+
open_markdown_file_in_preview_mode("README.md")
66+
67+
# Clear notebook outputs
68+
clear_all_outputs_in_notebook(True)
69+
70+
# Show notebook diff
71+
show_diff_of_current_notebook(True)
72+
```
73+
2374
## Uninstall
2475

2576
To remove the extension, execute:

jupyterlab_commands_toolkit/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@
99
__version__ = "dev"
1010

1111
import pathlib
12-
from jupyter_server_ai_tools.models import ToolSet, Toolkit
12+
1313
from jupyter_server.serverapp import ServerApp
14-
from .tools import TOOLS
14+
from .toolkit import toolkit
15+
16+
# Export the AI toolkit for jupyter-ai integration
17+
try:
18+
from .toolkit import toolkit as ai_toolkit
19+
except ImportError:
20+
# If jupyter-ai is not available, the AI toolkit won't be available
21+
toolkit = None
1522

1623
def _jupyter_labextension_paths():
1724
return [{
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""JupyterLab Commands toolkit for Jupyter AI"""
2+
from jupyter_ai.tools.models import Tool, Toolkit
3+
4+
from typing import Optional
5+
from .tools import (
6+
open_document,
7+
open_markdown_file_in_preview_mode,
8+
clear_all_outputs_in_notebook,
9+
show_diff_of_current_notebook,
10+
INSERT_MODE
11+
)
12+
13+
# Create the toolkit
14+
toolkit = Toolkit(
15+
name="jupyterlab_commands_toolkit",
16+
description="""A comprehensive toolkit for controlling JupyterLab interface and performing notebook operations through AI commands.
17+
18+
This toolkit provides programmatic access to JupyterLab's core functionality, enabling AI assistants to:
19+
20+
**Document Management:**
21+
- Open files, notebooks, and documents with precise control over layout positioning
22+
- Support for split-pane layouts (top, left, right, bottom) and tab management
23+
- Open markdown files in rendered preview mode for better readability
24+
25+
**Notebook Operations:**
26+
- Clear all cell outputs in the active notebook for cleanup and sharing
27+
- Display git diffs for notebooks using nbdime visualization
28+
- Maintain notebook structure while performing operations
29+
30+
**Layout Control:**
31+
- Split current workspace into multiple panes
32+
- Merge content with adjacent areas
33+
- Create new tabs before or after current position
34+
- Flexible positioning options for optimal workspace organization
35+
36+
**Key Features:**
37+
- Event-driven architecture using JupyterLab's command system
38+
- Seamless integration with Jupyter AI for natural language control
39+
- Support for relative file paths from server root directory
40+
- Comprehensive error handling and user feedback
41+
- Compatible with JupyterLab 4.0+ and modern Jupyter environments
42+
43+
Use these tools to programmatically manage your JupyterLab workspace, organize documents, and perform common notebook operations through conversational AI interfaces."""
44+
)
45+
46+
# Add tools to the toolkit
47+
toolkit.add_tool(Tool(callable=open_document, read=True))
48+
toolkit.add_tool(Tool(callable=open_markdown_file_in_preview_mode, read=True))
49+
toolkit.add_tool(Tool(callable=clear_all_outputs_in_notebook, read=True))
50+
toolkit.add_tool(Tool(callable=show_diff_of_current_notebook, read=True))

jupyterlab_commands_toolkit/tools.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
from typing import Literal, Optional
22
from jupyter_server.serverapp import ServerApp
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-
83

94

105
def emit(data):
@@ -20,7 +15,6 @@ def emit(data):
2015
INSERT_MODE = Literal['split-top', 'split-left', 'split-right', 'split-bottom', 'merge-top', 'merge-left', 'merge-right', 'merge-bottom', 'tab-before', 'tab-after']
2116

2217

23-
2418
def open_document(relative_path: str, mode: Optional[INSERT_MODE] = None) -> None:
2519
"""
2620
Open a document in JupyterLab.
@@ -188,15 +182,4 @@ def show_diff_of_current_notebook(run: bool) -> None:
188182
emit({
189183
"name": "nbdime:diff-git",
190184
"args": {}
191-
})
192-
193-
194-
195-
TOOLS = {
196-
Tool(callable=open_document, read=True),
197-
Tool(callable=open_markdown_file_in_preview_mode, read=True),
198-
Tool(callable=clear_all_outputs_in_notebook, read=True),
199-
Tool(callable=show_diff_of_current_notebook, read=True),
200-
}
201-
202-
185+
})

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ classifiers = [
2424
]
2525
dependencies = [
2626
"jupyter_server>=2.4.0,<3",
27-
"jupyterlab-eventlistener"
27+
"jupyterlab-eventlistener",
28+
"jupyter-ai"
2829
]
2930
dynamic = ["version", "description", "authors", "urls", "keywords"]
3031

0 commit comments

Comments
 (0)