Some quotes I bumped into last week
[gromacs.git] / python_packaging / sample_restraint / examples / strip_notebook.py
blobaa62454bbb64207268f30bb02ba74338831e9da3
1 #!/usr/bin/env python
2 # This script takes an ipython notebook as an argument and rewrites the file
3 # with metadata stripped to make change tracking with git easier.
4 import sys
5 import json
6 import os
7 import shutil
9 infile = sys.argv[1]
10 if not os.path.exists(infile):
11 sys.exit('command line argument must be an existing filename')
12 tempfile = infile+'.tmp'
14 with open(infile, 'r') as fh:
15 json_in = json.load(fh)
17 nb_metadata = json_in["metadata"]
18 def strip_output_from_cell(cell):
19 if "outputs" in cell:
20 cell["outputs"] = []
21 if "execution_count" in cell:
22 cell["execution_count"] = None
24 for cell in json_in["cells"]:
25 strip_output_from_cell(cell)
27 with open(tempfile, 'w') as fh:
28 json.dump(json_in, fh, sort_keys=True, indent=1, separators=(",",": "))
29 shutil.move(tempfile, infile)