Skip to content

Instantly share code, notes, and snippets.

@BlairCurrey
Created January 9, 2022 20:31
Show Gist options
  • Save BlairCurrey/eee6f3455ed953dee47f4324698823b2 to your computer and use it in GitHub Desktop.
Save BlairCurrey/eee6f3455ed953dee47f4324698823b2 to your computer and use it in GitHub Desktop.
python function to re order execution count of jupyter nb cells
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)
@BlairCurrey
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment