Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ This extension is composed of a Python package named `jupyterlab_commands_toolki
for the server extension and a NPM package named `jupyterlab-commands-toolkit`
for the frontend extension.

## Features

- **Document Management**: Open documents in JupyterLab with various layout modes
- **Markdown Preview**: Open markdown files in rendered preview mode
- **Notebook Operations**: Clear outputs and show diffs for notebooks
- **Jupyter AI Integration**: Tools available for use with Jupyter AI

## Requirements

- JupyterLab >= 4.0.0
- jupyter-ai (for AI toolkit functionality)

## Install

Expand All @@ -20,6 +28,49 @@ To install the extension, execute:
pip install jupyterlab_commands_toolkit
```

## Usage

### With Jupyter AI

The extension provides a toolkit for Jupyter AI with the following tools:

1. **open_document_tool**: Open documents in JupyterLab with various layout modes
2. **open_markdown_preview_tool**: Open markdown files in rendered preview mode
3. **clear_notebook_outputs_tool**: Clear all outputs in the active notebook
4. **show_notebook_diff_tool**: Show git diff for the current notebook using nbdime

```python
# Access the AI toolkit
from jupyterlab_commands_toolkit import ai_toolkit

# The toolkit is automatically available to Jupyter AI when installed
```

### Direct Usage

You can also use the commands directly:

```python
from jupyterlab_commands_toolkit.tools import (
open_document,
open_markdown_file_in_preview_mode,
clear_all_outputs_in_notebook,
show_diff_of_current_notebook
)

# Open a document
open_document("notebook.ipynb", mode="split-right")

# Open markdown in preview
open_markdown_file_in_preview_mode("README.md")

# Clear notebook outputs
clear_all_outputs_in_notebook(True)

# Show notebook diff
show_diff_of_current_notebook(True)
```

## Uninstall

To remove the extension, execute:
Expand Down
11 changes: 9 additions & 2 deletions jupyterlab_commands_toolkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@
__version__ = "dev"

import pathlib
from jupyter_server_ai_tools.models import ToolSet, Toolkit

from jupyter_server.serverapp import ServerApp
from .tools import TOOLS
from .toolkit import toolkit

# Export the AI toolkit for jupyter-ai integration
try:
from .toolkit import toolkit as ai_toolkit
except ImportError:
# If jupyter-ai is not available, the AI toolkit won't be available
toolkit = None

def _jupyter_labextension_paths():
return [{
Expand Down
50 changes: 50 additions & 0 deletions jupyterlab_commands_toolkit/toolkit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""JupyterLab Commands toolkit for Jupyter AI"""
from jupyter_ai.tools.models import Tool, Toolkit

from typing import Optional
from .tools import (
open_document,
open_markdown_file_in_preview_mode,
clear_all_outputs_in_notebook,
show_diff_of_current_notebook,
INSERT_MODE
)

# Create the toolkit
toolkit = Toolkit(
name="jupyterlab_commands_toolkit",
description="""A comprehensive toolkit for controlling JupyterLab interface and performing notebook operations through AI commands.

This toolkit provides programmatic access to JupyterLab's core functionality, enabling AI assistants to:

**Document Management:**
- Open files, notebooks, and documents with precise control over layout positioning
- Support for split-pane layouts (top, left, right, bottom) and tab management
- Open markdown files in rendered preview mode for better readability

**Notebook Operations:**
- Clear all cell outputs in the active notebook for cleanup and sharing
- Display git diffs for notebooks using nbdime visualization
- Maintain notebook structure while performing operations

**Layout Control:**
- Split current workspace into multiple panes
- Merge content with adjacent areas
- Create new tabs before or after current position
- Flexible positioning options for optimal workspace organization

**Key Features:**
- Event-driven architecture using JupyterLab's command system
- Seamless integration with Jupyter AI for natural language control
- Support for relative file paths from server root directory
- Comprehensive error handling and user feedback
- Compatible with JupyterLab 4.0+ and modern Jupyter environments

Use these tools to programmatically manage your JupyterLab workspace, organize documents, and perform common notebook operations through conversational AI interfaces."""
)

# Add tools to the toolkit
toolkit.add_tool(Tool(callable=open_document, read=True))
toolkit.add_tool(Tool(callable=open_markdown_file_in_preview_mode, read=True))
toolkit.add_tool(Tool(callable=clear_all_outputs_in_notebook, read=True))
toolkit.add_tool(Tool(callable=show_diff_of_current_notebook, read=True))
19 changes: 1 addition & 18 deletions jupyterlab_commands_toolkit/tools.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
from typing import Literal, Optional
from jupyter_server.serverapp import ServerApp
from jupyter_server_ai_tools.models import Tool
from jupyter_server.base.call_context import CallContext

handler = CallContext.get(CallContext.JUPYTER_HANDLER)



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



def open_document(relative_path: str, mode: Optional[INSERT_MODE] = None) -> None:
"""
Open a document in JupyterLab.
Expand Down Expand Up @@ -188,15 +182,4 @@ def show_diff_of_current_notebook(run: bool) -> None:
emit({
"name": "nbdime:diff-git",
"args": {}
})



TOOLS = {
Tool(callable=open_document, read=True),
Tool(callable=open_markdown_file_in_preview_mode, read=True),
Tool(callable=clear_all_outputs_in_notebook, read=True),
Tool(callable=show_diff_of_current_notebook, read=True),
}


})
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ classifiers = [
]
dependencies = [
"jupyter_server>=2.4.0,<3",
"jupyterlab-eventlistener"
"jupyterlab-eventlistener",
"jupyter-ai"
]
dynamic = ["version", "description", "authors", "urls", "keywords"]

Expand Down
Loading