Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132928 views
License: OTHER
1
"""
2
This script copies all notebooks from the book into the website directory, and
3
creates pages which wrap them and link together.
4
"""
5
import os
6
import nbformat
7
import shutil
8
9
PAGEFILE = """title: {title}
10
url:
11
save_as: {htmlfile}
12
Template: {template}
13
14
{{% notebook notebooks/{notebook_file} cells[{cells}] %}}
15
"""
16
17
INTRO_TEXT = """This website contains the full text of the [Python Data Science Handbook](http://shop.oreilly.com/product/0636920034919.do) by Jake VanderPlas; the content is available [on GitHub](https://github.com/jakevdp/PythonDataScienceHandbook) in the form of Jupyter notebooks.
18
19
The text is released under the [CC-BY-NC-ND license](https://creativecommons.org/licenses/by-nc-nd/3.0/us/legalcode), and code is released under the [MIT license](https://opensource.org/licenses/MIT).
20
21
If you find this content useful, please consider supporting the work by [buying the book](http://shop.oreilly.com/product/0636920034919.do)!
22
"""
23
24
25
def abspath_from_here(*args):
26
here = os.path.dirname(__file__)
27
path = os.path.join(here, *args)
28
return os.path.abspath(path)
29
30
NB_SOURCE_DIR = abspath_from_here('..', 'notebooks')
31
NB_DEST_DIR = abspath_from_here('content', 'notebooks')
32
PAGE_DEST_DIR = abspath_from_here('content', 'pages')
33
34
35
def copy_notebooks():
36
if not os.path.exists(NB_DEST_DIR):
37
os.makedirs(NB_DEST_DIR)
38
if not os.path.exists(PAGE_DEST_DIR):
39
os.makedirs(PAGE_DEST_DIR)
40
41
nblist = sorted(nb for nb in os.listdir(NB_SOURCE_DIR)
42
if nb.endswith('.ipynb'))
43
name_map = {nb: nb.rsplit('.', 1)[0].lower() + '.html'
44
for nb in nblist}
45
46
figsource = abspath_from_here('..', 'notebooks', 'figures')
47
figdest = abspath_from_here('content', 'figures')
48
49
if os.path.exists(figdest):
50
shutil.rmtree(figdest)
51
shutil.copytree(figsource, figdest)
52
53
figurelist = os.listdir(abspath_from_here('content', 'figures'))
54
figure_map = {os.path.join('figures', fig) : os.path.join('/PythonDataScienceHandbook/figures', fig)
55
for fig in figurelist}
56
57
for nb in nblist:
58
base, ext = os.path.splitext(nb)
59
print('-', nb)
60
61
content = nbformat.read(os.path.join(NB_SOURCE_DIR, nb),
62
as_version=4)
63
64
if nb == 'Index.ipynb':
65
# content[0] is the title
66
# content[1] is the cover image
67
# content[2] is the license
68
cells = '1:'
69
template = 'page'
70
title = 'Python Data Science Handbook'
71
content.cells[2].source = INTRO_TEXT
72
else:
73
# content[0] is the book information
74
# content[1] is the navigation bar
75
# content[2] is the title
76
cells = '2:'
77
template = 'booksection'
78
title = content.cells[2].source
79
if not title.startswith('#') or len(title.splitlines()) > 1:
80
raise ValueError('title not found in third cell')
81
title = title.lstrip('#').strip()
82
83
# put nav below title
84
content.cells.insert(0, content.cells.pop(2))
85
86
# Replace internal URLs and figure links in notebook
87
for cell in content.cells:
88
if cell.cell_type == 'markdown':
89
for nbname, htmlname in name_map.items():
90
if nbname in cell.source:
91
cell.source = cell.source.replace(nbname, htmlname)
92
for figname, newfigname in figure_map.items():
93
if figname in cell.source:
94
cell.source = cell.source.replace(figname, newfigname)
95
if cell.source.startswith("<!--NAVIGATION-->"):
96
# Undo replacement of notebook link in the colab badge
97
cell.source = nb.join(cell.source.rsplit(name_map[nb], 1))
98
99
nbformat.write(content, os.path.join(NB_DEST_DIR, nb))
100
101
pagefile = os.path.join(PAGE_DEST_DIR, base + '.md')
102
htmlfile = base.lower() + '.html'
103
with open(pagefile, 'w') as f:
104
f.write(PAGEFILE.format(title=title,
105
htmlfile=htmlfile,
106
notebook_file=nb,
107
template=template,
108
cells=cells))
109
110
if __name__ == '__main__':
111
copy_notebooks()
112
113