CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-papermill

Parameterize and run Jupyter and nteract Notebooks

Pending
Overview
Eval results
Files

inspection.mddocs/

Parameter Inspection

Tools for analyzing and inspecting notebook parameters before execution, enabling validation and dynamic parameter discovery. The inspection system analyzes parameter cells to infer types, defaults, and documentation.

Capabilities

Inspect Notebook Parameters

Returns the inferred notebook parameters from the parameters cell.

def inspect_notebook(
    notebook_path: str | Path,
    parameters: dict = None
) -> dict[str, Parameter]:
    """
    Return the inferred notebook parameters.

    Parameters:
    - notebook_path: Path to notebook
    - parameters: Arbitrary keyword arguments to pass to the notebook parameters

    Returns:
    Dict[str, dict]: Mapping of parameter name to parameter dictionary containing:
        - name: Parameter name
        - inferred_type_name: String representation of inferred type
        - default: String representation of default value
        - help: Help text/description
    """

Usage Examples:

import papermill as pm

# Basic parameter inspection
params = pm.inspect_notebook('analysis.ipynb')
print(f"Found parameters: {list(params.keys())}")

# Inspect with parameter overrides
params = pm.inspect_notebook(
    'notebook.ipynb',
    parameters={'base_path': '/data'}
)

# Access parameter details
for name, info in params.items():
    print(f"Parameter: {name}")
    print(f"  Type: {info['inferred_type_name']}")
    print(f"  Default: {info['default']}")
    print(f"  Help: {info['help']}")

Display Notebook Help

Displays help information about notebook parameters in a CLI-friendly format.

def display_notebook_help(
    ctx: click.Context,
    notebook_path: str,
    parameters: dict
) -> int:
    """
    Display help on notebook parameters.

    Parameters:
    - ctx: Click context
    - notebook_path: Path to the notebook to be inspected
    - parameters: Parameter overrides for path templating

    Returns:
    int: Exit code (0 for success, 1 for failure)
    """

Usage Example:

import click
from papermill.inspection import display_notebook_help

# Create click context
ctx = click.Context(click.Command('test'))

# Display help
exit_code = display_notebook_help(
    ctx,
    'notebook.ipynb',
    {'base_path': '/data'}
)

Internal Functions

Open Notebook with Parameters

Internal utility for opening and parameterizing notebook paths.

def _open_notebook(
    notebook_path: str,
    parameters: dict
) -> nbformat.NotebookNode:
    """
    Opens notebook with parameter path substitution.
    
    Parameters:
    - notebook_path: Path to notebook (may contain parameter templates)
    - parameters: Parameters for path templating
    
    Returns:
    NotebookNode: Loaded notebook
    """

Infer Parameters from Notebook

Internal function that performs the actual parameter inference from notebook cells.

def _infer_parameters(
    nb: nbformat.NotebookNode,
    name: str = None,
    language: str = None
) -> list[Parameter]:
    """
    Infer the notebook parameters.

    Parameters:
    - nb: Notebook to analyze
    - name: Kernel name override
    - language: Language override

    Returns:
    List[Parameter]: List of parameters with metadata (name, inferred_type_name, default, help)
    """

Advanced Usage

Parameter Validation Workflow

import papermill as pm

def validate_and_execute(notebook_path, user_params):
    # Inspect expected parameters
    expected_params = pm.inspect_notebook(notebook_path)
    
    # Validate user provided all required parameters
    required_params = {
        name: info for name, info in expected_params.items() 
        if info['default'] is None
    }
    
    missing_params = set(required_params.keys()) - set(user_params.keys())
    if missing_params:
        raise ValueError(f"Missing required parameters: {missing_params}")
    
    # Check for unknown parameters
    unknown_params = set(user_params.keys()) - set(expected_params.keys())
    if unknown_params:
        print(f"Warning: Unknown parameters: {unknown_params}")
    
    # Execute with validated parameters
    return pm.execute_notebook(
        notebook_path,
        'output.ipynb',
        parameters=user_params
    )

# Usage
try:
    result = validate_and_execute(
        'analysis.ipynb',
        {'dataset': 'sales_2024.csv', 'threshold': 0.8}
    )
    print("Execution successful!")
except ValueError as e:
    print(f"Validation error: {e}")

Dynamic Parameter Discovery

import papermill as pm

def discover_notebook_interface(notebook_path):
    """Discover complete notebook interface"""
    params = pm.inspect_notebook(notebook_path)
    
    interface = {
        'required_parameters': [],
        'optional_parameters': [],
        'parameter_types': {},
        'parameter_help': {}
    }
    
    for name, info in params.items():
        if info['default'] is None or info['default'] == 'None':
            interface['required_parameters'].append(name)
        else:
            interface['optional_parameters'].append(name)
        
        interface['parameter_types'][name] = info['inferred_type_name']
        interface['parameter_help'][name] = info['help']
    
    return interface

# Usage
interface = discover_notebook_interface('notebook.ipynb')
print(f"Required: {interface['required_parameters']}")
print(f"Optional: {interface['optional_parameters']}")

Batch Parameter Analysis

import os
import papermill as pm
from pathlib import Path

def analyze_notebook_directory(directory_path):
    """Analyze all notebooks in a directory"""
    notebook_dir = Path(directory_path)
    analysis_results = {}
    
    for notebook_file in notebook_dir.glob('*.ipynb'):
        try:
            params = pm.inspect_notebook(str(notebook_file))
            analysis_results[notebook_file.name] = {
                'parameter_count': len(params),
                'parameters': params,
                'has_parameters_cell': len(params) > 0
            }
        except Exception as e:
            analysis_results[notebook_file.name] = {
                'error': str(e)
            }
    
    return analysis_results

# Usage
results = analyze_notebook_directory('/path/to/notebooks')
for notebook, info in results.items():
    if 'error' in info:
        print(f"{notebook}: ERROR - {info['error']}")
    else:
        print(f"{notebook}: {info['parameter_count']} parameters")

Install with Tessl CLI

npx tessl i tessl/pypi-papermill

docs

cli.md

exceptions.md

execution.md

index.md

inspection.md

storage.md

tile.json