-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_notebook.py
executable file
·34 lines (26 loc) · 998 Bytes
/
run_notebook.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
#!/usr/bin/env python
import os
import sys
import os
import subprocess
import tempfile
import nbformat
kernel_name = os.environ.get("JUPYTER_KERNEL_NAME", "vor")
def _notebook_run(path):
"""Execute a notebook via nbconvert and collect output.
:returns (parsed nb object, execution errors)
"""
with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
args = ["jupyter", "nbconvert", "--to", "notebook", "--execute",
"--ExecutePreprocessor.timeout=-1",
"--ExecutePreprocessor.kernel_name={}".format(kernel_name),
"--output", fout.name, path]
subprocess.check_call(args)
fout.seek(0)
nb = nbformat.read(fout, nbformat.current_nbformat)
errors = [output for cell in nb.cells if "outputs" in cell
for output in cell["outputs"]
if output.output_type == "error"]
return nb, errors
if __name__ == '__main__':
_notebook_run('./analysis-ca-covid19-v1.ipynb')