Skip to content

Instantly share code, notes, and snippets.

@geekman
Created December 25, 2014 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geekman/690f0bb766f9fede911b to your computer and use it in GitHub Desktop.
Save geekman/690f0bb766f9fede911b to your computer and use it in GitHub Desktop.
sequentially re-numbers cells in an IPython Notebook file
#!/usr/bin/python
#
# sequentially re-numbers cells in an IPython notebook
# 2014.12.26 darell tan
#
import json
import sys
def main():
f1 = open(sys.argv[1], 'rb')
f2 = open(sys.argv[1] + '.new', 'wb')
cell_num = 1
nb = json.load(f1)
for cell in nb['worksheets'][0]['cells']:
if 'prompt_number' in cell:
cell['prompt_number'] = cell_num
cell_num += 1
json.dump(nb, f2, indent=1, separators=(',', ': '), sort_keys=True)
f1.close()
f2.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment