Path: blob/master/internal/execute_latexified_notebooks.py
1191 views
import os1from glob import glob2import pandas as pd3import nbformat4import argparse5import regex as re6import multiprocessing as mp78notebooks = glob("notebooks/book2/*/*.ipynb") # + glob("notebooks/book1/*/*.ipynb")91011def check_fun_to_notebook(notebook, fun):12"""13fun should take one argument: code14"""15try:16nb = nbformat.read(notebook, as_version=4)17except FileNotFoundError:18print("File not found:", notebook)19return False20for cell in nb.cells:21code = cell["source"]22output = fun(code)23if output:24return True25return False262728def check_latexify(code):29if "latexify" in code:30return True31return False323334def execute_notebook(notebook):35nb_name = notebook.split("/")[-1]36print(f"Executing {notebook} ......")37os.environ["LATEXIFY"] = ""38os.environ["FIG_DIR"] = f"internal/figures/{nb_name}"39os.system(f"ipython {notebook}")404142latexified_nb = []43for no, nb in enumerate(notebooks):44if check_fun_to_notebook(nb, check_latexify):45latexified_nb.append(nb)464748print(f"{len(latexified_nb)} notebooks are latexified")495051n_cpu = mp.cpu_count() - 252pool = mp.Pool(n_cpu)53pool.map(execute_notebook, latexified_nb)545556