Buckets:
tahamajs/Sysmem2_in_AI / ComputerAssignments /CA5_geometric_reasoning /notebooks /execute_notebook.py
| #!/usr/bin/env python3 | |
| """ | |
| Execute notebook cell by cell and capture outputs | |
| """ | |
| import nbformat | |
| from nbconvert.preprocessors import ExecutePreprocessor | |
| import os | |
| import sys | |
| def execute_notebook(notebook_path, output_path=None): | |
| """ | |
| Execute a Jupyter notebook and save the results | |
| Args: | |
| notebook_path: Path to the input notebook | |
| output_path: Path to save the executed notebook (optional) | |
| """ | |
| if output_path is None: | |
| output_path = notebook_path | |
| # Read the notebook | |
| print(f"๐ Reading notebook: {notebook_path}") | |
| with open(notebook_path, "r", encoding="utf-8") as f: | |
| nb = nbformat.read(f, as_version=4) | |
| # Configure the executor | |
| print("โ๏ธ Configuring executor...") | |
| ep = ExecutePreprocessor( | |
| timeout=600, # 10 minutes per cell | |
| kernel_name="python3", | |
| allow_errors=False, # Stop on errors | |
| store_widget_state=True, | |
| interrupt_on_timeout=True, | |
| ) | |
| # Execute the notebook | |
| print("๐ Executing notebook...") | |
| print(f"Total cells: {len(nb.cells)}") | |
| try: | |
| # Get the notebook directory for execution context | |
| notebook_dir = os.path.dirname(os.path.abspath(notebook_path)) | |
| # Execute | |
| ep.preprocess(nb, {"metadata": {"path": notebook_dir}}) | |
| print("โ Notebook executed successfully!") | |
| # Save the executed notebook | |
| print(f"๐พ Saving executed notebook to: {output_path}") | |
| with open(output_path, "w", encoding="utf-8") as f: | |
| nbformat.write(nb, f) | |
| print("โ Done!") | |
| return True | |
| except Exception as e: | |
| print(f"โ Error during execution: {e}") | |
| print(f"Saving partial results to: {output_path}") | |
| # Save partially executed notebook | |
| with open(output_path, "w", encoding="utf-8") as f: | |
| nbformat.write(nb, f) | |
| return False | |
| if __name__ == "__main__": | |
| notebook_path = "Advanced_Geometric_Reasoning.ipynb" | |
| output_path = "Advanced_Geometric_Reasoning_executed.ipynb" | |
| success = execute_notebook(notebook_path, output_path) | |
| sys.exit(0 if success else 1) | |
Xet Storage Details
- Size:
- 2.15 kB
- Xet hash:
- c4042e4972b3433f0e3b77978bf0a90087351a736cfef4395657ad32f843c373
ยท
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.