forked from dgerlanc/programming-with-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
executable file
·74 lines (52 loc) · 1.79 KB
/
build.py
File metadata and controls
executable file
·74 lines (52 loc) · 1.79 KB
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
#!/usr/bin/env python3
import logging
import subprocess
from pathlib import Path
logger = logging.getLogger(__name__)
# TODO: Only update files changed since last run
# TODO: Add conversion tool from existing answer notebooks
def answer2exercise(infile, outfile):
"""
Convert answer notebooks to exercise notebooks
TODO: Fail if output notebook is empty?
"""
cmd = 'jupyter nbconvert --config config.py --to notebook --output'.split()
cmd.extend([outfile, infile])
subprocess.run(cmd)
def slide2html(infile):
"""
Convert slide notebooks to reveal.js
"""
cmd = ('jupyter nbconvert'
' --to slides'
' --reveal-prefix=reveal.js'
' --SlidesExporter.file_extension=.html'
' --output-dir build').split()
cmd.append(str(infile))
subprocess.run(cmd)
def run_slide(infile):
cmd = 'jupyter nbconvert --to notebook --inplace --execute'.split()
cmd.append(str(infile))
devnull = subprocess.DEVNULL
subprocess.run(cmd, check=True, stdout=devnull, stderr=devnull)
def main():
p = Path('.')
# TODO: add test for errors
slide_fns = p.glob('*slides.ipynb')
for slide_fn in sorted(slide_fns):
print(f'Running {slide_fn}')
run_slide(slide_fn)
print('ipynb slides -> reveal.js html')
slide_fns = p.glob('*slides.ipynb')
for slide_fn in sorted(slide_fns):
slide2html(slide_fn)
print('Convert answers to exercises')
answers = p.glob('*answers.ipynb')
for answer_nb in sorted(answers):
exercise_nb = str(answer_nb).replace('answer', 'exercise')
# print(f'{answer_nb} -> {exercise_nb}')
answer2exercise(str(answer_nb), exercise_nb)
# copy over assets
# html slides -> pdf
if __name__ == '__main__':
main()