Reports from notebooks
Last updated
The owners don't want a notebook. They want a page they can open, read and print. In this lesson you'll turn a notebook into a clean HTML or PDF report with nbconvert and with Quarto, decide which code and output the reader sees, and add a parameter so the same notebook makes next month's report with one command.
By the end of this lesson you can
- Export a notebook as HTML or PDF with nbconvert
- Hide code or remove cells in a report with tags and Quarto options
- Rerun a report for a new month with a parameter
From notebook to report
A report made from a notebook has two big advantages over one typed into a word processor: every number and chart comes straight from the data, and next month's report is a rerun, not a rewrite.
What the reader sees is your choice, cell by cell:
- Setup cells (imports, loading, cleaning): removed.
- Chart and table cells: output only. The owners don't need the code.
- Markdown cells: the story (10.1).
- Working you'd show a colleague: kept, with code, in a technical version, or in the appendix.
Try it: choose what each code cell shows, and export.
# The Tindahan, December 2024
import pandas as pd books = pd.read_csv("data/clean/books.csv")December took ₱357,952 across the four stores, 6.9% more than last December.
fig = rosario_chart(books) fig
summary.loc["pesos_2024"]
KeyError: 'pesos_2024' Traceback (most recent call last): File "<cell 5>", line 1 ...
Caveat: card data covers card holders only.
The Tindahan, December 2024
import pandas as pd
books = pd.read_csv("data/clean/books.csv") December took ₱357,952 across the four stores, 6.9% more than last December.
fig = rosario_chart(books) fig
[chart] Rosario is the only store below last year, every month since May
summary.loc["pesos_2024"]
KeyError: 'pesos_2024' Traceback (most recent call last): File "<cell 5>", line 1 ...
Caveat: card data covers card holders only.
The traceback went into the report: the notebook was exported as it was last saved, error and all.
nbconvert
nbconvert comes with Jupyter. It turns a notebook into HTML, PDF, Markdown, a Python script and more. Run it on your computer, in the project folder, with the environment active (1.4):
(.venv) $ python -m pip install nbconvert (.venv) $ jupyter nbconvert --to html report.ipynb [NbConvertApp] Converting notebook report.ipynb to html [NbConvertApp] Writing 302118 bytes to report.html (.venv) $ jupyter nbconvert --to html --no-input --execute report.ipynb
--to htmlwritesreport.htmlnext to the notebook: one file, with the charts inside it, that opens in any browser.--no-inputleaves out every code cell's code and theIn [3]:prompts, keeping the outputs.--executeruns the whole notebook from the top first, in a fresh kernel. That's "Restart and run all" (1.3), done for you every time.--to pdfneeds LaTeX installed.--to webpdfmakes a PDF through a browser instead: install it withpython -m pip install "nbconvert[webpdf]".
To remove particular cells, give them a tag. In JupyterLab, open the property inspector (the gear icon on the right), select the cell, and add the tag remove_cell. Then:
(.venv) $ jupyter nbconvert --to html --no-input --execute --TagRemovePreprocessor.enabled=True --TagRemovePreprocessor.remove_cell_tags remove_cell report.ipynb
Wrong way: the traceback in the report
On Friday afternoon, the analyst changed one column name in the cleaning notebook and exported the report without rerunning it. The owners opened a page with a red block in the middle: KeyError: 'pesos_2024' and a traceback. Nobody had read it before it went out.
A notebook's saved outputs are whatever happened the last time each cell ran, in whatever order. Exporting without running it again publishes that state, errors and all. With --execute, the export runs everything fresh, and stops if a cell fails:
(.venv) $ jupyter nbconvert --to html --no-input --execute report.ipynb [NbConvertApp] Converting notebook report.ipynb to html ... nbclient.exceptions.CellExecutionError: An error occurred while executing the following cell: ------------------ summary.loc["pesos_2024"] ------------------ KeyError: 'pesos_2024'
That failure is what you want: nothing goes out until the notebook runs cleanly from the top. (--allow-errors would carry on and put the error in the report. Don't use it for reports.)
The check: always export with --execute, and open the report yourself before sending it.
Quarto
Quarto is a publishing tool that renders notebooks (and its own .qmd files) as HTML, PDF, Word documents, slides and websites. It installs separately from Python: download it from quarto.org. Its settings go in the notebook itself. A raw cell at the very top (in JupyterLab, change a cell's type to Raw) holds the document's options, in YAML:
--- title: "The Tindahan, December 2024" format: html execute: echo: false ---
echo: false hides the code of every cell. A single cell can change its own options with #| lines at its top:
#| label: fig-rosario #| fig-cap: "Per-day takings against the same month of 2023" fig = rosario_chart(books) fig
Then render it:
quarto render report.ipynb --execute quarto render report.ipynb --execute --to pdf quarto render report.ipynb --execute --to docx
Quarto doesn't run a notebook's cells unless you say --execute; without it, it uses the saved outputs, with the same danger as step 3. PDF needs LaTeX: quarto install tinytex installs a small one.
nbconvert or Quarto? For a quick HTML copy, nbconvert is already there. For a report with a title page, figure captions, cross-references and Word or PDF output, use Quarto.
A report for any month
A parameter is a value the report is built around, like the month, that changes each time it's run. Put the parameters in one cell at the top, and tag that cell parameters. Everything else must read from them. Try it here: change month to "2024-10", and run all.
For December 2024: ₱357,952 across the four stores, +6.9% on December 2023. No date is typed anywhere except the parameters cell, and every sentence is an f-string (9.10).
On your computer, Quarto sets the parameter from the command line. It runs the notebook with a new cell, after the parameters cell, that sets month to the value you gave:
quarto render report.ipynb --execute -P month:2025-01 --output-file 2025-01.html quarto render report.ipynb --execute -P month:2025-02 --output-file 2025-02.html
Each month is now one command. The same pattern makes one report per store, or per barangay.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- nbconvert: Using as a command line tool: --to html, --to pdf, --to webpdf, and --no-input
- nbconvert: Executing notebooks: --execute, and --allow-errors
- nbconvert: Removing cells, inputs, or outputs: TagRemovePreprocessor and cell tags
- Quarto: Using Python: rendering notebooks, --execute, and #| cell options
- Quarto: Parameters: the parameters tag and -P on the command line