Created
January 9, 2022 20:31
-
-
Save BlairCurrey/eee6f3455ed953dee47f4324698823b2 to your computer and use it in GitHub Desktop.
python function to re order execution count of jupyter nb cells
This file contains hidden or 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 | |
def reorder_execution_count(infile, outfile): | |
with open(infile, encoding='utf-8') as f: | |
nb = json.load(f) | |
count = 1 | |
for c in nb['cells']: | |
if 'execution_count' in c: | |
c['execution_count'] = count | |
if 'outputs' in c: | |
for o in c['outputs']: | |
if 'execution_count' in o: | |
o['execution_count'] = count | |
count += 1 | |
with open(outfile, 'w', encoding='utf-8') as outfile: | |
json.dump(nb, outfile, indent=2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not entirely sure what expected behavior should be for multiple output cells from 1 input cell as I have not run into that scenario. For 1 output cell the
expected_count
should be the same as the input cell. I made the assumption that this would also be true for more than 1 ouput cell.