-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo-ready-ipynb
executable file
·60 lines (46 loc) · 1.62 KB
/
demo-ready-ipynb
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#! /usr/bin/env python3
from json import load, dump
from argparse import ArgumentParser
from warnings import warn
warn(
"This program is deprecated and only kept for compatibility. "
"You should use the newer prepare-ipynb instead "
"(and convert your existing notebooks to it).",
DeprecationWarning)
parser = ArgumentParser()
parser.add_argument(
"-k", "--keep", action="store_true",
help="Keep everything, just delete '# keep' comments")
parser.add_argument("infile", metavar="INFILE.ipynb")
parser.add_argument("outfile", metavar="OUTFILE.ipynb")
args = parser.parse_args()
with open(args.infile, "rt") as inf:
ipynb = load(inf)
for ws in ipynb["worksheets"]:
for cell in ws["cells"]:
if cell["cell_type"] == "code":
do_keep = False
new_inputs = []
inputs = None
if inputs is None and "inputs" in cell:
inputs = cell["inputs"]
if inputs is None and "input" in cell:
inputs = cell["input"]
for input_line in inputs:
if "#keep" in input_line:
do_keep = True
else:
new_inputs.append(input_line)
if "inputs" in cell:
del cell["inputs"]
if args.keep:
cell["input"] = new_inputs
else:
if do_keep:
cell["input"] = new_inputs
else:
cell["input"] = []
cell["outputs"] = []
cell.pop("prompt_number", None)
with open(args.outfile, "wt") as outf:
dump(ipynb, outf)