def compile_notebook()

in projects/hive-bigquery-connector-demo/scripts/notebook_funcs.py [0:0]


def compile_notebook():
    """Compiles the Jupyter Notebook from the paragraph files."""
    logger.info("Compiling Jupyter Notebook")

    def replace_line(line):
        for state_key, state in ScriptState.tf_state().__dict__.items():
            state_key_up = f"<{state_key.upper()}>"
            if state_key_up in line:
                line = (
                    line.rstrip().replace(state_key_up, state)
                    + " # variable: "
                    + state_key_up
                )
        return line.rstrip() + "\n"

    notebook_path = "../notebook"
    paragraphs_path = "../notebook/paragraphs"
    cells = []
    for f in sorted(list(os.listdir(paragraphs_path))):
        with open(
            os.path.join(paragraphs_path, f), "r", encoding="utf-8"
        ) as fp:
            content = fp.readlines()
        content = [replace_line(line) for line in content if line.strip()]
        content[-1] = content[-1].rstrip()
        cell_type = f.split(".")[-1].replace("mdx", "markdown")
        cell = {
            "cell_type": cell_type,
            "source": content,
            "execution_count": None,
            "id": str(uuid.uuid1()),
            "metadata": {},
            "outputs": [],
        }
        cells.append(cell)
    notebook_json = {
        "cells": cells,
        "metadata": {
            "kernelspec": {
                "display_name": "Python 3",
                "language": "python",
                "name": "python3",
            },
            "language_info": {
                "codemirror_mode": {
                    "name": "ipython",
                    "version": 3,
                },
                "file_extension": ".py",
                "mimetype": "text/x-python",
                "name": "python",
                "nbconvert_exporter": "python",
                "pygments_l0exer": "ipython3",
                "version": "3.10.8",
            },
        },
        "nbformat": 4,
        "nbformat_minor": 5,
    }
    notebook_full_path = os.path.join(notebook_path, "notebook.ipynb")
    with open(notebook_full_path, "w", encoding="utf-8") as fp:
        json.dump(notebook_json, fp, indent=2)
    return notebook_full_path