Created
April 7, 2022 11:01
-
-
Save twolodzko/a6ea2bcef28a71041597977c30c30e38 to your computer and use it in GitHub Desktop.
Validate Jupyter notebook cell execution counts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import sys | |
# Validate if notebook cells were executed in order | |
if __name__ == '__main__': | |
if len(sys.argv) > 0: | |
path = sys.argv[1] | |
else: | |
raise RuntimeError("no path provided") | |
if not path.endswith(".ipynb"): | |
raise RuntimeError(f"{path} is not a notebook") | |
with open(path, "r") as file: | |
notebook = json.load(file) | |
prev_execution_count = 0 | |
for i, cell in enumerate(notebook["cells"]): | |
try: | |
execution_count = cell["execution_count"] | |
if execution_count is None: | |
continue | |
if execution_count != prev_execution_count + 1: | |
raise AssertionError(f"cell {i} was executed out of order") | |
prev_execution_count = execution_count | |
except KeyError: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment