Parameterize¶
See also
Generally, the first workflow step when using papermill is to parameterize the notebook.
To do this, tag notebook cells with parameters. These parameters are later used when the notebook is executed or run.
Designate parameters for a cell¶
To parameterize a notebook, designate a cell with the tag parameters.
Notebook¶
If using the Jupyter Notebook interface
Activate the tagging toolbar by navigating to
View,Cell Toolbar, and thenTagsEnter
parametersinto a textbox at the top right of a cellClick
Add tag
JupyterLab 3.0+¶
If using JupyterLab v3 or above:
Select the cell to parameterize
Click the property inspector in the right sidebar (double gear icon)
Type “parameters” in the “Add Tag” box and hit “Enter”.
JupyterLab 2.0 - 2.2.x¶
If using the JupyterLab interface
Select the cell to parameterize
Click the property inspector on the left side (double gear icon)
Type “parameters” in the “Add Tag” box and hit “Enter”.
JupyterLab < 2.0¶
If using JupyterLab < 2.0, consider using the jupyterlab-celltags extension.
Select the cell to parameterize
Click the cell inspector (wrench icon)
Type “parameters” in the “Add Tag” box and hit “Enter”.
If the extension is not installed, you can manually add the tag by editing the Cell Metadata field in the cell inspector by adding “tags”:[“parameters”].
Learn more about the jupyter notebook format and metadata fields here.
How parameters work¶
The parameters cell is assumed to specify default values which may be overridden by values specified at execution time.
papermill inserts a new cell tagged
injected-parametersimmediately after theparameterscellinjected-parameterscontains only the overridden parameterssubsequent cells are treated as normal cells, even if also tagged
parametersif no cell is tagged
parameters, theinjected-parameterscell is inserted at the top of the notebook
One caveat is that a parameters cell may not behave intuitively with inter-dependent parameters. Consider a notebook note.ipynb with two cells:
#parameters a = 1 twice = a * 2 print("a =", a, "and twice =", twice) when executed with papermill note.ipynb -p a 9, the output will be a = 9 and twice = 2 (not twice = 18).