Skip to main content

Re-run a notebook substituting input parameters in the first cell.

Project description

This is a tool to run notebooks with input values. When you write the notebook, these are defined in the first code cell - or a cell with a ‘parameters’ cell tag - with regular assignments like this:

stock = 'YHOO'
days_back = 600

Nbparameterise handles finding and extracting these parameters, and replacing them with input values. You can then run the notebook with the new values. This can be used for:

  • Batch processing: run the same code on a list of different inputs. See examples/batch.py.

  • Simple user interfaces: build an input form based on the parameters, and run the notebook when the user submits the form. See examples/webapp.py for an implementation of this with an HTML form.

Nbparameterise can identify and replace numbers, strings, booleans (True/False), lists and dicts - the types which can be represented in JSON (apart from None). It’s designed to change parameter values but keep their types, although this isn’t enforced.

Extra information about the parameters, such as names to display in a user interface, can be stored in notebook metadata.

Nbparameterise is written in Python 3, but it can handle notebooks that use Python 2.

Usage:

import nbclient
import nbformat
from nbparameterise import (
    extract_parameters, replace_definitions, parameter_values
)

with open("Stock display.ipynb") as f:
    nb = nbformat.read(f, as_version=4)

# Get a list of Parameter objects
orig_parameters = extract_parameters(nb)

# Update one or more parameters
params = parameter_values(orig_parameters, stock='GOOG')

# Make a notebook object with these definitions
new_nb = replace_definitions(nb, params)

# Execute the notebook with the new parameters
nbclient.execute(new_nb)

If you are interested in using your parameterized Jupyter notebooks through a command line interface, have a look at nbclick.

Changes

0.6.1

2024-05-15

  • nbparameterise no longer requires nbconvert, and loads it only if you pass the deprecated execute=True option.

0.6

2023-02-28

  • The parameters cell no longer needs to be the first code cell: if you add a cell tag ‘parameters’ to another cell, parameters will be extracted from and replaced in that cell. Capitalisation doesn’t matter. (PR #27).

  • Only the parameter values are replaced: other code in the parameter cell will now be preserved unchanged (PR #19). The comment= parameter now has no effect, and it may be removed in a future version.

  • The execute= parameter for replace_definitions() is now deprecated. Please use nbclient to execute your notebook after substituting parameters.

  • nbparameterise now requires Python 3.8 or above.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nbparameterise-0.6.1.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

nbparameterise-0.6.1-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file nbparameterise-0.6.1.tar.gz.

File metadata

  • Download URL: nbparameterise-0.6.1.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.31.0

File hashes

Hashes for nbparameterise-0.6.1.tar.gz
Algorithm Hash digest
SHA256 1b138747635231489844cded4d80ae3eb8b4c41e566242ab310b55e88c2a2a27
MD5 17bdd7cc7f4c20e4e21f1ed94d96b51f
BLAKE2b-256 d2bcf2913ce78f40ade2eff4aacb12f863f0e174241a90838666047cce8a97d3

See more details on using hashes here.

Provenance

File details

Details for the file nbparameterise-0.6.1-py3-none-any.whl.

File metadata

File hashes

Hashes for nbparameterise-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2ed563fdd3fc97adff0239381e19af9d37871d0da11383ae7c405c48e575a2b8
MD5 3548bf6e0b5a924971986c7beb676b40
BLAKE2b-256 0a389d45b7fdfd325f9fa1d548863307a9d441ef1cef6d7b6277101ca59aa907

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page