📚 The CoCalc Library - books, templates and other resources
License: OTHER
"""1This script copies all notebooks from the book into the website directory, and2creates pages which wrap them and link together.3"""4import os5import nbformat6import shutil78PAGEFILE = """title: {title}9url:10save_as: {htmlfile}11Template: {template}1213{{% notebook notebooks/{notebook_file} cells[{cells}] %}}14"""1516INTRO_TEXT = """This website contains the full text of 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) in the form of Jupyter notebooks.1718The 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).1920If you find this content useful, please consider supporting the work by [buying the book](http://shop.oreilly.com/product/0636920034919.do)!21"""222324def abspath_from_here(*args):25here = os.path.dirname(__file__)26path = os.path.join(here, *args)27return os.path.abspath(path)2829NB_SOURCE_DIR = abspath_from_here('..', 'notebooks')30NB_DEST_DIR = abspath_from_here('content', 'notebooks')31PAGE_DEST_DIR = abspath_from_here('content', 'pages')323334def copy_notebooks():35if not os.path.exists(NB_DEST_DIR):36os.makedirs(NB_DEST_DIR)37if not os.path.exists(PAGE_DEST_DIR):38os.makedirs(PAGE_DEST_DIR)3940nblist = sorted(nb for nb in os.listdir(NB_SOURCE_DIR)41if nb.endswith('.ipynb'))42name_map = {nb: nb.rsplit('.', 1)[0].lower() + '.html'43for nb in nblist}4445figsource = abspath_from_here('..', 'notebooks', 'figures')46figdest = abspath_from_here('content', 'figures')4748if os.path.exists(figdest):49shutil.rmtree(figdest)50shutil.copytree(figsource, figdest)5152figurelist = os.listdir(abspath_from_here('content', 'figures'))53figure_map = {os.path.join('figures', fig) : os.path.join('/PythonDataScienceHandbook/figures', fig)54for fig in figurelist}5556for nb in nblist:57base, ext = os.path.splitext(nb)58print('-', nb)5960content = nbformat.read(os.path.join(NB_SOURCE_DIR, nb),61as_version=4)6263if nb == 'Index.ipynb':64# content[0] is the title65# content[1] is the cover image66# content[2] is the license67cells = '1:'68template = 'page'69title = 'Python Data Science Handbook'70content.cells[2].source = INTRO_TEXT71else:72# content[0] is the book information73# content[1] is the navigation bar74# content[2] is the title75cells = '2:'76template = 'booksection'77title = content.cells[2].source78if not title.startswith('#') or len(title.splitlines()) > 1:79raise ValueError('title not found in third cell')80title = title.lstrip('#').strip()8182# put nav below title83content.cells.insert(0, content.cells.pop(2))8485# Replace internal URLs and figure links in notebook86for cell in content.cells:87if cell.cell_type == 'markdown':88for nbname, htmlname in name_map.items():89if nbname in cell.source:90cell.source = cell.source.replace(nbname, htmlname)91for figname, newfigname in figure_map.items():92if figname in cell.source:93cell.source = cell.source.replace(figname, newfigname)94if cell.source.startswith("<!--NAVIGATION-->"):95# Undo replacement of notebook link in the colab badge96cell.source = nb.join(cell.source.rsplit(name_map[nb], 1))9798nbformat.write(content, os.path.join(NB_DEST_DIR, nb))99100pagefile = os.path.join(PAGE_DEST_DIR, base + '.md')101htmlfile = base.lower() + '.html'102with open(pagefile, 'w') as f:103f.write(PAGEFILE.format(title=title,104htmlfile=htmlfile,105notebook_file=nb,106template=template,107cells=cells))108109if __name__ == '__main__':110copy_notebooks()111112113