Notebooks as executable documents
A notebook file stores cells, metadata, and possibly outputs. A kernel owns the
live Python process and mutable state. Saved output is evidence from a prior
execution, not proof that the current source runs in order.
Workflow
- Inspect notebook format version, kernelspec, language metadata, cell order,
execution counts, imports, file paths, parameters, secrets, widgets, large
outputs, and repository environment.
- Choose the correct kernel from the project environment. Record interpreter
and dependency versions; do not trust a familiar kernel display name.
- Make dependencies and input paths explicit. Put reusable logic in imported
modules with tests; keep the notebook for orchestration, explanation, and
display.
- Restart and execute all cells top-to-bottom in a clean process. Stop on the
first unexpected error; do not use
allow_errors to make a failing notebook
appear successful.
- Write the executed notebook to a separate artifact unless in-place mutation
was explicitly requested. Inspect error outputs, execution counts, output
size, and sensitive content.
- Re-run from the same clean inputs and compare material artifacts. Persist
scripts, data products, or reports separately when they are the canonical
result.
Invariants
- Out-of-order success is hidden-state failure. A clean top-to-bottom run is
the minimum reproducibility test.
- A notebook kernel can outlive cell edits and deletions; restart before proof.
- Do not embed credentials, personal row data, or huge binary/base64 output.
- Set random seeds and control clocks/network inputs when reproducibility
requires them, while documenting unavoidable nondeterminism.
- Parameter cells or a small configuration object are preferable to manual
edits scattered through cells.
import nbformat
from nbclient import NotebookClient
notebook = nbformat.read("analysis.ipynb", as_version=4)
client = NotebookClient(
notebook,
timeout=600,
kernel_name="python3",
resources={"metadata": {"path": "notebooks"}},
)
client.execute()
nbformat.write(notebook, "artifacts/analysis.executed.ipynb")
Read state and structure, execution and verification,
and module/report boundaries.
1---2name: jupyter-python3description: Create, review, debug, test, or reproduce Python Jupyter notebooks by inspecting format, executing cells top-to-bottom in a clean kernel, and verifying outputs.4---56# Notebooks as executable documents78A notebook file stores cells, metadata, and possibly outputs. A kernel owns the9live Python process and mutable state. Saved output is evidence from a prior10execution, not proof that the current source runs in order.1112## Workflow13141. Inspect notebook format version, kernelspec, language metadata, cell order,15 execution counts, imports, file paths, parameters, secrets, widgets, large16 outputs, and repository environment.172. Choose the correct kernel from the project environment. Record interpreter18 and dependency versions; do not trust a familiar kernel display name.193. Make dependencies and input paths explicit. Put reusable logic in imported20 modules with tests; keep the notebook for orchestration, explanation, and21 display.224. Restart and execute all cells top-to-bottom in a clean process. Stop on the23 first unexpected error; do not use `allow_errors` to make a failing notebook24 appear successful.255. Write the executed notebook to a separate artifact unless in-place mutation26 was explicitly requested. Inspect error outputs, execution counts, output27 size, and sensitive content.286. Re-run from the same clean inputs and compare material artifacts. Persist29 scripts, data products, or reports separately when they are the canonical30 result.3132## Invariants3334- Out-of-order success is hidden-state failure. A clean top-to-bottom run is35 the minimum reproducibility test.36- A notebook kernel can outlive cell edits and deletions; restart before proof.37- Do not embed credentials, personal row data, or huge binary/base64 output.38- Set random seeds and control clocks/network inputs when reproducibility39 requires them, while documenting unavoidable nondeterminism.40- Parameter cells or a small configuration object are preferable to manual41 edits scattered through cells.4243```python44import nbformat45from nbclient import NotebookClient4647notebook = nbformat.read("analysis.ipynb", as_version=4)48client = NotebookClient(49 notebook,50 timeout=600,51 kernel_name="python3",52 resources={"metadata": {"path": "notebooks"}},53)54client.execute()55nbformat.write(notebook, "artifacts/analysis.executed.ipynb")56```5758Read [state and structure](references/state.md), [execution and verification](references/execution.md),59and [module/report boundaries](references/boundaries.md).