Path: blob/master/internal/book2/handle_book1_notebooks.py
1192 views
from glob import glob1from itertools import count2import nbformat as nbf34book1_nb = glob("notebooks/book1/*/*.ipynb")5book1_nb_to_chap = {}6for nb in book1_nb:7name = nb.split("/")[-1]8chap = nb.split("/")[-2]9book1_nb_to_chap[name] = chap1011colab_base_url = "https://colab.research.google.com/github/probml/pyprobml/blob/master/notebooks/book1"12prefix = "Source of this notebook is here:"1314book2_nb = glob("notebooks/book2/*/*.ipynb")15counter = 016for nb in book2_nb:17name = nb.split("/")[-1]18if name in book1_nb_to_chap:19# read20nb_content = nbf.read(nb, as_version=4)2122# replace with redirected link23book1_chap = book1_nb_to_chap[name]2425# create new cell26new_cell = nbf.v4.new_markdown_cell(f"{prefix} {colab_base_url}/{book1_chap}/{name}")27nb_content["cells"] = [new_cell]2829# write30nbf.write(nb_content, nb)3132print(f"{nb} is redirected to {colab_base_url}/{book1_chap}/{name}")33counter += 13435print("Done. {} notebooks are redirected.".format(counter))363738