nbparameterise Library

0.6.1 · active · verified Fri Apr 17

nbparameterise is a Python library (current version 0.6.1) designed to re-run Jupyter notebooks while substituting input parameters in the first cell. It's often used for automating notebook execution with different configurations or datasets, making it a valuable tool for reproducible research and data pipeline orchestration. The project maintains a steady release cadence, typically driven by bug fixes and minor feature enhancements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically define, parameterise, and execute a Jupyter notebook using `nbparameterise`. It creates a dummy notebook, overrides its initial parameters, executes it, and then prints the output, finally cleaning up generated files. Ensure `ipykernel` is installed for successful execution.

import os
from nbparameterise import parameterise, execute_notebook
import nbformat

# 1. Define the input notebook content (as a string) that includes parameters
input_notebook_content = """
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Parameters\n",
    "x = 10\n",
    "y = 'hello'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(f'Original x: {x}')\n",
    "print(f'Original y: {y}')\n",
    "result = x * 2\n",
    "print(f'Result: {result}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.x.x"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
"""
# Define file paths
input_path = "quickstart_input.ipynb"
output_path = "quickstart_output.ipynb"

# Save the dummy input notebook file
with open(input_path, "w") as f:
    f.write(input_notebook_content)
print(f"Created input notebook: {input_path}")

try:
    # Load the notebook object
    with open(input_path) as f:
        nb = nbformat.read(f, as_version=4)

    # Extract current parameters and define new values to override them
    params = parameterise.extract_parameters(nb)
    new_params = parameterise.parameter_values(params, x=25, y="world!") # Override x and y
    parameterised_nb = parameterise.replace_definitions(nb, new_params)

    # Execute the modified notebook and save the output
    # Requires 'ipykernel' to be installed in the environment.
    execute_notebook(parameterised_nb, output_path)
    print(f"Notebook executed and saved to {output_path}")

    # Optionally, verify the output from the executed notebook
    with open(output_path) as f:
        output_nb = nbformat.read(f, as_version=4)
        print("\n--- Output Notebook Cells ---")
        for cell in output_nb.cells:
            if cell.cell_type == 'code':
                for output in cell.outputs:
                    if output.output_type == 'stream':
                        print(output.text.strip())

except Exception as e:
    print(f"Error during quickstart execution: {e}")
    print("Hint: Ensure 'ipykernel' is installed (`pip install ipykernel`) for notebook execution.")
finally:
    # Clean up generated files
    if os.path.exists(input_path):
        os.remove(input_path)
    if os.path.exists(output_path):
        os.remove(output_path)
    print("\nCleaned up generated files.")

view raw JSON →