Skip to content

Commit 10f7882

Browse files
committed
create tool for adding book info
1 parent c1f16bb commit 10f7882

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

tools/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ These are tools for managing the notebooks in this repository.
44

55
- ``generate_contents.py``: this will generate a markdown table of contents for use in the README and in the Index.ipynb notebook
66

7-
- ``add_navigation.py``: this script adds navigation links at the top and bottom of each notebook.
7+
- ``add_navigation.py``: this script adds navigation links at the top and bottom of each notebook.
8+
9+
- ``add_book_info.py``: this script adds book information to the top of each notebook.

tools/add_book_info.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
3+
import nbformat
4+
from nbformat.v4.nbbase import new_markdown_cell
5+
6+
from generate_contents import iter_notebooks, NOTEBOOK_DIR
7+
8+
9+
BOOK_COMMENT = "<!--BOOK_INFORMATION-->"
10+
11+
12+
BOOK_INFO = BOOK_COMMENT + """
13+
<img align="left" style="padding-right:10px;" src="figures/PDSH-cover-small.png">
14+
*This notebook contains an excerpt from 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).*
15+
16+
*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). If you find this content useful, please support the work by [buying the book](http://shop.oreilly.com/product/0636920034919.do)!*"""
17+
18+
19+
def add_book_info():
20+
for nb_name in iter_notebooks():
21+
nb_file = os.path.join(NOTEBOOK_DIR, nb_name)
22+
nb = nbformat.read(nb_file, as_version=4)
23+
24+
is_comment = lambda cell: cell.source.startswith(BOOK_COMMENT)
25+
26+
if is_comment(nb.cells[0]):
27+
print('- amending comment for {0}'.format(nb_name))
28+
nb.cells[0].source = BOOK_INFO
29+
else:
30+
print('- inserting comment for {0}'.format(nb_name))
31+
nb.cells.insert(0, new_markdown_cell(BOOK_INFO))
32+
nbformat.write(nb, nb_file)
33+
34+
35+
if __name__ == '__main__':
36+
add_book_info()

0 commit comments

Comments
 (0)