Sub topic 10
Parameterizing a Notebook
This chapter assumes you can run a notebook from the command line and that you have already done a clean "Restart and run all" check. You should also be comfortable with a virtual environment and a pinned requirements.txt.
Why parameterize
A notebook often has values near the top that you edit by hand: a smoothing factor, an input file, a date range. Editing them between runs is a hidden-state risk. You may forget which values produced a saved result. A parameterized notebook declares those values in one place and lets a tool override them from outside, so each run is recorded by the command that launched it.
One cell for parameters
Put every value you might change in a single cell near the top. Give each one a default. Keep the cell free of logic, so a tool can find and replace the assignments.
alpha = 0.05
input_path = "data/raw/measurements.csv"
seed = 20240601Everything below this cell should read these names and never hard-code the same numbers again. If 0.05 appears again later in the notebook, an override will change only one of the two places, and the result will be wrong without any error.
Tagging the cell for Papermill
Papermill is a tool that runs a notebook and replaces parameter values. It looks for a cell tagged parameters. In JupyterLab, open the property inspector (the gear icon), select the cell, and add the tag parameters. Install it in your project environment, then record the version.
pip install papermill
pip freeze > requirements.txtWhen Papermill runs, it inserts a new cell right after the tagged one. That cell holds the overriding values, so the later assignments win while the defaults stay visible in the saved file.
Running with different values
Pass each override with -p. The second argument is the output notebook, which keeps the executed copy separate from your source.
papermill analysis.ipynb outputs/analysis_alpha_0.1.ipynb -p alpha 0.1Name the output file after the parameter value, as above, so you can tell runs apart later. Papermill converts values by type: 0.1 becomes a float and 20240601 becomes an integer. To pass a string that looks like a number, use -r (raw) instead of -p.
Common errors
- No cell is tagged. Papermill still runs, but it adds the overrides at the top and warns that no parameters cell was found. Defaults in your own cell may then overwrite the overrides if the cell runs later.
- A parameter name is misspelled. Papermill does not require the name to exist in the defaults, so a typo such as
alfaruns silently with no effect onalpha. Check the injected cell in the output notebook. - Parameters are changed later in the notebook. Reassigning
alphain a middle cell undoes the override. Treat parameters as read-only after the first cell. - Seed is not a parameter. If you run many parameter sets, vary the seed deliberately and record it, rather than letting it stay hidden inside a cell.
Exercises
Exercise
You run papermill analysis.ipynb out.ipynb -p alpha 0.1, but the results look identical to the default run. The notebook contains a cell tagged parameters with alpha = 0.05. Name two likely causes.
Show answer
A later cell reassigns alpha (for example alpha = 0.05 again), which overrides the injected value. Or the tagged cell is not the one that defines alpha, or the name was misspelled on the command line. Open the output notebook and check the injected cell and where alpha is used.
Exercise
Why is it better to run Papermill with a separate output path than to edit the parameter cell and re-run the source notebook?
Show answer
The command line records which values produced each result, and the source notebook stays unchanged as a template. Editing the cell by hand leaves no record of earlier runs and brings back hidden state.