Path: blob/master/internal/book2/handle_book2_duplicates.py
1192 views
from glob import glob1import nbformat as nbf23book2_nb = glob("notebooks/book2/*/*.ipynb")45colab_base_url = "https://colab.research.google.com/github/probml/pyprobml/blob/master/notebooks/book2"6prefix = "Source of this notebook is here:"78# create notebook to chapters mapping9nb_to_chap = {}10for nb in book2_nb:11name = nb.split("/")[-1]12chap = nb.split("/")[-2]13if name in nb_to_chap:14nb_to_chap[name].append(chap)15else:16nb_to_chap[name] = [chap]1718# keep first notebook and redirect others19counter = 020for nb_name in nb_to_chap:21chapters = sorted(nb_to_chap[nb_name])22first_chap = chapters[0]23for chapter in chapters[1:]:24# read25nb_content = nbf.read(f"notebooks/book2/{chapter}/{nb_name}", as_version=4)2627# replace with redirected link28new_cell = nbf.v4.new_markdown_cell(f"{prefix} {colab_base_url}/{first_chap}/{nb_name}")29nb_content["cells"] = [new_cell]3031# write32nbf.write(nb_content, f"notebooks/book2/{chapter}/{nb_name}")33print(f"{nb_name} duplicate in {chapter} is redirected to {first_chap}")34counter += 13536print("Done. {} notebooks are redirected.".format(counter))373839