A tool for orchestrating and executing Jupyter notebooks, enabling seamless parameter passing between notebooks.
Project description
notebook-orchestration-and-execution-manager
Orchestrate Jupyter notebooks by passing parameters dynamically between them. This solution enables seamless execution, where the output of one notebook becomes the input for the next. Includes automated execution, parameter injection, logging, and output management for streamlined workflows.
Notebook Execution and Variable Extraction
This project provides a Python class and workflow to manage the execution of Jupyter notebooks with parameters, extract variables and their values from executed notebooks, and display the results in a structured format.
Features
- Execute Jupyter Notebooks: Run Jupyter notebooks with specified parameters using
papermill
. - Dynamic Parameter Passing: Pass custom parameters to notebooks during execution.
- Variable Extraction: Extract variable data (name, operation, and value) from executed notebook cells.
- Logging: Track execution steps with detailed logs.
- Directory Management: Automatically manage output directories for processed notebooks.
Requirements
- Python 3.6+
- Libraries:
os
,papermill
,logging
,ast
,IPython
Install dependencies via pip:
pip install notebook-orchestration-and-execution-manager
Usage
1. Initialize the NotebookOrchestationExecutionManager
Create an instance of NotebookOrchestationExecutionManager
, specifying the directory for processed notebooks.
from notebook_orchestation_execution_manager import NotebookOrchestationExecutionManager
processor = NotebookOrchestationExecutionManager(processed_directory="./processed_notebook")
1.1 Parameters Definition
The recommended practice is to define parameters in the first cell of the notebook. This ensures a clear structure, makes them easy to locate, and provides a centralized configuration that can be used throughout the notebook's execution.
Parameters can be defined in a Markdown, Raw, or Code cell, or even without explicitly defining a cell for this purpose. Parameter injection will automatically take place above the first code cell in the notebook. This provides greater flexibility when working with parameterization tools like Papermill or automating notebook execution in configurable environments.
In Mardown Cell
In Code Cell
No Definition Cell
1.2 Parameters Injection
2. Define Notebooks and Parameters
Provide a list of notebooks with input paths, output paths, and parameter dictionaries.
notebooks_with_parameters = [
("./sample_notebooks/1_Add.ipynb", "./processed_notebook/add_executed.ipynb", {"params": [10, 5, 7]}),
("./sample_notebooks/4_Divide.ipynb", "./processed_notebook/divide_executed.ipynb", {"x": 20, "y": 0}),
("./sample_notebooks/2_Subtract.ipynb", "./processed_notebook/subtract_executed.ipynb", {"x": 10, "y": 3}),
("./sample_notebooks/3_Multiply.ipynb", "./processed_notebook/multiply_executed.ipynb", {"inject_values": {"x": [2, 3], "y": [4, 5]}}),
("./sample_notebooks/5_No_parameters.ipynb", "./processed_notebook/no_parameters_executed.ipynb", {"inject_values": {"x": [2, 3], "y": [4, 5]}}),
]
3. Execute Notebooks
Run each notebook with parameters and save the results.
notebook_execution_results = []
for input_path, output_path, params in notebooks_with_parameters:
notebook_results = processor.run_notebook_with_parameters(input_path, output_path, params)
notebook_execution_results.append(notebook_results)
4. Extract Variables from Notebooks
Extract variable data and display it in a structured format.
for notebook_result in notebook_execution_results:
if notebook_result:
extracted_data = processor.extract_variable_data_from_notebook_cells(notebook_result)
processor.display_notebook_variables_and_values_extracted_from_notebook(extracted_data)
Code Breakdown
1. NotebookOrchestationExecutionManager Class
Handles the execution of notebooks, directory creation, and variable extraction.
Methods
create_directory_if_not_exists(directory: str)
: Ensures the specified directory exists.run_notebook_with_parameters(notebook_input_path: str, notebook_output_path: str, params: dict)
: Executes a Jupyter notebook with parameters.extract_variable_data_from_notebook_cells(notebook_data: dict)
: Extracts variable data from notebook cells.display_notebook_variables_and_values_extracted_from_notebook(extracted_variables_data_from_notebook: dict)
: Displays extracted variable data in logs.
Example Workflow
Input Notebook
- File:
1_Add.ipynb
- Parameters:
{"params": [10, 5, 7]}
Output
- File:
./processed_notebook/add_executed.ipynb
- Logs: Execution details and extracted variables.
Logging
Logs include:
- Notebook execution status.
- Variable extraction details.
- Metadata from executed notebooks.
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file notebook-orchestration-and-execution-manager-0.2.13.tar.gz
.
File metadata
- Download URL: notebook-orchestration-and-execution-manager-0.2.13.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cabcd9a1edf46b9559f83df28e5b8919a0069c391b9a0de5a2c1dfc45b7dd665 |
|
MD5 | 38c01bde47345bbb43dfa88941d93f7a |
|
BLAKE2b-256 | ad2c55266777aef772328121d3a8291c3e2ab643fa650716a0a9645ea0a12e0c |
File details
Details for the file notebook_orchestration_and_execution_manager-0.2.13.0-py3-none-any.whl
.
File metadata
- Download URL: notebook_orchestration_and_execution_manager-0.2.13.0-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8a0caccd772bddc823dafc87cf95f367cc463035fec810562175adf22a39afa |
|
MD5 | f9d91ae9a8d3e133e7bc27e94d57a37f |
|
BLAKE2b-256 | ef8eaed60eca326144ae6eb84f5d4cb33a2ac75df152b8748670785858db9d9b |
File details
Details for the file notebook_orchestration_and_execution_manager-0.2.13-py3-none-any.whl
.
File metadata
- Download URL: notebook_orchestration_and_execution_manager-0.2.13-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5644a053d3c1b8c8a9379f46680fd5e516c993ae495d6d0386fb6860566b57ab |
|
MD5 | 3305c180b530ec45097b2b70d380edab |
|
BLAKE2b-256 | 73bdbcdfd12e02dad3124476867c4e0541c26aa8218e36f94479cc635d05c93f |