nb - Notebook CLI
A fast, programmatic command-line interface for working with Jupyter notebooks. Designed for AI agents, automation scripts, and developers who need reliable notebook manipulation without opening a browser.
Installation
The binary will be available at target/release/nb.
Quick Start
# Create and build a notebook
# Execute and view results
Local Mode
Default behavior. Operations directly modify .ipynb files.
Local mode lets you create, edit, execute, and query notebooks on disk without any server running. All changes are written directly to the .ipynb file.
# Create and edit
# Read and search
# Execute locally (requires Python dependencies)
Python Dependencies for Local Execution
To execute notebooks in local mode, install:
Or manually:
Note: These dependencies are only required for local execution. Remote mode doesn't need them.
Remote Mode
Connect to a running JupyterLab server for real-time synchronization.
When you connect to a Jupyter server, the CLI uses Y.js for conflict-free real-time updates. Changes appear instantly in your open JupyterLab tabs, and you can execute code using the server's kernel.
Connect to a Server
Auto-detection (recommended):
Automatically finds running Jupyter servers using jupyter server list, validates them, and connects. If multiple servers are found, you'll get an interactive prompt to choose one.
Manual connection:
Connection options:
--server: Server URL (e.g.,http://localhost:8888)--token: Authentication token from Jupyter
Connection Persistence
Connection info is saved in .jupyter/cli.json in the current directory. All subsequent commands automatically use this connection until you disconnect or change directories.
# Connect once (auto-detect)
# Future commands use saved connection
# Check current connection
# Disconnect when done
How it works: When connected, the CLI detects if a notebook is open in JupyterLab and uses Y.js for instant sync. If the notebook isn't open on the server, operations fall back to file-based mode.
Remote Mode Examples
# Connect automatically
# Add cell - appears instantly in JupyterLab
# Update cell in real-time
# Execute via remote kernel
# Disconnect when switching projects
Commands
| Command | Purpose |
|---|---|
nb notebook create <path> |
Create a new notebook |
nb notebook read <path> |
Read notebook structure and cells |
nb notebook execute <path> |
Execute all cells in notebook |
nb notebook search <path> <pattern> |
Search for patterns in notebook |
nb cell add <path> --source <code> |
Add a new cell |
nb cell update <path> --cell <index> |
Update an existing cell |
nb cell delete <path> --cell <index> |
Delete a cell |
nb cell execute <path> --cell <index> |
Execute a specific cell |
nb output clear <path> |
Clear cell outputs |
nb connect [--server URL --token TOKEN] |
Connect to Jupyter server (auto-detects if no args) |
nb status |
Show current connection status |
nb disconnect |
Disconnect from server |
Use --help with any command for full details and options.
Key Features
Cell Referencing
Two ways to reference cells:
- Index:
--cell 0(position-based, supports negative indexing:-1= last cell) - ID:
--cell-id "my-cell"(stable, doesn't change when cells move)
Output Format
Control output format for better integration with your workflow:
- JSON (default): Structured, nbformat-compliant for programmatic use
- Text (
-f text): Human-readable for terminal viewing
Multi-line Code
Escape sequences are automatically interpreted:
# Add cell with proper formatting
# Append to existing cell
Common Workflows
Build notebook programmatically:
Debug and fix cells:
# Find problematic cells
# Inspect specific cell with outputs
# Fix the cell
# Re-execute
Extract specific content:
For AI agents:
# Analyze all code in a notebook
# Find cells with errors
# Add analysis cell and execute
Examples
See examples/ directory for sample notebooks demonstrating various cell types and outputs.