-
Notifications
You must be signed in to change notification settings - Fork 18.2k
/
Copy pathcopy_notebooks.py
63 lines (48 loc) · 1.87 KB
/
copy_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
"""
This script copies all notebooks from the book into the website directory, and
creates pages which wrap them and link together.
"""
import os
import nbformat
PAGEFILE = """title: {title}
slug: {slug}
Template: page
{{% notebook notebooks/{notebook_file} cells[2:] %}}
"""
def abspath_from_here(*args):
here = os.path.dirname(__file__)
path = os.path.join(here, *args)
return os.path.abspath(path)
NB_SOURCE_DIR = abspath_from_here('..', 'notebooks')
NB_DEST_DIR = abspath_from_here('content', 'notebooks')
PAGE_DEST_DIR = abspath_from_here('content', 'pages')
def copy_notebooks():
nblist = sorted(os.listdir(NB_SOURCE_DIR))
name_map = {nb: nb.rsplit('.', 1)[0] + '.html'
for nb in nblist}
for nb in nblist:
base, ext = os.path.splitext(nb)
if ext != '.ipynb':
continue
print('-', nb)
content = nbformat.read(os.path.join(NB_SOURCE_DIR, nb),
as_version=4)
title = content.cells[2].source
if not title.startswith('#'):
raise ValueError('title not found in third cell')
title = title.lstrip('#').strip()
# put nav below title
content.cells[1], content.cells[2] = content.cells[2], content.cells[1]
for cell in content.cells:
if cell.cell_type == 'markdown':
for nbname, htmlname in name_map.items():
if nbname in cell.source:
cell.source = cell.source.replace(nbname, htmlname)
nbformat.write(content, os.path.join(NB_DEST_DIR, nb))
pagefile = os.path.join(PAGE_DEST_DIR, base + '.md')
with open(pagefile, 'w') as f:
f.write(PAGEFILE.format(title=title,
slug=base.lower(),
notebook_file=nb))
if __name__ == '__main__':
copy_notebooks()