Converting Notebooks to Scripts and Back
Notebooks and Scripts: Best of Both Worlds
Notebooks are great for exploration but terrible for version control (.ipynb files are JSON with embedded outputs), code review, and production pipelines. Tools like jupytext and papermill bridge the gap β letting you write in notebooks and deploy as scripts.
Definition: jupytext β Notebooks as Plain Text
jupytext β Notebooks as Plain Text
Jupytext synchronizes .ipynb notebooks with plain .py files:
pip install jupytext
# Convert notebook to script
jupytext --to py:percent notebook.ipynb
# Convert script back to notebook
jupytext --to notebook script.py
# Pair: auto-sync .ipynb and .py
jupytext --set-formats ipynb,py:percent notebook.ipynb
The percent format uses # %% markers to delimit cells:
# %% [markdown]
# # Title
# %%
import numpy as np
x = np.linspace(0, 1, 100)
Version-control the .py file (human-readable diffs) and
.gitignore the .ipynb file.
Definition: papermill β Parameterized Notebook Execution
papermill β Parameterized Notebook Execution
Papermill runs notebooks with different parameters from the CLI:
pip install papermill
# Run with specific parameters
papermill template.ipynb output.ipynb \
-p snr_db 15 \
-p modulation QPSK \
-p n_bits 100000
In the notebook, tag a cell as parameters:
# Parameters (tag this cell as "parameters")
snr_db = 10
modulation = "BPSK"
n_bits = 50000
Papermill injects new values before execution.
Definition: nbconvert β Export Notebooks
nbconvert β Export Notebooks
nbconvert exports notebooks to HTML, PDF, LaTeX, or slides:
# To HTML
jupyter nbconvert --to html notebook.ipynb
# To PDF (requires LaTeX)
jupyter nbconvert --to pdf notebook.ipynb
# To Python script
jupyter nbconvert --to script notebook.ipynb
# Clear outputs (before committing)
jupyter nbconvert --clear-output --inplace notebook.ipynb
Theorem: When to Use Notebooks vs. Scripts
Use the right tool for the task:
| Task | Format |
|---|---|
| Exploratory analysis | Notebook |
| Final figure generation | Script |
| Library/module code | Script (.py) |
| Teaching/tutorial | Notebook |
| Production pipeline | Script (+ CLI) |
| Parameter sweep | papermill + notebook |
| Code review | jupytext .py |
The ideal workflow: explore in notebooks, extract reusable code
to .py modules, keep notebooks as thin analysis wrappers.
Notebooks are for storytelling; scripts are for automation. Use both, not exclusively one or the other.
Example: Version-Controlled Notebook with jupytext
Set up a notebook that is automatically synced with a .py file
for version control.
Setup
# Pair the notebook
jupytext --set-formats ipynb,py:percent analysis.ipynb
# Add to .gitignore
echo "*.ipynb" >> .gitignore
# Commit only the .py file
git add analysis.py
git commit -m "Add analysis notebook (jupytext)"
Workflow
# Edit in Jupyter (changes sync to .py)
jupyter lab analysis.ipynb
# Or edit in VS Code (changes sync to .ipynb)
code analysis.py
# On another machine: regenerate notebook
jupytext --to notebook analysis.py
Example: Parameter Sweep with papermill
Run the same analysis notebook for multiple SNR values and collect the results.
Implementation
import papermill as pm
import pandas as pd
results = []
for snr in [5, 10, 15, 20]:
output = f'output/analysis_snr{snr}.ipynb'
pm.execute_notebook(
'template.ipynb', output,
parameters={'snr_db': snr, 'n_bits': 100000}
)
# Extract results from output notebook
nb = pm.read_notebook(output)
results.append(nb.dataframe)
all_results = pd.concat(results)
all_results.to_csv('sweep_results.csv')
Development Workflow Comparison
Compare notebook-only, script-only, and hybrid workflows for scientific projects.
Parameters
Notebook-to-Production Pipeline
Watch the progression from exploratory notebook to production script, showing how code gets extracted and tested.
Parameters
Common Mistake: .ipynb Files in Git
Mistake:
Committing .ipynb files with outputs. Every execution changes
cell execution counts and outputs, creating noisy diffs that are
impossible to review.
Correction:
Use jupytext to pair with .py files and commit only the .py.
Or use nbstripout as a git filter to strip outputs automatically:
pip install nbstripout
nbstripout --install
Key Takeaway
Use a hybrid workflow: explore in notebooks, deploy as scripts.
Use jupytext for version control, papermill for parameterized
execution, and nbconvert for exporting. Keep notebooks thin β
extract reusable code into .py modules.
Quick Check
What problem does jupytext solve?
Making notebooks run faster
Converting notebooks to plain text files for version control
Adding GPU support to notebooks
Compiling notebooks to native code
jupytext syncs .ipynb with .py files, enabling clean diffs and code review.
jupytext
A tool that converts Jupyter notebooks to/from plain text formats (Python scripts, Markdown) for version control.
papermill
A tool for parameterizing and executing Jupyter notebooks from the command line, enabling batch processing and parameter sweeps.
nbconvert
A Jupyter tool for converting notebooks to HTML, PDF, LaTeX, slides, or plain Python scripts.