tahamajs's picture
download
raw
3.34 kB
"""
Simple Notebook Runner using jupyter nbconvert
Runs all notebooks and captures output
"""
import subprocess
import json
from pathlib import Path
from datetime import datetime
import sys
def run_notebook(notebook_path: Path, output_dir: Path):
"""Run a notebook using jupyter nbconvert"""
print(f"\n{'='*80}")
print(f"Running: {notebook_path.name}")
print(f"{'='*80}")
output_nb = output_dir / f"{notebook_path.stem}_executed.ipynb"
# Command to execute notebook
cmd = [
"jupyter",
"nbconvert",
"--to",
"notebook",
"--execute",
"--inplace",
"--ExecutePreprocessor.timeout=600",
"--ExecutePreprocessor.kernel_name=python3",
"--output",
str(output_nb),
str(notebook_path),
]
try:
print(f"Executing notebook...")
result = subprocess.run(
cmd, capture_output=True, text=True, timeout=1200 # 20 minutes
)
if result.returncode == 0:
print(f"✓ Success! Output saved to: {output_nb.name}")
return {"status": "success", "notebook": str(notebook_path.name)}
else:
print(f"✗ Failed with return code: {result.returncode}")
print(f"Error: {result.stderr[:500]}")
return {
"status": "failed",
"notebook": str(notebook_path.name),
"error": result.stderr,
}
except subprocess.TimeoutExpired:
print(f"✗ Timeout after 20 minutes")
return {"status": "timeout", "notebook": str(notebook_path.name)}
except Exception as e:
print(f"✗ Error: {str(e)}")
return {"status": "error", "notebook": str(notebook_path.name), "error": str(e)}
def main():
print("╔" + "=" * 78 + "╗")
print("║" + " " * 78 + "║")
print("║" + "CA4 VQA Notebooks - Simple Runner".center(78) + "║")
print("║" + " " * 78 + "║")
print("╚" + "=" * 78 + "╝")
project_root = Path(__file__).parent
notebooks_dir = project_root / "notebooks"
output_dir = project_root / "data" / "notebook_results"
output_dir.mkdir(parents=True, exist_ok=True)
# Find notebooks
notebooks = list(notebooks_dir.glob("*.ipynb"))
if not notebooks:
print("⚠ No notebooks found!")
return
print(f"\nFound {len(notebooks)} notebook(s):")
for nb in notebooks:
print(f" • {nb.name}")
# Run each notebook
results = []
for notebook in notebooks:
result = run_notebook(notebook, output_dir)
results.append(result)
# Summary
print(f"\n{'='*80}")
print("EXECUTION SUMMARY")
print(f"{'='*80}")
successful = sum(1 for r in results if r["status"] == "success")
failed = sum(1 for r in results if r["status"] != "success")
print(f"Total notebooks: {len(results)}")
print(f"Successful: {successful}")
print(f"Failed: {failed}")
# Save summary
summary_file = (
output_dir / f"summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
)
with open(summary_file, "w") as f:
json.dump(
{"timestamp": datetime.now().isoformat(), "results": results}, f, indent=2
)
print(f"\n→ Summary saved to: {summary_file}")
if __name__ == "__main__":
main()

Xet Storage Details

Size:
3.34 kB
·
Xet hash:
a87d37405f1e79f921b68fb4f6c4d07e505180c0dac86ede3d7b48e62c786567

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.