Running notebooks with parameters

Parameters passed in the ARGUMENTS list are placed into the sys.argv list, with one entry per argument.

The examples on this page use EXECUTE CODE BUNDLE, which takes a list of argument strings. The older EXECUTE NOTEBOOK PROJECT command takes a single string instead. To pass a value that’s computed at runtime, such as a task configuration value, see Pass arguments computed at runtime.

Example: Execute a Code Bundle with parameters

The following example passes two arguments (env and prod) using ARGUMENTS = ('env', 'prod').

The first element (sys.argv[0]) is the notebook filename, followed by each argument in the list.

EXECUTE CODE BUNDLE "<database_name>"."<schema_name>"."<bundle_name>"
  ENTRYPOINT = 'path/to/notebook.ipynb' -- Notebook file to run
  ARGUMENTS = ('env', 'prod'); -- Each element is passed as a separate argument. Point to the environment configuration.

The compute pool, runtime, query warehouse, and dependencies are defined in the Code Bundle’s code_bundle.yml specification.

View all arguments

To inspect the full list of parameters passed to the session, use the sys module.

import sys
print(sys.argv)

Output example:

['exampletestSCOS.ipynb', 'env', 'prod']

To process or log each parameter individually, loop through the sys.argv list.

import sys
for arg in sys.argv:
    print(arg)

Output example:

exampletestSCOS.ipynb
env
prod

Access a specific argument

Parameters are accessed by their index in the list. Because sys.argv[0] is the notebook name, the first user parameter starts at index[1].

import sys

# Access the first user parameter
first_param = sys.argv[1]
print(first_param)

Output example:

env

For full syntax and parameter details, see EXECUTE CODE BUNDLE.