Notebook: Cell
Run real Python inside a ComfyUI graph (shared kernel, plots, stdout)
- input
- input_2
- Result
- Plot
- Stdout
Ever sat in ComfyUI and thought "if I could just write a loop, or print the shape of this tensor, or plot the loss curve"? That's what NotebookCell is for. It's a Jupyter-style cell that executes Python code inside your workflow, with the usual notebook conveniences bolted on: syntax highlighting, shared variables between cells, preloaded numpy/torch/PIL/matplotlib, and live stdout streaming. It turns the graph into a scratchpad, which sounds like a gimmick until you're poking at a model's weights or debugging a sampler that's doing something you don't understand.
One warning before we go anywhere. The README says it plainly and it means it: this node is for power users and programmers only. It executes arbitrary Python with the same privileges as your ComfyUI process. ComfyUI is not a sandbox - installing a custom node or running a workflow that executes code is functionally like running an executable, and the ecosystem has paid for that lesson before. Treat downloaded workflows that contain NotebookCell cells like downloaded Python source files: read them before you run them.
How it works
Under the hood there's a real "kernel" per workflow. The pack keeps a _NOTEBOOK_KERNELS dict keyed by workflow ID, and each entry is a Python module object. Your cell's code runs against that module's namespace, so variables you define in one cell are there in the next - exactly like Jupyter's "In [1] / In [2]" flow, just inside a graph. np, torch, Image, plt (and nn, F for torch users) are preloaded for you. Matplotlib is set to the non-interactive Agg backend and plt.show() is neutered so figures get auto-captured at the end of the cell instead of blocking.
Two implementation details are worth knowing. First, the pack is written against ComfyUI's new V3 node API (from comfy_api.latest import io, define_schema, comfy_entrypoint) - so it needs a recent ComfyUI that ships that API; on an old install it just won't load. Second, each cell's code is written to a temp .py file under the pack's temp_notebook_cells/ folder so you can drop breakpoints and debug the cell in VSCode/Cursor. Execution runs in a thread with interrupt checking wired into range, zip, and friends, so a runaway loop is actually cancelable rather than a frozen server.
The inputs and outputs that matter
Only one input matters day to day, and it's required:
code(multiline string) - the Python you want to run. This is the whole node.
Optional input and input_2 accept anything and show up inside your code as the input and input_2 variables - that's how you feed the cell tensors or data from other nodes. That's also the natural home for the pack's Force Rerun node, which is a pair article in this same pack.
The three outputs:
Result(*) - whatever you assign to aResultvariable in your code. Assign nothing and it'sNone. Wire it into any node that takes a generic input.Plot(IMAGE) - the auto-captured matplotlib figures, converted to a real image tensor so you can preview or save them. Gotcha: all figures in one cell run must have the samefigsize, or the node raises "The figsize of all plots must be the same."Stdout(STRING) - everything yourprint()calls wrote, streamed live into the node's UI as the cell runs. Shows[No output]when you printed nothing.
Install
ComfyUI Manager (search "Notebook"), or the manual route:
cd ComfyUI/custom_nodes
git clone https://github.com/liusida/ComfyUI-Notebook
Then restart ComfyUI. The only Python dependency the pack adds is matplotlib (check requirements.txt); torch, numpy, and PIL come from ComfyUI's own environment, so there's no model download and nothing heavy to fetch.
Troubleshooting
- Node doesn't show up after restart - your ComfyUI is too old for the V3 API it's written against. Update ComfyUI first.
- No syntax highlighting - the Monaco editor is loaded from a CDN (cdnjs). If the browser can't reach it you get a plainer editor, but the code still runs.
- Variables not persisting - each workflow gets its own kernel, so cells in a different workflow tab share nothing. There's also a
/notebook/freeAPI that clears the namespace and frees memory; run it when the kernel's accumulated junk starts worrying you. - Memory creep - the kernel holds everything you've defined. Long sessions where cells keep allocating tensors will leak until you clear the kernel or restart ComfyUI.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| code | STRING | Python code to execute. Use 'input' for connected data. Variables defined in cells are automatically shared. | |
| inputopt | * | Optional input from another cell. Access via 'input' variable. | |
| input_2opt | * | Optional extra input. Access via 'input_2' variable. |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| Result | * | — |
| Plot | IMAGE | — |
| Stdout | STRING | — |