Skip to content

notebook_edit — mutate Jupyter notebook cells

notebook_edit is the structured-edit counterpart for Jupyter notebooks. It parses the .ipynb JSON, applies a single cell-level mutation, and writes it back. The notebook stays parseable — no string-level surgery that could corrupt the JSON envelope.

ArgTypeRequiredDescription
notebook_pathstringyesFilesystem path to the .ipynb file.
actionstringyesinsert, replace, or delete.
cell_indexintegerone of0-based cell index.
cell_idstringone ofnbformat v4 cell id.
contentstringfor insert / replaceCell source.
cell_typestringfor insertcode or markdown. Defaults to code.

Exactly one of cell_index / cell_id is required for replace and delete. For insert, cell_index is the position to insert at.

{
"notebook_path": "notebooks/analysis.ipynb",
"action": "insert",
"cell_index": 2,
"cell_type": "markdown",
"content": "## Section 2 — exploratory plots"
}
from chimera.tools.notebook_edit import NotebookEditTool
tool = NotebookEditTool()
result = tool.execute(
{"notebook_path": "demo.ipynb",
"action": "replace",
"cell_index": 0,
"content": "import pandas as pd\nimport numpy as np\n"},
env=local_env,
)
Replaced cell 0 in demo.ipynb (12 → 39 chars).
  • Only nbformat v4 notebooks are supported.
  • Outputs and execution counts on touched cells are cleared so re-execution starts from a clean slate.