File: strip_notebook.py

package info (click to toggle)
gromacs 2026~rc-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 274,216 kB
  • sloc: xml: 3,831,143; cpp: 686,111; ansic: 75,300; python: 21,171; sh: 3,553; perl: 2,246; yacc: 644; fortran: 397; lisp: 265; makefile: 174; lex: 125; awk: 68; csh: 39
file content (32 lines) | stat: -rwxr-xr-x 822 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python
# This script takes an ipython notebook as an argument and rewrites the file
# with metadata stripped to make change tracking with git easier.
import sys
import json
import os
import shutil

infile = sys.argv[1]
if not os.path.exists(infile):
    sys.exit("command line argument must be an existing filename")
tempfile = infile + ".tmp"

with open(infile, "r") as fh:
    json_in = json.load(fh)

nb_metadata = json_in["metadata"]


def strip_output_from_cell(cell):
    if "outputs" in cell:
        cell["outputs"] = []
    if "execution_count" in cell:
        cell["execution_count"] = None


for cell in json_in["cells"]:
    strip_output_from_cell(cell)

with open(tempfile, "w") as fh:
    json.dump(json_in, fh, sort_keys=True, indent=1, separators=(",", ": "))
shutil.move(tempfile, infile)