forked from chengsoonong/didbits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipynbhelper.py
238 lines (204 loc) · 6.99 KB
/
ipynbhelper.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
"""Utility script to be used to cleanup the notebooks before git commit
This a mix from @minrk's various gists.
Copied from Olivier Griesel's parallel ipython tutorial:
https://github.com/ogrisel/parallel_ml_tutorial
"""
import time
import sys
import os
import io
import argparse
try:
from queue import Empty
except:
# Python 2 backward compat
from Queue import Empty
from IPython.nbformat import current
from IPython.kernel import KernelManager
from IPython.parallel import Client
try:
FileNotFoundError
except NameError:
FileNotFoundError = IOError
assert KernelManager # to silence pyflakes
def remove_outputs(nb):
"""Remove the outputs from a notebook"""
for ws in nb.worksheets:
for cell in ws.cells:
if cell.cell_type == 'code':
cell.outputs = []
if 'prompt_number' in cell:
del cell['prompt_number']
def remove_signature(nb):
"""Remove the signature from a notebook"""
if 'signature' in nb.metadata:
del nb.metadata['signature']
def run_cell(shell, iopub, cell, timeout=300):
if not hasattr(cell, 'input'):
return [], False
shell.execute(cell.input)
# wait for finish, maximum 5min by default
reply = shell.get_msg(timeout=timeout)['content']
if reply['status'] == 'error':
failed = True
print("\nFAILURE:")
print(cell.input)
print('-----')
print("raised:")
print('\n'.join(reply['traceback']))
else:
failed = False
# Collect the outputs of the cell execution
outs = []
while True:
try:
msg = iopub.get_msg(timeout=0.2)
except Empty:
break
msg_type = msg['msg_type']
if msg_type in ('status', 'pyin'):
continue
elif msg_type == 'clear_output':
outs = []
continue
content = msg['content']
out = current.NotebookNode(output_type=msg_type)
if msg_type == 'stream':
out.stream = content['name']
out.text = content['data']
elif msg_type in ('display_data', 'pyout'):
for mime, data in content['data'].items():
attr = mime.split('/')[-1].lower()
# this gets most right, but fix svg+html, plain
attr = attr.replace('+xml', '').replace('plain', 'text')
setattr(out, attr, data)
if msg_type == 'pyout':
out.prompt_number = content['execution_count']
elif msg_type == 'pyerr':
out.ename = content['ename']
out.evalue = content['evalue']
out.traceback = content['traceback']
else:
print("unhandled iopub msg: %s" % msg_type)
outs.append(out)
# Special handling of ipcluster restarts
if '!ipcluster stop' in cell.input:
# wait some time for cluster commands to complete
for i in range(10):
try:
if len(Client()) == 0:
break
except FileNotFoundError:
pass
sys.stdout.write("@"); sys.stdout.flush()
time.sleep(5)
if '!ipcluster start' in cell.input:
# wait some time for cluster commands to complete
for i in range(10):
try:
if len(Client()) > 0:
break
except FileNotFoundError:
pass
sys.stdout.write("#"); sys.stdout.flush()
time.sleep(5)
return outs, failed
def run_notebook(nb):
km = KernelManager()
km.start_kernel(stderr=open(os.devnull, 'w'))
if hasattr(km, 'client'):
kc = km.client()
kc.start_channels()
iopub = kc.iopub_channel
else:
# IPython 0.13 compat
kc = km
kc.start_channels()
iopub = kc.sub_channel
shell = kc.shell_channel
# simple ping:
shell.execute("pass")
shell.get_msg()
cells = 0
failures = 0
for ws in nb.worksheets:
for cell in ws.cells:
if cell.cell_type != 'code':
continue
outputs, failed = run_cell(shell, iopub, cell)
cell.outputs = outputs
cell['prompt_number'] = cells
failures += failed
cells += 1
sys.stdout.write('.')
sys.stdout.flush()
print()
print("ran notebook %s" % nb.metadata.name)
print(" ran %3i cells" % cells)
if failures:
print(" %3i cells raised exceptions" % failures)
kc.stop_channels()
km.shutdown_kernel()
del km
def process_notebook_file(fname, action='clean', output_fname=None):
print("Performing '{}' on: {}".format(action, fname))
orig_wd = os.getcwd()
with io.open(fname, 'r') as f:
nb = current.read(f, 'json')
if action == 'check':
os.chdir(os.path.dirname(fname))
run_notebook(nb)
remove_outputs(nb)
remove_signature(nb)
elif action == 'render':
os.chdir(os.path.dirname(fname))
run_notebook(nb)
else:
# Clean by default
remove_outputs(nb)
remove_signature(nb)
os.chdir(orig_wd)
if output_fname is None:
output_fname = fname
with io.open(output_fname, 'w') as f:
nb = current.write(nb, f, 'json')
def take_action(action, targets):
rendered_folder = os.path.join(os.path.dirname(__file__),
'rendered_notebooks')
if not os.path.exists(rendered_folder):
os.makedirs(rendered_folder)
if not targets:
targets = [os.path.join(os.path.dirname(__file__), 'notebooks')]
for target in targets:
if os.path.isdir(target):
fnames = [os.path.abspath(os.path.join(target, f))
for f in os.listdir(target)
if f.endswith('.ipynb')]
else:
fnames = [target]
for fname in fnames:
if action == 'render':
output_fname = os.path.join(rendered_folder,
os.path.basename(fname))
else:
output_fname = fname
process_notebook_file(fname, action=action,
output_fname=output_fname)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('targets', help='List of directories to apply action to',
action='append')
parser.add_argument('-c', '--clean', help='Clean up generated files',
action='store_true')
parser.add_argument('-d', '--debug', help='Check that notebooks are ok',
action='store_true')
parser.add_argument('-r', '--render', help='Render notebooks to directory render_notebooks',
action='store_true')
args = parser.parse_args()
targets = [t for t in args.targets]
if args.render:
take_action('render', targets)
if args.debug:
take_action('check', targets)
if args.clean:
take_action('clean', targets)