Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

Python Data Science Handbook

Views: 90271
1
import os
2
import re
3
import itertools
4
import nbformat
5
6
NOTEBOOK_DIR = os.path.join(os.path.dirname(__file__), '..', 'notebooks')
7
8
CHAPTERS = {"00": "Preface",
9
"01": "IPython: Beyond Normal Python",
10
"02": "NumPy",
11
"03": "Pandas",
12
"04": "Matplotlib",
13
"05": "Machine Learning"}
14
15
REG = re.compile(r'(\d\d)\.(\d\d)-(.*)\.ipynb')
16
17
18
def iter_notebooks():
19
return sorted(nb for nb in os.listdir(NOTEBOOK_DIR) if REG.match(nb))
20
21
22
def get_notebook_title(nb_file):
23
nb = nbformat.read(os.path.join(NOTEBOOK_DIR, nb_file), as_version=4)
24
for cell in nb.cells:
25
if cell.source.startswith('#'):
26
return cell.source[1:].splitlines()[0].strip()
27
28
29
def gen_contents(directory=None):
30
for nb in iter_notebooks():
31
if directory:
32
nb_url = os.path.join(directory, nb)
33
else:
34
nb_url = nb
35
chapter, section, title = REG.match(nb).groups()
36
title = get_notebook_title(nb)
37
if section == '00':
38
if chapter in ['00', '06']:
39
yield '\n### [{0}]({1})'.format(title, nb_url)
40
else:
41
yield '\n### [{0}. {1}]({2})'.format(int(chapter),
42
title, nb_url)
43
else:
44
yield "- [{0}]({1})".format(title, nb_url)
45
46
47
def print_contents(directory=None):
48
print('\n'.join(gen_contents(directory)))
49
50
51
if __name__ == '__main__':
52
print_contents()
53
print('\n', 70 * '#', '\n')
54
print_contents('http://nbviewer.jupyter.org/github/jakevdp/PythonDataScienceHandbook/blob/master/notebooks/')
55
56