Jupyter Notebook Reports: Clean Reruns, Parameters, HTML
skillMake a Jupyter notebook rerun from a clean start and read as a report: parameters, pinned environment, HTML export. Use to share or publish a notebook.
agents that use this skill
instructions
Overview
A notebook mixes code, results and prose, and it keeps state in a running kernel, so what a reader sees can differ from what a clean run produces. This skill turns an analysis notebook into one that reruns from a fresh kernel, tells the story in its text, and is shared as HTML or PDF without secrets or stale output. The deliverable is the exported report from a clean top-to-bottom run that was checked, plus the notebook source with its outputs cleared.
Mental model
- The notebook file is the source and the kernel is a cache. Only a fresh kernel proves the file works. Rule et al. (2019) tell authors to restart the kernel and run all cells regularly, and to put key variables at the top.
- The reader is a person with a question. Rule et al. advise telling a story with an introduction, steps and interpretation, and writing for your future self.
- A shared notebook is a publication: no output from an old run, no private paths, no credentials.
Rules
Structure
Order the notebook as: title and the question in one sentence; data (source, extract date, row counts); analysis in labelled steps; conclusion with the answer first. Put the parameters and imports in the first code cell.
- One job per cell, each under a markdown heading. Rule et al. call cells over about 100 lines or one page too long.
- Move repeated code into functions or a module instead of copying cells.
- The conclusion is a markdown cell written by hand. A number in the prose that could change on a rerun comes from a computed variable, not typed text.
- Long work splits into several notebooks with an index notebook.
Hidden state and cell order
| Hazard | Check |
|---|---|
| A variable defined in a cell that was deleted or run once | Fresh-kernel run fails with NameError |
| A cell run twice, or out of order, so results depend on history | Execution counts are not 1, 2, 3 in order |
| A dataframe modified in place by a later cell, then reused by an earlier one | Fresh run gives different numbers |
| A file read that an earlier run wrote | Delete the files the notebook creates, rerun |
| Time, randomness or an external service | Pin or record: the seed, the as-of date, the extract |
The clean-run gate
Execute top to bottom in a fresh kernel and let it stop at the first error. This command exits non-zero when a cell raises. Each cell has a 30 s limit by default; for slow cells add --ExecutePreprocessor.timeout=-1 to remove the limit:
uv run --with nbconvert --with ipykernel jupyter nbconvert --to notebook --execute --inplace report.ipynbAdd a --with for each library the notebook imports, or run the same command inside the project's own environment. Add --allow-errors only to inspect a broken notebook, never for the final run. Compare the key numbers of the clean run with the numbers in the prose; if they differ, fix the prose or the code. Execution counts after the run must be 1, 2, 3 in order.
Parameters
- Put every value that changes between runs (period, region, input path) in one cell near the top, tagged
parameters, with a default. - Run with papermill, which injects an overriding cell after the tagged cell:
uv run --with papermill --with ipykernel papermill report.ipynb report-2026-11.ipynb -k python3 -p month 2026-11-psets one value; numbers and booleans are typed from their look, and-rforces a string.-f params.yamlreads values from a file.-knames the kernel and overrides the kernel name in the notebook metadata.- Quarto uses the same tag and takes values on the command line:
quarto render report.ipynb --execute -P month:2026-11. Quarto does not run the cells of an .ipynb unless--executeis passed, and it applies the values to a notebook file through the papermill package: install it in the Python Quarto runs, then confirm the page shows the value you passed.
Environment and seeds
- Record the environment: package versions in a requirements or lock file, and print the key ones in the first cell (Rule et al. suggest the watermark package). With uv, a script header or
uv run --with <package>names what the notebook needs. - Record the random seed for any analysis that includes randomness (Sandve et al., rule 6): set it once in the parameters cell, and say in the text which steps depend on it.
- State the data extract date and the query. A notebook that reads a live table gives different answers on different days; read a dated snapshot for a published report.
Hide code for readers
| Goal | How |
|---|---|
| HTML for readers, no code | jupyter nbconvert --to html --no-input report.ipynb |
| Remove chosen cells (setup, debug) | Tag them, then --TagRemovePreprocessor.enabled=True --TagRemovePreprocessor.remove_cell_tags <tag> |
| Images linked from markdown cells embedded in the HTML | add --embed-images; chart outputs are embedded already |
| Quarto | execute: echo: false hides code; code-fold: true under format: html lets readers expand it |
Hiding code does not hide data or paths in outputs. Read the exported file before sending it.
Export
- HTML:
jupyter nbconvert --to html report.ipynbwrites one HTML file with chart outputs embedded; the default template is lab. - PDF through nbconvert needs pandoc and a TeX install (xelatex). Without them, open the HTML and print to PDF from a browser, or use Quarto if it is installed.
- Keep the executed notebook and the export together, named by period.
- In a Quarto project,
freeze: truekeeps stored results and skips re-execution during a whole-project render;freeze: autore-executes only documents whose source changed. Results go in a_freezefolder: commit it so collaborators render without your computing environment. Changed input data is not detected, so re-render fully when data changes.
Clear outputs before sharing the source
Share the executed HTML for reading. Share the notebook itself with outputs cleared so results cannot go stale or leak:
jupyter nbconvert --clear-output --inplace report.ipynbOutputs become empty and execution counts null. When the notebook is committed, use a notebook-aware diff tool such as nbdime, as Rule et al. advise, or a text pairing, because a notebook is JSON.
No secrets, no absolute paths
Read credentials from environment variables or a secrets store, never from a cell. Use paths relative to the notebook or a parameter. Before sharing, search the file; the output of the search must be empty or explained:
grep -nEi '/Users/|/home/|api[_-]?key|token|secret|password' report.ipynbThe search finds text, not meaning: read the outputs of data-loading cells too.
Examples
Out-of-order to clean run. Bad: the chart cell uses df_clean, defined in a cell the author later deleted; it works only in the author's open kernel. Good: the cleaning step is its own labelled cell above the chart, and a fresh-kernel run reproduces the figure.
Results as typed text. Bad: "Revenue grew 12%" typed into a markdown cell. Good: the markdown cell is generated from the computed value, or the sentence is rewritten after each clean run and compared against the output.
Absolute path to parameter. Bad: pd.read_csv("/Users/ana/Downloads/q3.csv"). Good: pd.read_csv(data_path) with data_path set in the parameters cell to a relative path.
Checklist
- The notebook opens with the question and ends with the answer in prose.
- A fresh-kernel, top-to-bottom run finished without error; execution counts are in order.
- Numbers in the prose match the clean run.
- Parameters sit in one tagged cell; the seed, the as-of date and package versions are recorded.
- The HTML export was opened; code is hidden or shown on purpose.
- The shared source has outputs cleared.
- The secrets and path search came back empty or each hit was explained.
- Any step that could not run (no Quarto, no TeX) is named in the hand-off.
Composition / References
Scope limits: the content of the analysis, chart design, the structure of a data report outside notebooks and dashboards are separate jobs.
- Sandve et al., "Ten Simple Rules for Reproducible Computational Research", PLOS Computational Biology, 2013: https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1003285
- Rule et al., "Ten simple rules for writing and sharing computational analyses in Jupyter Notebooks", PLOS Computational Biology, 2019: https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1007007
- nbconvert, "Using as a command line tool": https://nbconvert.readthedocs.io/en/latest/usage.html and "Removing cells": https://nbconvert.readthedocs.io/en/latest/removing_cells.html
- Papermill, "Execute": https://papermill.readthedocs.io/en/latest/usage-execute.html
- Quarto: https://quarto.org/docs/computations/parameters.html , https://quarto.org/docs/computations/execution-options.html , https://quarto.org/docs/output-formats/html-code.html , https://quarto.org/docs/projects/code-execution.html , https://quarto.org/docs/tools/jupyter-lab.html