forked from meatballs/sql_python_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
118 lines (92 loc) · 3.19 KB
/
Copy pathmain.py
File metadata and controls
118 lines (92 loc) · 3.19 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import pathlib
import tqdm
import collections
import jinja2
from nbconvert import HTMLExporter
ROOT = 'sql_python_tutorial'
def get_id(path):
"""
Return the id of a file
"""
stem = path.stem
try:
return stem[:stem.index('-')]
except ValueError:
stem = stem.lower()
stem = stem.replace(" ", "-")
stem = stem.replace(",", "")
return stem
def get_name(path):
"""
Return the name of a file
"""
stem = path.stem
try:
return stem[stem.index('-'):].replace('-', ' ')
except ValueError:
return stem
def convert_html(nb_path):
"""
Convert a notebook to html
"""
html_exporter = HTMLExporter()
html_exporter.template_file = "basic"
return html_exporter.from_file(str(nb_path))
def render_template(template_file, template_vars):
"""
Render a jinja2 template
"""
templateLoader = jinja2.FileSystemLoader(searchpath="./templates/")
template_env = jinja2.Environment(loader=templateLoader)
template = template_env.get_template(template_file)
return template.render(template_vars)
def make_dir(path, directory, previous_url=None, next_url=None):
"""
Create a directory for the name of the file
"""
path_id = get_id(path)
p = pathlib.Path(f"./{directory}/{path_id}")
p.mkdir(exist_ok=True)
nb, _ = convert_html(path)
nb = nb.replace("{{root}}", ROOT)
html = render_template("notebook.html", {"nb": nb,
"root": ROOT,
"id": path_id,
"previous_url": previous_url,
"next_url": next_url})
(p / 'index.html').write_text(html)
def make_collection(paths, directory,
make_previous_url=True,
make_next_url=True):
number_of_paths = len(paths)
for index, filename in enumerate(paths):
previous_path = paths[(index - 1) % number_of_paths]
previous_id = get_id(previous_path)
next_path = paths[(index + 1) % number_of_paths]
next_id = get_id(next_path)
make_dir(pathlib.Path(filename), directory=directory,
previous_url=previous_id,
next_url=next_id)
Chapter = collections.namedtuple("chapter", ["dir", "title", "nb"])
if __name__ == "__main__":
nb_dir = pathlib.Path('notebooks')
chapter_paths = sorted(nb_dir.glob('./*ipynb'))
for paths, directory in [(chapter_paths, "chapters")]:
make_collection(paths=paths, directory=directory)
chapters = []
for path in tqdm.tqdm(sorted(chapter_paths)):
chapters.append(Chapter(f"{get_id(path)}",
get_name(path), str(path)))
pages = [
{'template': 'home.html', 'output': 'index.html'},
{'template': 'intro.html', 'output': 'pages/intro.html'},
{'template': 'howto.html', 'output': 'pages/howto.html'},
{'template': 'primer.html', 'output': 'pages/primer.html'}]
for page in pages:
html = render_template(f'pages/{page["template"]}', {'root': ROOT})
with open(page['output'], 'w') as f:
f.write(html)
html = render_template(
"chapters.html", {"chapters": chapters, "root": ROOT})
with open('./chapters/index.html', 'w') as f:
f.write(html)