forked from jakevdp/PythonDataScienceHandbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
39 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import os | ||
|
||
import nbformat | ||
from nbformat.v4.nbbase import new_markdown_cell | ||
|
||
from generate_contents import iter_notebooks, NOTEBOOK_DIR | ||
|
||
|
||
BOOK_COMMENT = "<!--BOOK_INFORMATION-->" | ||
|
||
|
||
BOOK_INFO = BOOK_COMMENT + """ | ||
<img align="left" style="padding-right:10px;" src="figures/PDSH-cover-small.png"> | ||
*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).* | ||
*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)!*""" | ||
|
||
|
||
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() |