NotebookManager Reference
TheNotebookManager
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
Attribute | Type | Description |
---|---|---|
notebook | Dict[str, JupyterClientExecutor] | Dictionary storing notebook instances by notebook ID |
active_nbid | Optional[str] | Currently active notebook ID |
Methods
create_notebook(nbid, path, kernel="python")
Creates a new Jupyter notebook instance.
Parameters
Parameter | Type | Default | Description |
---|---|---|---|
nbid | str | Required | Unique notebook ID |
path | str | Required | Path to save the notebook file |
kernel | str | "python" | Jupyter kernel to use (e.g., “python”, “ir”, “r”) |
Behavior
- Creates a new
JupyterClientExecutor
instance with the specified kernel - Associates the notebook with the provided ID
- Sets the new notebook as the active notebook
- Automatically saves the notebook if a path is provided
Example
delete_notebook(nbid)
Deletes a notebook instance and shuts down its kernel.
Parameters
Parameter | Type | Description |
---|---|---|
nbid | str | Notebook ID to delete |
Behavior
- Shuts down the notebook’s Jupyter kernel
- Removes the notebook from the manager
- Cleans up associated resources
Example
switch_notebook(nbid)
Switches the active notebook to the specified notebook ID.
Parameters
Parameter | Type | Description |
---|---|---|
nbid | str | Notebook ID to switch to |
Behavior
- Changes the
active_nbid
to the specified notebook ID - 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
- Raises
ValueError
if no notebooks have been created - Raises
ValueError
if the active notebook ID doesn’t exist - Returns the
JupyterClientExecutor
instance for the active notebook
Example
JupyterClientExecutor
TheNotebookManager
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