Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
probml
GitHub Repository: probml/pyprobml
Path: blob/master/internal/book2/handle_book1_notebooks.py
1192 views
1
from glob import glob
2
from itertools import count
3
import nbformat as nbf
4
5
book1_nb = glob("notebooks/book1/*/*.ipynb")
6
book1_nb_to_chap = {}
7
for nb in book1_nb:
8
name = nb.split("/")[-1]
9
chap = nb.split("/")[-2]
10
book1_nb_to_chap[name] = chap
11
12
colab_base_url = "https://colab.research.google.com/github/probml/pyprobml/blob/master/notebooks/book1"
13
prefix = "Source of this notebook is here:"
14
15
book2_nb = glob("notebooks/book2/*/*.ipynb")
16
counter = 0
17
for nb in book2_nb:
18
name = nb.split("/")[-1]
19
if name in book1_nb_to_chap:
20
# read
21
nb_content = nbf.read(nb, as_version=4)
22
23
# replace with redirected link
24
book1_chap = book1_nb_to_chap[name]
25
26
# create new cell
27
new_cell = nbf.v4.new_markdown_cell(f"{prefix} {colab_base_url}/{book1_chap}/{name}")
28
nb_content["cells"] = [new_cell]
29
30
# write
31
nbf.write(nb_content, nb)
32
33
print(f"{nb} is redirected to {colab_base_url}/{book1_chap}/{name}")
34
counter += 1
35
36
print("Done. {} notebooks are redirected.".format(counter))
37
38