Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
probml
GitHub Repository: probml/pyprobml
Path: blob/master/internal/execute_latexified_notebooks.py
1191 views
1
import os
2
from glob import glob
3
import pandas as pd
4
import nbformat
5
import argparse
6
import regex as re
7
import multiprocessing as mp
8
9
notebooks = glob("notebooks/book2/*/*.ipynb") # + glob("notebooks/book1/*/*.ipynb")
10
11
12
def check_fun_to_notebook(notebook, fun):
13
"""
14
fun should take one argument: code
15
"""
16
try:
17
nb = nbformat.read(notebook, as_version=4)
18
except FileNotFoundError:
19
print("File not found:", notebook)
20
return False
21
for cell in nb.cells:
22
code = cell["source"]
23
output = fun(code)
24
if output:
25
return True
26
return False
27
28
29
def check_latexify(code):
30
if "latexify" in code:
31
return True
32
return False
33
34
35
def execute_notebook(notebook):
36
nb_name = notebook.split("/")[-1]
37
print(f"Executing {notebook} ......")
38
os.environ["LATEXIFY"] = ""
39
os.environ["FIG_DIR"] = f"internal/figures/{nb_name}"
40
os.system(f"ipython {notebook}")
41
42
43
latexified_nb = []
44
for no, nb in enumerate(notebooks):
45
if check_fun_to_notebook(nb, check_latexify):
46
latexified_nb.append(nb)
47
48
49
print(f"{len(latexified_nb)} notebooks are latexified")
50
51
52
n_cpu = mp.cpu_count() - 2
53
pool = mp.Pool(n_cpu)
54
pool.map(execute_notebook, latexified_nb)
55
56