Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. import json
  5.  
  6. def renumber_nb(nb_file_name):
  7. assert nb_file_name.endswith('.ipynb')
  8. base = '.'.join(nb_file_name.split('.')[:-1])
  9. i = 1
  10. j = json.load(open(nb_file_name, 'r'))
  11. for cell in j['cells']:
  12. if cell['cell_type'] == 'code':
  13. cell['execution_count'] = i
  14. for o in cell['outputs']:
  15. if 'data' in o and o['output_type'] == 'execute_result':
  16. o['execution_count'] = i
  17. i += 1
  18. outfile = '{}-numbered.ipynb'.format(base)
  19. json.dump(j, open(outfile, 'w'))
  20. return outfile
  21.  
  22. if __name__ == '__main__':
  23. if len(sys.argv) != 2:
  24. print('Usage: {} <jupyter_notebook.ipynb>'.format(sys.argv[0]))
  25. sys.exit(1)
  26. outfile = renumber_nb(sys.argv[1])
  27. print('Wrote {}'.format(outfile))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement