-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_notebooks.py
54 lines (40 loc) · 1.44 KB
/
run_notebooks.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
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor, CellExecutionError
"""
This script requires nbformat and nbconvert >5.x
"""
def execute(nb_in_fn, kernel_name='python3', run_path='.'):
nb_out_fn = nb_in_fn.replace('.ipynb', '.nbconvert.ipynb')
with open(nb_in_fn) as f:
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(timeout=600, kernel_name=kernel_name)
failed = False
try:
out = ep.preprocess(nb, {'metadata': {'path': run_path}})
except CellExecutionError as e:
out = None
print('Error executing the notebook "{}". Traceback:'.format(nb_in_fn))
print(e.traceback)
failed = True
finally:
with open(nb_out_fn, mode='wt') as f:
nbformat.write(nb, f)
return not failed
if __name__ == '__main__':
import os
import sys
from glob import glob
kernel_name = 'python3'
nbfns = glob('*/*.ipynb')
nbfns = [fn for fn in nbfns if not fn.endswith('.nbconvert.ipynb')]
print("Running the following notebooks:", nbfns)
succeeded = True
for nbfn in nbfns:
nb_dir = os.path.abspath(os.path.join('.', os.path.split(nbfn)[0]))
succeeded = succeeded and execute(nbfn, kernel_name, nb_dir)
if succeeded:
print('All the notebooks succeeded!')
sys.exit(0)
else:
print('Some of the notebooks failed in execution. Scroll up to see details.')
sys.exit(1)