Skip to main content

Notebook Edit

Completely replaces the contents of a specific cell in a Jupyter notebook with support for insert and delete operations.

Common Properties

  • Name - The custom name of the node.
  • Color - The custom color of the node.
  • Delay Before (sec) - Waits in seconds before executing the node.
  • Delay After (sec) - Waits in seconds after executing node.
  • Continue On Error - Automation will continue regardless of any error. The default value is false.

Inputs

  • Notebook Path - string - Absolute path to the Jupyter notebook file (.ipynb) to edit (required).
  • New Source - string - The new source code or markdown content for the cell (required).
  • Cell ID - string - ID of the cell to edit; for insert mode, new cell will be inserted after this ID (optional).
  • Cell Type - string - Type of the cell: "code" or "markdown" (optional, defaults to current type for replace, required for insert).
  • Edit Mode - string - Edit operation: replace (default), insert, or delete (optional).

Options

None (all configuration through inputs).

Outputs

  • Notebook Path - string - Path to the edited notebook.
  • Cell ID - string - ID of the affected cell (newly created ID for insert operations).
  • Edit Info - object - Information about the edit operation:
    • operation - Type of operation performed: create, replace, insert, delete
    • position - Cell position in the notebook
    • cellType - Type of cell affected
    • afterCell - Reference cell ID (for insert operations)
    • error - Error message if operation failed

How It Works

The Notebook Edit node modifies Jupyter notebook cells. When executed, the node:

  1. Validates the notebook path is absolute and ends with .ipynb
  2. Reads and parses the existing notebook
  3. Performs the requested operation:

Replace Mode (default)

  • If Cell ID provided: Finds and replaces that cell's content
  • If no Cell ID: Creates new cell at the end
  • Updates cell type if specified
  • Clears outputs if changing to markdown

Insert Mode

  • Creates new cell with generated ID
  • If Cell ID provided: Inserts after that cell
  • If no Cell ID: Inserts at beginning
  • Sets cell type (defaults to "code")

Delete Mode

  • Requires Cell ID
  • Removes the specified cell
  • Decrements position of all following cells
  1. Writes the modified notebook back to disk with proper formatting
  2. Returns the affected cell ID and operation details

Requirements

  • Valid absolute path to .ipynb file
  • File must be a valid Jupyter notebook
  • Write permissions on the file
  • Valid cell ID (required for delete mode)
  • Non-empty new source (except for delete mode)

Error Handling

The node will return specific errors in the following cases:

  • Missing notebook path - "Notebook path is required"
  • Missing new source - "New source is required"
  • Relative path - "Notebook path must be absolute"
  • Wrong file extension - "File must be a Jupyter notebook (.ipynb)"
  • File not found - "Notebook not found: {{path}}"
  • Read error - "Failed to read notebook: {{error}}"
  • Invalid format - "Invalid notebook format: {{error}}"
  • Write error - "Failed to write notebook: {{error}}"
  • Serialize error - "Failed to serialize notebook: {{error}}"

Edit Info may contain errors for specific operations:

  • Cell not found - "Cell with ID '{{id}}' not found"
  • Delete without ID - "Cell ID is required for delete operation"

Usage Examples

Replace Cell Content

Notebook Path: /home/user/analysis.ipynb
Cell ID: abc123
New Source: import pandas as pd
df = pd.read_csv('new_data.csv')
df.head()
Edit Mode: replace

Insert New Code Cell

Notebook Path: /home/user/notebook.ipynb
Cell ID: xyz789
New Source: # New analysis section
print("Starting analysis...")
Cell Type: code
Edit Mode: insert

Insert Markdown Cell

Notebook Path: /home/user/docs.ipynb
New Source: # Chapter 1: Introduction

This notebook demonstrates...
Cell Type: markdown
Edit Mode: insert

Delete Cell

Notebook Path: /home/user/notebook.ipynb
Cell ID: old123
Edit Mode: delete
New Source: (ignored for delete)

Create Cell at End

Notebook Path: /home/user/notebook.ipynb
New Source: print("Final output")
Cell Type: code
Edit Mode: replace
(No Cell ID = append to end)

Change Cell Type

Notebook Path: /home/user/notebook.ipynb
Cell ID: abc123
New Source: # This is now markdown
Cell Type: markdown
Edit Mode: replace

Edit Modes

Replace Mode

  • With Cell ID: Replaces existing cell's content and optionally type
  • Without Cell ID: Creates new cell at the end
  • Use case: Update existing cells or add new cells to the end

Insert Mode

  • With Cell ID: Inserts new cell after the specified cell
  • Without Cell ID: Inserts new cell at the beginning
  • Use case: Add cells at specific positions in the notebook

Delete Mode

  • Requires Cell ID: Removes the specified cell
  • Use case: Clean up unwanted cells

Source Formatting

The node automatically formats source for notebook storage:

  • Splits content into lines
  • Each line (except last) ends with \n
  • Last line has no trailing newline if empty
  • Returns array format (standard for notebooks)

Cell Type Changes

When changing cell type from code to markdown:

  • Outputs are cleared
  • Execution count is removed

When changing from markdown to code:

  • Empty outputs array is created
  • Execution count is set to 0

Usage Notes

  • Cell IDs are auto-generated for new cells (8-character UUID)
  • Notebooks are written with pretty-printing (indented JSON)
  • Original notebook format version is preserved
  • Metadata is preserved for both notebook and cells
  • Empty source strings are handled correctly
  • Multi-line source is properly formatted
  • The operation is atomic - either succeeds completely or fails

Common Use Cases

Automated Code Updates

Update cells with new code versions or fixes.

Template Customization

Modify template notebooks for specific use cases.

Documentation Generation

Add or update markdown cells with generated documentation.

Test Notebook Creation

Programmatically create test notebooks with specific cells.

Notebook Refactoring

Reorganize cells by inserting and deleting.

Output Cleanup

Replace code cells to clear their outputs (change type or recreate).

Best Practices

  • Read before editing to understand current structure
  • Use specific Cell IDs for targeted edits
  • Specify Cell Type explicitly when creating cells
  • Backup notebooks before bulk edits
  • Validate source before writing to notebook
  • Check Edit Info to verify operation succeeded
  • Use replace for updates and insert for additions
  • Preserve cell order when making multiple edits
  • Test on copies before editing production notebooks

Example Workflows

Update All Import Cells

1. Notebook Read: get all cells
2. Filter: cells with "import" in source
3. For each import cell:
- Notebook Edit: update to new imports
- Use replace mode with cell ID

Add Header Cell

1. Notebook Edit:
- Mode: insert
- Cell Type: markdown
- New Source: # Notebook Title...
- (No Cell ID = insert at beginning)

Remove All Empty Cells

1. Notebook Read: get all cells
2. Filter: cells with empty or whitespace source
3. For each empty cell:
- Notebook Edit:
- Mode: delete
- Cell ID: `{{cell.id}}`

Convert Code to Markdown

1. Notebook Read: get specific cell
2. Notebook Edit:
- Cell ID: `{{cell.id}}`
- New Source: # `{{original source as comment}}`
- Cell Type: markdown
- Mode: replace

Comparison with Other Nodes

  • Notebook Edit vs Notebook Read: Notebook Edit modifies cells; Notebook Read extracts content
  • Notebook Edit vs Edit: Notebook Edit works with notebook structure; Edit does text replacement
  • Notebook Edit vs Write: Notebook Edit modifies cells; Write replaces entire files