view source

Jupyter Notebook Reports: Clean Reruns, Parameters, HTML

skill

Make 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.

Apache-2.0 Jupyter notebooks with Python. Commands run with uv fetching nbconvert, papermill and ipykernel on demand; Quarto and a TeX install are optional.

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

HazardCheck
A variable defined in a cell that was deleted or run onceFresh-kernel run fails with NameError
A cell run twice, or out of order, so results depend on historyExecution counts are not 1, 2, 3 in order
A dataframe modified in place by a later cell, then reused by an earlier oneFresh run gives different numbers
A file read that an earlier run wroteDelete the files the notebook creates, rerun
Time, randomness or an external servicePin 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:

text
uv run --with nbconvert --with ipykernel jupyter nbconvert --to notebook --execute --inplace report.ipynb

Add 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:
text
uv run --with papermill --with ipykernel papermill report.ipynb report-2026-11.ipynb -k python3 -p month 2026-11
  • -p sets one value; numbers and booleans are typed from their look, and -r forces a string. -f params.yaml reads values from a file. -k names 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 --execute is 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

GoalHow
HTML for readers, no codejupyter 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 HTMLadd --embed-images; chart outputs are embedded already
Quartoexecute: 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.ipynb writes 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: true keeps stored results and skips re-execution during a whole-project render; freeze: auto re-executes only documents whose source changed. Results go in a _freeze folder: 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:

text
jupyter nbconvert --clear-output --inplace report.ipynb

Outputs 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:

text
grep -nEi '/Users/|/home/|api[_-]?key|token|secret|password' report.ipynb

The 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.