| name | jupyter-patterns |
| description | Apply best practices for Jupyter notebooks: cell ordering, reproducibility, parameterisation. Use when working with .ipynb files. |
SKILL: Jupyter Notebook Patterns
Notebook Structure
Cells should follow this order:
- Setup — imports, configuration, constants
- Data Loading — load raw data with validation
- EDA — exploratory data analysis, distributions, correlations
- Preprocessing — cleaning, feature engineering
- Modeling — model training and evaluation
- Visualization — charts and figures
- Conclusions — findings summary, next steps
Parameterized Execution with Papermill
dataset = "data/train.csv"
output_dir = "outputs"
n_estimators = 100
random_state = 42
papermill input.ipynb output.ipynb \
-p dataset "data/test.csv" \
-p n_estimators 200 \
-p random_state 0
Programmatic Notebook Creation
import nbformat as nbf
nb = nbf.v4.new_notebook()
nb.cells = [
nbf.v4.new_markdown_cell("# Analysis: {Title}"),
nbf.v4.new_code_cell("import pandas as pd\nimport numpy as np"),
nbf.v4.new_code_cell("df = pd.read_csv('data.csv')\ndf.head()"),
]
with open('analysis.ipynb', 'w') as f:
nbf.write(nb, f)
Export
jupyter nbconvert --to html --execute notebook.ipynb
jupyter nbconvert --to pdf --execute notebook.ipynb
jupyter nbconvert --to notebook --execute --inplace notebook.ipynb
Git Hygiene
jupyter nbconvert --to notebook --ClearOutputPreprocessor.enabled=True \
--inplace notebook.ipynb
pip install nbstripout
nbstripout --install
Rules
- Restart kernel and run all cells before committing.
- Clear all outputs before git commit.
- Tag parameter cells for papermill.
- Each notebook should be self-contained and reproducible.
- Include
random_state parameter for reproducibility.
- Use relative paths for data files.