Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add run notebook script #624

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,6 @@ dmypy.json

# PyCharm .idea files
.idea/

# Notebook runner output
scripts/run_notebooks/outputs/*.ipynb
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ dependencies = [
docs = [
"sphinx",
"ipython!=8.7.0",
"fastprogress",
"graphviz",
"lifetimes",
"myst-parser",
"myst-nb",
"pydata-sphinx-theme>=0.12.0.dev0",
Expand All @@ -36,6 +39,7 @@ docs = [
"sphinxext-opengraph",
"sphinx-notfound-page",
"sphinx-design",
"watermark",
]
lint = ["mypy", "pandas-stubs", "pre-commit>=2.19.0", "ruff>=0.1.4"]
test = ["lifetimes==0.11.3", "pytest==7.0.1", "pytest-cov==3.0.0"]
Expand Down
74 changes: 74 additions & 0 deletions scripts/run_notebooks/runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import logging
import os

import papermill as pm
from tqdm import tqdm

KERNEL_NAME: str = "pymc-marketing-dev"
NOTEBOOKS_PATH: str = "docs/source/notebooks"
OUTPUT_PATH: str = "scripts/run_notebooks/outputs"


def setup_logging() -> None:
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)


def get_notebooks_names() -> list[str]:
return [
"clv/bg_nbd.ipynb",
"clv/clv_quickstart.ipynb",
"clv/gamma_gamma.ipynb",
"clv/sBG.ipynb",
"mmm/mmm_budget_allocation_example.ipynb",
"mmm/mmm_example.ipynb",
"mmm/mmm_lift_test.ipynb",
]


def get_notebooks_paths() -> list[str]:
return [
f"{NOTEBOOKS_PATH}/{notebook_name}" for notebook_name in get_notebooks_names()
]


def verify_notebooks_exist() -> None:
for notebook_path in get_notebooks_paths():
if not os.path.exists(notebook_path):
raise FileNotFoundError(f"Notebook not found: {notebook_path}")


def make_output_directory() -> None:
os.makedirs(OUTPUT_PATH, exist_ok=True)


def run_notebook(notebook_path: str) -> None:
output_path = construct_output_path(notebook_path)
pm.execute_notebook(
input_path=notebook_path,
output_path=output_path,
kernel_name=KERNEL_NAME,
)


def construct_output_path(notebook_path: str) -> str:
return f"{OUTPUT_PATH}/{notebook_path.split('/')[-1]}"


if __name__ == "__main__":
setup_logging()
logging.info("Starting notebook runner")

make_output_directory()
logging.info("Output directory created")

notebooks_paths = get_notebooks_paths()
verify_notebooks_exist()

for notebook_path in tqdm(notebooks_paths):
logging.info(f"Running notebook: {notebook_path}")
run_notebook(notebook_path)

logging.info("Notebooks run successfully!")
Loading