Skip to main content

NotebookManager Reference

The NotebookManager is the core backend manager for handling Jupyter notebooks in the scmcphub ecosystem. It provides a centralized way to manage multiple Jupyter notebook instances with different kernels, making it essential for code-mode execution and interactive analysis workflows. Note: This implementation depends on abcoder for Jupyter kernel management and code execution.

Overview

NotebookManager serves as the primary notebook management system for:
  • Multiple Notebook Support: Managing multiple Jupyter notebook instances simultaneously
  • Kernel Management: Handling different Jupyter kernels (Python, R, etc.)
  • Active Notebook Tracking: Maintaining the currently active notebook for operations
  • Code Execution: Providing a unified interface for code execution across notebooks
  • Notebook Lifecycle: Managing creation, deletion, and switching between notebooks

Class Definition

Constructor

__init__()

Initializes a new NotebookManager instance.

Parameters

No parameters required.

Example

Instance Attributes

Methods

create_notebook(nbid, path, kernel="python")

Creates a new Jupyter notebook instance.

Parameters

Behavior

  1. Creates a new JupyterClientExecutor instance with the specified kernel
  2. Associates the notebook with the provided ID
  3. Sets the new notebook as the active notebook
  4. Automatically saves the notebook if a path is provided

Example

delete_notebook(nbid)

Deletes a notebook instance and shuts down its kernel.

Parameters

Behavior

  1. Shuts down the notebook’s Jupyter kernel
  2. Removes the notebook from the manager
  3. Cleans up associated resources

Example

switch_notebook(nbid)

Switches the active notebook to the specified notebook ID.

Parameters

Behavior

  1. Changes the active_nbid to the specified notebook ID
  2. No validation is performed - ensure the notebook exists before switching

Example

active_notebook (Property)

Gets the currently active notebook instance.

Returns

  • Type: JupyterClientExecutor
  • Description: The currently active notebook instance

Behavior

  1. Raises ValueError if no notebooks have been created
  2. Raises ValueError if the active notebook ID doesn’t exist
  3. Returns the JupyterClientExecutor instance for the active notebook

Example

JupyterClientExecutor

The NotebookManager uses JupyterClientExecutor instances to handle individual notebooks. Each executor provides:

Key Features

  • Kernel Management: Handles Jupyter kernel lifecycle
  • Code Execution: Executes code and returns results
  • Output Handling: Manages stdout, stderr, and display data
  • Error Handling: Captures and formats execution errors
  • Notebook Persistence: Saves notebook state to files

Main Methods

Usage Examples

Basic Usage

Multi-Notebook Workflow

Integration with MCP Tools

Multi-Kernel Support

Error Handling

Advanced Code Execution Patterns

Single-Cell Analysis Workflow

Best Practices

1. Notebook ID Management

Use descriptive and unique notebook IDs:

2. Kernel Selection

Choose appropriate kernels for your analysis:

Integration Patterns

With BaseMCPManager

With MCP Tools