|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Update all Jupyter notebooks in the docs directory by executing them. |
| 4 | +This script runs all notebooks and updates their outputs. |
| 5 | +""" |
| 6 | + |
| 7 | +import sys |
| 8 | +import argparse |
| 9 | +from pathlib import Path |
| 10 | +import subprocess |
| 11 | +import concurrent.futures |
| 12 | +from typing import List, Tuple |
| 13 | + |
| 14 | + |
| 15 | +def execute_notebook(notebook_path: Path, timeout: int = 300) -> Tuple[Path, bool, str]: |
| 16 | + """ |
| 17 | + Execute a single notebook and update its outputs. |
| 18 | +
|
| 19 | + Parameters |
| 20 | + ---------- |
| 21 | + notebook_path : Path |
| 22 | + Path to the notebook file |
| 23 | + timeout : int |
| 24 | + Timeout in seconds for notebook execution |
| 25 | +
|
| 26 | + Returns |
| 27 | + ------- |
| 28 | + tuple |
| 29 | + (notebook_path, success, message) |
| 30 | + """ |
| 31 | + try: |
| 32 | + print(f"Executing: {notebook_path}") |
| 33 | + |
| 34 | + # Use jupyter nbconvert to execute and update in place |
| 35 | + result = subprocess.run( |
| 36 | + [ |
| 37 | + "jupyter", "nbconvert", |
| 38 | + "--to", "notebook", |
| 39 | + "--execute", |
| 40 | + "--inplace", |
| 41 | + "--ExecutePreprocessor.timeout={}".format(timeout), |
| 42 | + "--ExecutePreprocessor.kernel_name=python3", |
| 43 | + str(notebook_path) |
| 44 | + ], |
| 45 | + capture_output=True, |
| 46 | + text=True, |
| 47 | + timeout=timeout + 10 # Add buffer to subprocess timeout |
| 48 | + ) |
| 49 | + |
| 50 | + if result.returncode == 0: |
| 51 | + print(f"✓ Success: {notebook_path}") |
| 52 | + return (notebook_path, True, "Executed successfully") |
| 53 | + else: |
| 54 | + error_msg = result.stderr or result.stdout |
| 55 | + print(f"✗ Failed: {notebook_path}") |
| 56 | + print(f" Error: {error_msg[:200]}") |
| 57 | + return (notebook_path, False, error_msg) |
| 58 | + |
| 59 | + except subprocess.TimeoutExpired: |
| 60 | + msg = f"Timeout after {timeout} seconds" |
| 61 | + print(f"✗ Timeout: {notebook_path}") |
| 62 | + return (notebook_path, False, msg) |
| 63 | + except Exception as e: |
| 64 | + msg = str(e) |
| 65 | + print(f"✗ Error: {notebook_path} - {msg}") |
| 66 | + return (notebook_path, False, msg) |
| 67 | + |
| 68 | + |
| 69 | +def find_notebooks(source_dir: Path, exclude_checkpoints: bool = True) -> List[Path]: |
| 70 | + """ |
| 71 | + Find all Jupyter notebooks in the source directory. |
| 72 | +
|
| 73 | + Parameters |
| 74 | + ---------- |
| 75 | + source_dir : Path |
| 76 | + Source directory to search |
| 77 | + exclude_checkpoints : bool |
| 78 | + Whether to exclude .ipynb_checkpoints directories |
| 79 | +
|
| 80 | + Returns |
| 81 | + ------- |
| 82 | + list |
| 83 | + List of notebook paths |
| 84 | + """ |
| 85 | + notebooks = [] |
| 86 | + for nb in source_dir.rglob("*.ipynb"): |
| 87 | + if exclude_checkpoints and ".ipynb_checkpoints" in str(nb): |
| 88 | + continue |
| 89 | + notebooks.append(nb) |
| 90 | + return sorted(notebooks) |
| 91 | + |
| 92 | + |
| 93 | +def main(): |
| 94 | + parser = argparse.ArgumentParser( |
| 95 | + description="Execute and update Jupyter notebooks in docs" |
| 96 | + ) |
| 97 | + parser.add_argument( |
| 98 | + "--source-dir", |
| 99 | + type=Path, |
| 100 | + default=Path("source"), |
| 101 | + help="Source directory containing notebooks (default: source)" |
| 102 | + ) |
| 103 | + parser.add_argument( |
| 104 | + "--timeout", |
| 105 | + type=int, |
| 106 | + default=300, |
| 107 | + help="Timeout per notebook in seconds (default: 300)" |
| 108 | + ) |
| 109 | + parser.add_argument( |
| 110 | + "--parallel", |
| 111 | + type=int, |
| 112 | + default=1, |
| 113 | + help="Number of parallel workers (default: 1)" |
| 114 | + ) |
| 115 | + parser.add_argument( |
| 116 | + "--filter", |
| 117 | + type=str, |
| 118 | + default="", |
| 119 | + help="Filter notebooks by name pattern (e.g., 'quick_start')" |
| 120 | + ) |
| 121 | + parser.add_argument( |
| 122 | + "--dry-run", |
| 123 | + action="store_true", |
| 124 | + help="List notebooks without executing them" |
| 125 | + ) |
| 126 | + parser.add_argument( |
| 127 | + "--fail-fast", |
| 128 | + action="store_true", |
| 129 | + help="Stop on first failure" |
| 130 | + ) |
| 131 | + |
| 132 | + args = parser.parse_args() |
| 133 | + |
| 134 | + # Find all notebooks |
| 135 | + print(f"Searching for notebooks in {args.source_dir}...") |
| 136 | + notebooks = find_notebooks(args.source_dir) |
| 137 | + |
| 138 | + # Apply filter |
| 139 | + if args.filter: |
| 140 | + notebooks = [nb for nb in notebooks if args.filter in str(nb)] |
| 141 | + |
| 142 | + if not notebooks: |
| 143 | + print("No notebooks found.") |
| 144 | + return 0 |
| 145 | + |
| 146 | + print(f"\nFound {len(notebooks)} notebook(s):") |
| 147 | + for nb in notebooks: |
| 148 | + print(f" - {nb.relative_to(args.source_dir.parent)}") |
| 149 | + |
| 150 | + if args.dry_run: |
| 151 | + print("\nDry run - no notebooks executed.") |
| 152 | + return 0 |
| 153 | + |
| 154 | + print(f"\nExecuting notebooks (timeout: {args.timeout}s, workers: {args.parallel})...") |
| 155 | + print("=" * 80) |
| 156 | + |
| 157 | + # Execute notebooks |
| 158 | + results = [] |
| 159 | + if args.parallel > 1: |
| 160 | + # Parallel execution |
| 161 | + with concurrent.futures.ThreadPoolExecutor(max_workers=args.parallel) as executor: |
| 162 | + futures = { |
| 163 | + executor.submit(execute_notebook, nb, args.timeout): nb |
| 164 | + for nb in notebooks |
| 165 | + } |
| 166 | + |
| 167 | + for future in concurrent.futures.as_completed(futures): |
| 168 | + result = future.result() |
| 169 | + results.append(result) |
| 170 | + |
| 171 | + if args.fail_fast and not result[1]: |
| 172 | + # Cancel remaining futures |
| 173 | + for f in futures: |
| 174 | + f.cancel() |
| 175 | + break |
| 176 | + else: |
| 177 | + # Sequential execution |
| 178 | + for nb in notebooks: |
| 179 | + result = execute_notebook(nb, args.timeout) |
| 180 | + results.append(result) |
| 181 | + |
| 182 | + if args.fail_fast and not result[1]: |
| 183 | + break |
| 184 | + |
| 185 | + # Print summary |
| 186 | + print("\n" + "=" * 80) |
| 187 | + print("Summary:") |
| 188 | + print("=" * 80) |
| 189 | + |
| 190 | + success_count = sum(1 for _, success, _ in results if success) |
| 191 | + fail_count = len(results) - success_count |
| 192 | + |
| 193 | + print(f"Total: {len(results)}") |
| 194 | + print(f"Success: {success_count}") |
| 195 | + print(f"Failed: {fail_count}") |
| 196 | + |
| 197 | + if fail_count > 0: |
| 198 | + print("\nFailed notebooks:") |
| 199 | + for nb, success, msg in results: |
| 200 | + if not success: |
| 201 | + print(f" ✗ {nb.relative_to(args.source_dir.parent)}") |
| 202 | + print(f" {msg[:100]}") |
| 203 | + return 1 |
| 204 | + |
| 205 | + print("\n✓ All notebooks executed successfully!") |
| 206 | + return 0 |
| 207 | + |
| 208 | + |
| 209 | +if __name__ == "__main__": |
| 210 | + sys.exit(main()) |
0 commit comments