Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit db6e9e8

Browse files
committed
copy figures as well
1 parent 12a9c39 commit db6e9e8

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

‎website/copy_notebooks.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
"""
55
import os
66
import nbformat
7+
import shutil
78

89
PAGEFILE = """title: {title}
910
slug: {slug}
1011
Template: page
1112
12-
{{% notebook notebooks/{notebook_file} cells[2:] %}}
13+
{{% notebook notebooks/{notebook_file} cells[{cells}] %}}
1314
"""
1415

1516

@@ -24,38 +25,58 @@ def abspath_from_here(*args):
2425

2526

2627
def copy_notebooks():
27-
nblist = sorted(os.listdir(NB_SOURCE_DIR))
28+
nblist = sorted(nb for nb in os.listdir(NB_SOURCE_DIR)
29+
if nb.endswith('.ipynb'))
2830
name_map = {nb: nb.rsplit('.', 1)[0] + '.html'
2931
for nb in nblist}
3032

33+
figsource = abspath_from_here('..', 'notebooks', 'figures')
34+
figdest = abspath_from_here('content', 'figures')
35+
36+
if os.path.exists(figdest):
37+
shutil.rmtree(figdest)
38+
shutil.copytree(figsource, figdest)
39+
40+
figurelist = os.listdir(abspath_from_here('content', 'figures'))
41+
figure_map = {os.path.join('figures', fig) : os.path.join('/figures', fig)
42+
for fig in figurelist}
43+
3144
for nb in nblist:
3245
base, ext = os.path.splitext(nb)
33-
if ext != '.ipynb':
34-
continue
3546
print('-', nb)
3647

3748
content = nbformat.read(os.path.join(NB_SOURCE_DIR, nb),
3849
as_version=4)
39-
title = content.cells[2].source
40-
if not title.startswith('#'):
41-
raise ValueError('title not found in third cell')
42-
title = title.lstrip('#').strip()
4350

44-
# put nav below title
45-
content.cells[1], content.cells[2] = content.cells[2], content.cells[1]
51+
if nb == 'Index.ipynb':
52+
cells = '1:'
53+
title = 'Python Data Science Handbook'
54+
else:
55+
cells = '2:'
56+
# put nav below title
57+
title = content.cells[2].source
58+
if not title.startswith('#') or len(title.splitlines()) > 1:
59+
raise ValueError('title not found in third cell')
60+
title = title.lstrip('#').strip()
61+
content.cells[1], content.cells[2] = content.cells[2], content.cells[1]
4662

4763
for cell in content.cells:
4864
if cell.cell_type == 'markdown':
4965
for nbname, htmlname in name_map.items():
5066
if nbname in cell.source:
5167
cell.source = cell.source.replace(nbname, htmlname)
68+
for figname, newfigname in figure_map.items():
69+
if figname in cell.source:
70+
cell.source = cell.source.replace(figname, newfigname)
71+
5272
nbformat.write(content, os.path.join(NB_DEST_DIR, nb))
5373

5474
pagefile = os.path.join(PAGE_DEST_DIR, base + '.md')
5575
with open(pagefile, 'w') as f:
5676
f.write(PAGEFILE.format(title=title,
5777
slug=base.lower(),
58-
notebook_file=nb))
78+
notebook_file=nb,
79+
cells=cells))
5980

6081
if __name__ == '__main__':
6182
copy_notebooks()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /