forked from dashamstyr/numeric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_notebooks.py
74 lines (69 loc) · 1.77 KB
/
convert_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import scandir
import os
import re
import subprocess
import pipes
notebook = re.compile('.*\.ipynb$')
#
# find all notebooks
#
targets = []
for subdir, dirs, files in scandir.walk('.'):
for the_file in files:
full_name = "{}/{}".format(subdir, the_file)
try:
if notebook.match(full_name) and not ('.ipynb_checkpoints' in full_name):
targets.append(os.path.abspath(full_name))
except FileNotFoundError:
pass
#
# convert to html
#
outdir = './html_notebooks'
if not os.path.exists(outdir):
os.makedirs(outdir)
try:
os.chdir(outdir)
for target in targets:
escaped=pipes.quote(target)
command='ipython nbconvert --to html {}'.format(escaped)
status,output=subprocess.getstatusoutput(command)
print(target,status,output)
except Exception as err:
print('trouble: {}'.format(err))
finally:
os.chdir('..')
#
# convert to latex
#
outdir = './pdf_notebooks'
if not os.path.exists(outdir):
os.makedirs(outdir)
try:
os.chdir(outdir)
for target in targets:
escaped=pipes.quote(target)
command='ipython nbconvert --to latex {}'.format(escaped)
status,output=subprocess.getstatusoutput(command)
print(target,status,output)
except Exception as err:
print('trouble: {}'.format(err))
finally:
os.chdir('..')
#
# copy to single directory
#
outdir = './transfer_notebooks'
if not os.path.exists(outdir):
os.makedirs(outdir)
try:
os.chdir(outdir)
for target in targets:
escaped=pipes.quote(target)
command='cp -af {} .'.format(escaped)
status,output=subprocess.getstatusoutput(command)
print(target,status,output)
except Exception as err:
print('trouble: {}'.format(err))
finally:
os.chdir('..')