/
add_book_info.py
45 lines (28 loc) · 1.51 KB
/
add_book_info.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
import os
import re
import nbformat
from nbformat.v4.nbbase import new_markdown_cell
BOOK_COMMENT = "<!--BOOK_INFORMATION-->"
BOOK_INFO = BOOK_COMMENT + """
<img align="left" style="padding-right:10px;" src="fig/cover-small.jpg">
*This notebook contains an excerpt from the [Whirlwind Tour of Python](http://www.oreilly.com/programming/free/a-whirlwind-tour-of-python.csp) by Jake VanderPlas; the content is available [on GitHub](https://github.com/jakevdp/WhirlwindTourOfPython).*
*The text and code are released under the [CC0](https://github.com/jakevdp/WhirlwindTourOfPython/blob/master/LICENSE) license; see also the companion project, the [Python Data Science Handbook](https://github.com/jakevdp/PythonDataScienceHandbook).*
"""
NOTEBOOK_DIR = os.path.join(os.path.dirname(__file__), '..')
REG = re.compile(r'(\d\d)-(.*)\.ipynb')
def iter_notebooks():
return sorted(nb for nb in os.listdir(NOTEBOOK_DIR) if REG.match(nb))
def add_book_info():
for nb_name in iter_notebooks():
nb_file = os.path.join(NOTEBOOK_DIR, nb_name)
nb = nbformat.read(nb_file, as_version=4)
is_comment = lambda cell: cell.source.startswith(BOOK_COMMENT)
if is_comment(nb.cells[0]):
print('- amending comment for {0}'.format(nb_name))
nb.cells[0].source = BOOK_INFO
else:
print('- inserting comment for {0}'.format(nb_name))
nb.cells.insert(0, new_markdown_cell(BOOK_INFO))
nbformat.write(nb, nb_file)
if __name__ == '__main__':
add_book_info()