How to pass to and use parameters in your code?

Why do you need to read this article?

If you want to re-use your code, but need to make some small changes without hardcoding them, Run Parameters and Run Secrets can be very helpful. This article it will show you how to pass and use such parameters in your code.

Steps

Passing Run Parameters

When you are ready to run your code, you'll be able to provide parameters in either RUN PARAMETERS or RUN SECRETS, see a screenshot below and more detail in Running a Code Object.

documentation_run-code-object_singledataset.png

Loading/Using Run Parameters

To use the parameters, simply access them by reading the JSON file in which they are stored. The parameters will be stored in "/input/run_params.json" and in "/input/secret_run_params.json" for RUN PARAMETERS and RUN SECRETS PARAMETERS respectively. For example, let's say you want to pass "my_parameter" = 55555, in RUN PARAMETERS. In the UI, you'll set the following RUN PARAMETER:

{"my_parameter": 55555}

If you were to write Python code, in your code first you'd read /input/run_params.json:

parameter_key = "my_parameter"

run_params = {}
run_params_filename = "/input/run_params.json"
try:
with open(run_params_filename, "r") as run_params_file:
run_params = json.load(run_params_file)
except IOError:
logging.error("No params file found")
sys.exit(1)
except json.JSONDecodeError:
logging.error("Invalid JSON found in params file")
sys.exit(1)

if parameter_key not in run_params:
logging.error(f"Key '{parameter_key}' not found in run parameters")
sys.exit(1)

and last you'd access the value of your parameter as follows:

my_parameter_value = run_params[parameter_key]

For RUN SECRETS PARAMETERS the process would be almost identical except that you would set the parameters in the RUN SECRET PARAMETERS input form, and you would read them from /input/secret_run_params.json.

Was this article helpful?
0 out of 0 found this helpful

Articles in this section