Parameterize and run Jupyter and nteract Notebooks
—
Core functionality for executing Jupyter notebooks with parameter injection, supporting various execution options, progress tracking, and error handling. The execution engine provides comprehensive control over notebook execution lifecycle.
Executes a single notebook locally with comprehensive parameter and execution control.
def execute_notebook(
input_path: str | Path | nbformat.NotebookNode,
output_path: str | Path | None,
parameters: dict = None,
engine_name: str = None,
request_save_on_cell_execute: bool = True,
prepare_only: bool = False,
kernel_name: str = None,
language: str = None,
progress_bar: bool = True,
log_output: bool = False,
stdout_file = None,
stderr_file = None,
start_timeout: int = 60,
report_mode: bool = False,
cwd: str | Path = None,
**engine_kwargs
) -> nbformat.NotebookNode:
"""
Executes a single notebook locally.
Parameters:
- input_path: Path to input notebook or NotebookNode object
- output_path: Path to save executed notebook. If None, no file will be saved
- parameters: Arbitrary keyword arguments to pass to the notebook parameters
- engine_name: Name of execution engine to use
- request_save_on_cell_execute: Request save notebook after each cell execution
- prepare_only: Flag to determine if execution should occur or not
- kernel_name: Name of kernel to execute the notebook against
- language: Programming language of the notebook
- progress_bar: Flag for whether or not to show the progress bar
- log_output: Flag for whether or not to write notebook output to the configured logger
- stdout_file: File to write stdout to during execution
- stderr_file: File to write stderr to during execution
- start_timeout: Duration in seconds to wait for kernel start-up
- report_mode: Flag for whether or not to hide input
- cwd: Working directory to use when executing the notebook
- **engine_kwargs: Arbitrary keyword arguments to pass to the notebook engine
Returns:
NotebookNode: Executed notebook object
Raises:
PapermillExecutionError: When notebook execution encounters errors
PapermillMissingParameterException: When required parameters are missing
"""Usage Examples:
import papermill as pm
# Basic execution
pm.execute_notebook('input.ipynb', 'output.ipynb')
# With parameters
pm.execute_notebook(
'analysis.ipynb',
'result.ipynb',
parameters={'dataset': 'sales_2024.csv', 'threshold': 0.8}
)
# With execution options
pm.execute_notebook(
'report.ipynb',
'monthly_report.ipynb',
parameters={'month': 'January', 'year': 2024},
kernel_name='python3',
progress_bar=True,
log_output=True,
start_timeout=120,
cwd='/data/reports'
)
# Prepare without executing (validation)
pm.execute_notebook(
'template.ipynb',
'prepared.ipynb',
parameters={'param1': 'value1'},
prepare_only=True
)
# Report mode (hide input cells)
pm.execute_notebook(
'analysis.ipynb',
'clean_report.ipynb',
parameters={'data_file': 'data.csv'},
report_mode=True
)Prepares notebook metadata for execution, setting up execution tracking and provenance information.
def prepare_notebook_metadata(
nb: nbformat.NotebookNode,
input_path: str,
output_path: str,
report_mode: bool = False
) -> None:
"""
Prepares notebook metadata for execution.
Parameters:
- nb: Notebook to prepare
- input_path: Input notebook path
- output_path: Output notebook path
- report_mode: Whether to prepare for report mode
"""Utilities for handling and processing execution errors.
def remove_error_markers(nb: nbformat.NotebookNode) -> None:
"""
Removes error markers from notebook cells.
Parameters:
- nb: Notebook to clean
"""
def raise_for_execution_errors(nb: nbformat.NotebookNode, output_path: str) -> None:
"""
Raises exceptions for execution errors found in notebook.
Parameters:
- nb: Executed notebook to check for errors
- output_path: Path where notebook was saved
Raises:
PapermillExecutionError: If execution errors are found
"""# Using a custom engine
pm.execute_notebook(
'notebook.ipynb',
'output.ipynb',
engine_name='custom_engine',
custom_engine_param='value'
)import sys
# Redirect stdout and stderr
with open('execution.log', 'w') as stdout_file, \
open('errors.log', 'w') as stderr_file:
pm.execute_notebook(
'notebook.ipynb',
'output.ipynb',
stdout_file=stdout_file,
stderr_file=stderr_file,
log_output=True
)import nbformat
# Load notebook as NotebookNode first
nb = nbformat.read('input.ipynb', as_version=4)
# Execute using NotebookNode
result_nb = pm.execute_notebook(
nb, # NotebookNode instead of path
'output.ipynb',
parameters={'param': 'value'}
)
# Process result without saving to file
result_nb = pm.execute_notebook(
'input.ipynb',
None, # Don't save to file
parameters={'param': 'value'}
)Install with Tessl CLI
npx tessl i tessl/pypi-papermill