tahamajs's picture
download
raw
2.57 kB
#!/usr/bin/env python3
"""
Execute CA11 notebook cell by cell with error handling and output capture
"""
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor, CellExecutionError
import sys
from pathlib import Path
def execute_notebook(notebook_path, output_path=None):
"""
Execute a Jupyter notebook cell by cell
Args:
notebook_path: Path to the input notebook
output_path: Path to save the executed notebook (optional)
"""
print(f"๐Ÿš€ Starting execution of {notebook_path}")
print("=" * 80)
# Read the notebook
with open(notebook_path, 'r', encoding='utf-8') as f:
nb = nbformat.read(f, as_version=4)
# Configure the executor
ep = ExecutePreprocessor(
timeout=600, # 10 minutes per cell
kernel_name='python3',
allow_errors=True # Continue on errors
)
# Get notebook directory for proper path resolution
notebook_dir = Path(notebook_path).parent
print(f"๐Ÿ“ Notebook directory: {notebook_dir}")
print(f"๐Ÿ“Š Total cells: {len(nb.cells)}")
print("=" * 80)
# Execute the notebook
try:
print("\nโš™๏ธ Executing notebook...")
ep.preprocess(nb, {'metadata': {'path': str(notebook_dir)}})
print("\nโœ… Notebook execution completed!")
except Exception as e:
print(f"\nโš ๏ธ Execution encountered errors: {e}")
print("Continuing with partial results...")
# Save the executed notebook
if output_path is None:
output_path = notebook_path.replace('.ipynb', '_executed.ipynb')
with open(output_path, 'w', encoding='utf-8') as f:
nbformat.write(nb, f)
print(f"\n๐Ÿ’พ Saved executed notebook to: {output_path}")
# Print summary
print("\n" + "=" * 80)
print("๐Ÿ“Š EXECUTION SUMMARY")
print("=" * 80)
code_cells = [cell for cell in nb.cells if cell.cell_type == 'code']
executed = sum(1 for cell in code_cells if cell.get('outputs'))
errors = sum(1 for cell in code_cells
if any('ename' in output for output in cell.get('outputs', [])))
print(f"Total code cells: {len(code_cells)}")
print(f"Successfully executed: {executed}")
print(f"Cells with errors: {errors}")
print(f"Success rate: {executed/len(code_cells)*100:.1f}%")
return nb
if __name__ == "__main__":
notebook_path = "notebooks/CA11.ipynb"
if len(sys.argv) > 1:
notebook_path = sys.argv[1]
execute_notebook(notebook_path)

Xet Storage Details

Size:
2.57 kB
ยท
Xet hash:
4a5095c6e01ddddde84b811948f94c2eeac35dbf249ca2f697980a9fb515a55f

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