-
Notifications
You must be signed in to change notification settings - Fork 54
/
run_notebooks.py
51 lines (38 loc) · 1.17 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division, print_function
import os
import re
import sys
import glob
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor, CellExecutionError
if len(sys.argv) >= 2:
pattern = sys.argv[1]
else:
pattern = "*.ipynb"
errors = []
for filename in glob.glob(pattern):
if filename.endswith("_exec.ipynb"):
continue
with open(filename) as f:
notebook = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(timeout=-1)
print("running: {0}".format(filename))
try:
ep.preprocess(notebook, {"metadata": {"path": "."}})
except CellExecutionError as e:
msg = "error while running: {0}\n\n".format(filename)
msg += e.traceback
print(msg)
errors.append(msg)
finally:
with open(os.path.splitext(filename)[0] + "_exec.ipynb",
mode="wt") as f:
nbformat.write(notebook, f)
txt = "\n\n".join(errors)
ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
txt = ansi_escape.sub("", txt)
with open("notebook_errors.log", "wb") as f:
f.write(txt.encode("utf-8"))
sys.exit(len(errors))