Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

Python Data Science Handbook

Views: 90270
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 consider supporting 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()
37
38