-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_nb.py
45 lines (33 loc) · 1.19 KB
/
test_nb.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
import os
import subprocess
import tempfile
def _exec_notebook(path):
with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
args = ["jupyter", "nbconvert", "--to", "notebook", "--execute",
"--ExecutePreprocessor.timeout=1000",
"--output", fout.name, path]
subprocess.check_call(args)
def get_temp_file_name():
ftemp = tempfile.NamedTemporaryFile(suffix=".ipynb")
temp_file_name = os.path.join(os.getcwd(), os.path.split(ftemp.name)[-1])
ftemp.close()
return temp_file_name
def _exec_notebook_win(path):
temp_file_name = get_temp_file_name()
args = ["jupyter", "nbconvert", "--to", "notebook", "--execute",
"--ExecutePreprocessor.timeout=1000",
"--ExecutePreprocessor.kernel_name=python",
"--output", temp_file_name, path]
try:
subprocess.check_call(args)
except BaseException as e:
if os.path.exists(temp_file_name):
os.remove(temp_file_name)
raise e
if os.path.exists(temp_file_name):
os.remove(temp_file_name)
def test():
if 'nt' == os.name:
_exec_notebook_win('test.ipynb')
else:
_exec_notebook('test.ipynb')