Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
Python Data Science Handbook
Project: Python Data Science Handbook
Views: 92281import os1import itertools23from ipykernel import kernelspec as ks4import nbformat5from nbformat.v4.nbbase import new_markdown_cell67from generate_contents import NOTEBOOK_DIR, REG, iter_notebooks, get_notebook_title8910def prev_this_next(it):11a, b, c = itertools.tee(it,3)12next(c)13return zip(itertools.chain([None], a), b, itertools.chain(c, [None]))141516PREV_TEMPLATE = "< [{title}]({url}) "17CONTENTS = "| [Contents](Index.ipynb) |"18NEXT_TEMPLATE = " [{title}]({url}) >"19NAV_COMMENT = "<!--NAVIGATION-->\n"202122def iter_navbars():23for prev_nb, nb, next_nb in prev_this_next(iter_notebooks()):24navbar = NAV_COMMENT25if prev_nb:26navbar += PREV_TEMPLATE.format(title=get_notebook_title(prev_nb),27url=prev_nb)28navbar += CONTENTS29if next_nb:30navbar += NEXT_TEMPLATE.format(title=get_notebook_title(next_nb),31url=next_nb)32yield os.path.join(NOTEBOOK_DIR, nb), navbar333435def write_navbars():36for nb_name, navbar in iter_navbars():37nb = nbformat.read(nb_name, as_version=4)38nb_file = os.path.basename(nb_name)39is_comment = lambda cell: cell.source.startswith(NAV_COMMENT)4041if is_comment(nb.cells[1]):42print("- amending navbar for {0}".format(nb_file))43nb.cells[1].source = navbar44else:45print("- inserting navbar for {0}".format(nb_file))46nb.cells.insert(1, new_markdown_cell(source=navbar))4748if is_comment(nb.cells[-1]):49nb.cells[-1].source = navbar50else:51nb.cells.append(new_markdown_cell(source=navbar))52nbformat.write(nb, nb_name)535455if __name__ == '__main__':56write_navbars()575859