Sub topic 14
Checking notebooks automatically
This chapter assumes you can run a notebook headlessly with jupyter nbconvert or Papermill, keep dependencies in a requirements.txt, and use Git and basic YAML. The goal is to make a machine run your notebook on every change. If someone's edit breaks the notebook, you find out before a reader does.
Why a manual check is not enough
"Restart and run all" works, but only when someone remembers to do it. A notebook that ran cleanly last month can fail today because a teammate renamed a column, a package released a new version, or a cell was edited out of order. An automated job repeats the clean run on every push, in a fresh environment, with no state left over from anyone's session.
The job has three parts:
- Check out the repository on a clean machine.
- Create the environment from the pinned requirements file.
- Execute the notebook from top to bottom and fail on any error.
The check as a CI job
Continuous integration (CI) is a service that runs commands automatically when you push code. The example below uses GitHub Actions. Save it as .github/workflows/notebooks.yml.
name: notebooks
on:
push:
pull_request:
jobs:
run-notebook:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: pip
- name: Install pinned dependencies
run: |
pip install -r requirements.txt
pip install nbconvert ipykernel
- name: Execute notebook
run: |
jupyter nbconvert --to notebook --execute analysis.ipynb \
--output analysis_executed.ipynb
- name: Keep the executed copy
if: always()
uses: actions/upload-artifact@v4
with:
name: executed-notebook
path: analysis_executed.ipynbIf any cell raises an error, nbconvert exits with a non-zero status and the job fails. The last step uses if: always() so the executed copy is saved even after a failure. Open it to see which cell broke and what the output looked like.
Make the check meaningful
A notebook that runs without errors can still give wrong answers. Add a few cheap checks inside the notebook itself:
- Use
assertstatements for facts that must hold, such as row counts, no missing values in key columns, or a result within an expected range. - Reuse the SHA-256 check from earlier so the job fails if the input data changed.
- Seed random generators, so a threshold on a result does not fail by chance.
assert len(df) > 0, "No rows were loaded"
assert df["value"].notna().all(), "Missing values in 'value'"
assert 0.0 <= accuracy <= 1.0, f"Accuracy out of range: {accuracy}"Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| Works locally, fails in CI | Package missing from `requirements.txt` | Regenerate it from a clean environment |
| `No such kernel` error | `ipykernel` is not installed in CI | Install it, as in the example above |
| File not found | Data is not in the repository | Download it in a step, or use a small sample |
| Job takes too long | Full dataset is used | Use a small sample in CI via a parameter |
Exercise
A notebook passes "Restart and run all" on your laptop but the CI job fails with ModuleNotFoundError: No module named 'scipy'. What is the most likely cause, and how do you fix it?
Show answer
The notebook imports scipy, but it was installed only in your local environment and is missing from requirements.txt, so the clean CI machine does not have it. Recreate a clean virtual environment, install what the notebook needs, run pip freeze > requirements.txt, commit the file, and push again.