Skip to content

Commit 4f53eb6

Browse files
committed
Merge branch 'feature/test-notebooks-ci' of https://github.com/selmanozleyen/squidpy into feature/test-notebooks-ci
2 parents 9ce411c + 41bff5e commit 4f53eb6

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

.run_notebooks.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env python3
2+
from __future__ import annotations
3+
4+
import argparse
5+
import glob
6+
import os
7+
import subprocess
8+
import sys
9+
10+
EPILOG = """
11+
Examples:
12+
python .run_notebooks.py docs/notebooks
13+
python .run_notebooks.py /path/to/notebooks --kernel my-kernel
14+
"""
15+
16+
17+
def main() -> None:
18+
# Set up argument parser
19+
parser = argparse.ArgumentParser(
20+
description="Run Jupyter notebooks in specified directories using jupytext",
21+
formatter_class=argparse.RawDescriptionHelpFormatter,
22+
epilog=EPILOG,
23+
)
24+
25+
parser.add_argument("base_directory", help="Base directory containing notebook subdirectories")
26+
27+
parser.add_argument(
28+
"-k", "--kernel", default="squidpy", help="Jupyter kernel to use for execution (default: squidpy)"
29+
)
30+
31+
parser.add_argument(
32+
"--dry-run", action="store_true", help="Show which notebooks would be run without executing them"
33+
)
34+
35+
args = parser.parse_args()
36+
37+
# Base directory for notebooks
38+
base_dir = args.base_directory
39+
40+
# Define notebook directories or patterns
41+
notebook_patterns = [
42+
f"{base_dir}/examples/tools/*.ipynb",
43+
f"{base_dir}/examples/plotting/*.ipynb",
44+
f"{base_dir}/examples/image/*.ipynb",
45+
f"{base_dir}/examples/graph/*.ipynb",
46+
# f"{base_dir}/tutorials/*.ipynb" # don't include because it contains many external modules
47+
]
48+
49+
# Initialize a list to hold valid notebook paths
50+
valid_notebooks = []
51+
52+
# Gather all valid notebook files from the patterns
53+
print("Gathering notebooks...")
54+
for pattern in notebook_patterns:
55+
for nb_path in glob.glob(pattern):
56+
if os.path.isfile(nb_path): # Check if the file exists
57+
valid_notebooks.append(nb_path) # Add to the list of valid notebooks
58+
59+
# Check if we have any notebooks to run
60+
if len(valid_notebooks) == 0:
61+
print("No notebooks found to run.")
62+
sys.exit(1)
63+
64+
# Echo the notebooks that will be run for clarity
65+
print("Preparing to run the following notebooks:")
66+
for nb in valid_notebooks:
67+
print(f" {nb}")
68+
69+
# If dry run, exit here
70+
if args.dry_run:
71+
print(f"\nDry run complete. Would execute {len(valid_notebooks)} notebooks with kernel '{args.kernel}'.")
72+
return
73+
74+
# Initialize a flag to track the success of all commands
75+
all_success = True
76+
77+
# Execute all valid notebooks
78+
print(f"\nExecuting notebooks with kernel '{args.kernel}'...")
79+
for nb in valid_notebooks:
80+
print(f"Running {nb}")
81+
try:
82+
subprocess.run(["jupytext", "-k", args.kernel, "--execute", nb], check=True)
83+
except subprocess.CalledProcessError:
84+
print(f"Failed to run {nb}")
85+
all_success = False
86+
87+
# Check if any executions failed
88+
if not all_success:
89+
print("One or more notebooks failed to execute.")
90+
sys.exit(1)
91+
92+
print("All notebooks executed successfully.")
93+
94+
95+
if __name__ == "__main__":
96+
main()

0 commit comments

Comments
 (0)