📚 The CoCalc Library - books, templates and other resources
License: OTHER
import os1import re2import itertools3import nbformat45NOTEBOOK_DIR = os.path.join(os.path.dirname(__file__), '..', 'notebooks')67CHAPTERS = {"00": "Preface",8"01": "IPython: Beyond Normal Python",9"02": "NumPy",10"03": "Pandas",11"04": "Matplotlib",12"05": "Machine Learning"}1314REG = re.compile(r'(\d\d)\.(\d\d)-(.*)\.ipynb')151617def iter_notebooks():18return sorted(nb for nb in os.listdir(NOTEBOOK_DIR) if REG.match(nb))192021def get_notebook_title(nb_file):22nb = nbformat.read(os.path.join(NOTEBOOK_DIR, nb_file), as_version=4)23for cell in nb.cells:24if cell.source.startswith('#'):25return cell.source[1:].splitlines()[0].strip()262728def gen_contents(directory=None):29for nb in iter_notebooks():30if directory:31nb_url = os.path.join(directory, nb)32else:33nb_url = nb34chapter, section, title = REG.match(nb).groups()35title = get_notebook_title(nb)36if section == '00':37if chapter in ['00', '06']:38yield '\n### [{0}]({1})'.format(title, nb_url)39else:40yield '\n### [{0}. {1}]({2})'.format(int(chapter),41title, nb_url)42else:43yield "- [{0}]({1})".format(title, nb_url)444546def print_contents(directory=None):47print('\n'.join(gen_contents(directory)))484950if __name__ == '__main__':51print_contents()52print('\n', 70 * '#', '\n')53print_contents('http://nbviewer.jupyter.org/github/jakevdp/PythonDataScienceHandbook/blob/master/notebooks/')545556