Sub topic 13
Export and run notebooks with nbconvert
This chapter assumes you have a working virtual environment with JupyterLab installed, a notebook called analysis.ipynb, and basic comfort with the terminal. You should also know why running cells in order matters, which the earlier chapter on hidden state covered.
What nbconvert does
nbconvert is a command-line tool that ships with JupyterLab. It reads an .ipynb file and either converts it to another format or runs it. You can do both without opening a browser. That makes it useful for sharing results with people who do not use Jupyter, and for checking that a notebook still works from top to bottom.
The two jobs in this chapter are:
- Exporting a notebook to a static format such as HTML.
- Executing a notebook and saving the fresh outputs.
Export a notebook to HTML
Run this from the project folder, with the virtual environment active:
jupyter nbconvert --to html analysis.ipynbThis writes analysis.html next to the notebook. The file contains your code, text, and the outputs that were saved in the notebook. Anyone can open it in a browser, and no Python installation is needed.
Export does not run any code. If the notebook has stale or missing outputs, the HTML shows exactly that. To check which formats your install supports, run jupyter nbconvert --help. HTML, Markdown, and Python script are common choices. PDF needs extra software, so start with HTML.
Run a notebook from the command line
To re-run every cell in order and save the new outputs, use the --execute option:
jupyter nbconvert --to notebook --execute --inplace analysis.ipynbThe options work like this:
| Option | Effect |
|---|---|
| `--to notebook` | The output format is another `.ipynb` file. |
| `--execute` | Run all cells from the top in a fresh kernel before saving. |
| `--inplace` | Overwrite the input file instead of creating a new one. |
Because the run starts with a fresh kernel, it behaves like "Restart Kernel and Run All Cells". Any cell that depends on a variable defined later, or on a cell you deleted, fails here. That makes this command a quick test of reproducibility.
You can combine both jobs into one step. This runs the notebook and writes an HTML report without touching the original file:
jupyter nbconvert --to html --execute analysis.ipynbCommon errors
- The run stops at the first failing cell.
nbconvertraises an error and does not save the result. Fix the cell, or add--allow-errorsif you want to keep going and record the error output. - Wrong kernel. The notebook stores a kernel name in its metadata. If that kernel is not registered on the machine, the run fails with a "No such kernel" message. Register it as described in the kernels chapter, or pass
--ExecutePreprocessor.kernel_name=data-analysis. - Relative paths break. Paths like
data/sales.csvare resolved from the notebook's folder. Run the command from the project folder so the paths match what you saw in JupyterLab. - Long cells time out. The default per-cell limit is 30 seconds. Raise it with
--ExecutePreprocessor.timeout=600. - Overwriting by accident.
--inplacereplaces your file. Commit to Git first, or leave the flag out and use--outputto choose a new name.
Exercise
You need to send a colleague a report that shows current results, but they have no Python installed. The notebook's saved outputs are out of date. Which single command produces a suitable file without changing the original notebook?
Show answer
jupyter nbconvert --to html --execute analysis.ipynb. The --execute option re-runs all cells in a fresh kernel so the outputs are current, and omitting --inplace leaves analysis.ipynb unchanged. The result is analysis.html, which opens in any browser.
Exercise
A scheduled job runs jupyter nbconvert --to notebook --execute --inplace analysis.ipynb and fails with "No such kernel: data-analysis". What is the likely cause and a fix?
Show answer
The notebook's metadata names a kernel that is not registered in the environment running the job. Activate the correct virtual environment and register the kernel with python -m ipykernel install --user --name data-analysis, or pass --ExecutePreprocessor.kernel_name with a kernel that exists.