forked from cgpotts/cs224u
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotebook_tester.py
51 lines (44 loc) · 1.46 KB
/
notebook_tester.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
import click
import glob
import os
import subprocess
__author__ = "Christopher Potts"
__version__ = "CS224u, Stanford, Fall 2020"
TEMP_PREFIX = "TEMP_"
@click.command()
@click.argument("filename_or_dirname")
def main(filename_or_dirname):
if filename_or_dirname.endswith(".ipynb"):
filenames = [filename_or_dirname]
else:
filenames = glob.glob(filename_or_dirname + "*.ipynb")
for filename in filenames:
path, basename = os.path.split(filename)
output_filename = TEMP_PREFIX + basename
output_filename = os.path.join(path, output_filename)
cmd = [
'jupyter', 'nbconvert',
'--to', 'python',
filename,
'--stdout']
proc = subprocess.run(cmd, stdout=subprocess.PIPE)
b = proc.stdout
contents = b.decode('utf8')
contents = contents.replace("get_ipython()", "# get_ipython()")
with open(output_filename, "wt") as f:
f.write(contents)
print("Running {}".format(output_filename))
try:
subprocess.run(
['python', output_filename],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True)
except subprocess.CalledProcessError as err:
print(err)
else:
print("Completed {} with no errors".format(output_filename))
finally:
os.remove(output_filename)
if __name__ == '__main__':
main()