Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
| Download
Python Data Science Handbook
Project: Python Data Science Handbook
Views: 94668"""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():35nblist = sorted(nb for nb in os.listdir(NB_SOURCE_DIR)36if nb.endswith('.ipynb'))37name_map = {nb: nb.rsplit('.', 1)[0].lower() + '.html'38for nb in nblist}3940figsource = abspath_from_here('..', 'notebooks', 'figures')41figdest = abspath_from_here('content', 'figures')4243if os.path.exists(figdest):44shutil.rmtree(figdest)45shutil.copytree(figsource, figdest)4647figurelist = os.listdir(abspath_from_here('content', 'figures'))48figure_map = {os.path.join('figures', fig) : os.path.join('/PythonDataScienceHandbook/figures', fig)49for fig in figurelist}5051for nb in nblist:52base, ext = os.path.splitext(nb)53print('-', nb)5455content = nbformat.read(os.path.join(NB_SOURCE_DIR, nb),56as_version=4)5758if nb == 'Index.ipynb':59cells = '1:'60template = 'page'61title = 'Python Data Science Handbook'62content.cells[2].source = INTRO_TEXT63else:64cells = '2:'65template = 'booksection'66title = content.cells[2].source67if not title.startswith('#') or len(title.splitlines()) > 1:68raise ValueError('title not found in third cell')69title = title.lstrip('#').strip()7071# put nav below title72content.cells[0], content.cells[1], content.cells[2] = content.cells[2], content.cells[0], content.cells[1]7374# Replace internal URLs and figure links in notebook75for cell in content.cells:76if cell.cell_type == 'markdown':77for nbname, htmlname in name_map.items():78if nbname in cell.source:79cell.source = cell.source.replace(nbname, htmlname)80for figname, newfigname in figure_map.items():81if figname in cell.source:82cell.source = cell.source.replace(figname, newfigname)8384nbformat.write(content, os.path.join(NB_DEST_DIR, nb))8586pagefile = os.path.join(PAGE_DEST_DIR, base + '.md')87htmlfile = base.lower() + '.html'88with open(pagefile, 'w') as f:89f.write(PAGEFILE.format(title=title,90htmlfile=htmlfile,91notebook_file=nb,92template=template,93cells=cells))9495if __name__ == '__main__':96copy_notebooks()979899100101