Don't let notebook cells run out of order
Project description
enforce-notebook-run-order
Enforce the run order of Jupyter notebooks.
Jupyter notebooks are great for interactive data analysis. However, they can encourage a bad habit: running cells out of order. This can lead to notebooks being committed to the repository in a state where they don't run from top to bottom, and other collaborators may receive different results when trying to reproduce the analysis.
enforce-notebook-run-order attempts to fix this by raising an
exception before each commit if any cells are run out of order.
Language Support
This tool works with all Jupyter notebook kernels, including:
- Python (IPython)
- R (IRkernel)
- Julia
- Scala, Java, C++, and many others
Any language kernel that produces standard .ipynb files with
execution_count metadata is supported. The tool is language-agnostic
and only inspects the notebook's execution order metadata.
Validation Requirements
For a notebook to be considered valid, it must meet the following requirements:
- All non-empty code cells must be executed: Each code cell with
content must have an
execution_countvalue (notNone). - Execution must start from 1: The first non-empty code cell must
have
execution_count=1. - No gaps in execution sequence: Execution counts must be strictly sequential (1, 2, 3, ...) with no skipped numbers.
Examples:
- ✅ Valid: Cells with execution counts
1, 2, 3, 4 - ❌ Invalid: Cells starting with
0, 1, 2(must start from 1) - ❌ Invalid: Cells starting with
2, 3, 4(must start from 1) - ❌ Invalid: Cells with
1, 2, 4, 5(gap at 3) - ❌ Invalid: Cells with
1, 3, 2(not sequential) - ❌ Invalid: Cells with
1, 2, None(unexecuted cell)
Note: Empty code cells (cells with no content) are ignored and do not need to be executed.
Usage
enforce-notebook-run-order is designed to work primarily as a
pre-commit hook, but can also be used as a
standalone script when needed.
pre-commit hook (Recommended)
The intended way to use enforce-notebook-run-order is as a pre-commit
hook to automatically validate notebook execution order before each
commit.
To set it up, add the following to your .pre-commit-config.yaml:
repos:
- repo: https://github.com/cmhac/enforce-notebook-run-order
rev: <replace with latest version from https://github.com/cmhac/enforce-notebook-run-order/releases/>
hooks:
- id: enforce-notebook-run-order
This will automatically check all notebooks in your repository before each commit, preventing out-of-order execution from being committed to your repository.
Standalone
For manual validation or CI integration, enforce-notebook-run-order
can be used as a standalone script.
First, install it the same way you install other Python packages, such as:
pip install enforce-notebook-run-order
Run it with the path to the notebook(s) you want to check:
nbcheck my_notebook.ipynb my_other_notebook.ipynb
Or point it to a directory to check all notebooks in that directory:
nbcheck my_notebooks/
If no paths are specified, nbcheck will check all notebooks in the
current directory.
You can also use the full enforce-notebook-run-order command, but the
nbcheck command is provided as a convenience.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file enforce_notebook_run_order-2.1.1.tar.gz.
File metadata
- Download URL: enforce_notebook_run_order-2.1.1.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8258db49c5d05418a49a097f14159083c0267741118ee6d624fb8a812c11d8ff
|
|
| MD5 |
a3646261ebfbe01a4610a388d26c62aa
|
|
| BLAKE2b-256 |
eca29689cd1ee75c72d6e92a71e1f126da74d59e39d6c7e7264ab98687710c06
|
File details
Details for the file enforce_notebook_run_order-2.1.1-py3-none-any.whl.
File metadata
- Download URL: enforce_notebook_run_order-2.1.1-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.25
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b65f511a31721f985c39e711186fa3287cc8ec5b5a02b43e39857cb652a8b979
|
|
| MD5 |
f949ae9595d71c630990054963b4123a
|
|
| BLAKE2b-256 |
8f8f9444800014183bdcb115c5d6ae000fb2137ad7acfd9d4cac3db91ba9bcbf
|