Set a project level variable from a python recipe
![tomas](https://us.v-cdn.net/6038231/uploads/lithium_attachments/n5261i79DA1BFFD65C0DD1.png)
I need to set a project level variable from a python recipe. How can I do that? I dont want to use Scenario in this case.
Thanks for the example,
Tomas
Best Answer
-
Hi,
You need to use the public API Python client
dataiku.api_client().get_project().get_variables() ... then set_variables()
Answers
-
Thanks, actually it is a little bit more complicated, the sample code is:
import dataiku
v = dataiku.api_client().get_project('PROJECT_KEY').get_variables()
v['standard']['my_variable_name'] = 'value'
dataiku.api_client().get_project('PROJECT_KEY').set_variables(v) -
Marlan Neuron 2020, Neuron, Registered, Dataiku Frontrunner Awards 2021 Finalist, Neuron 2021, Neuron 2022, Dataiku Frontrunner Awards 2021 Participant, Neuron 2023 Posts: 318 Neuron
Updating to use default project key (i.e., project key for current project):
project_handle = dataiku.api_client().get_project(dataiku.default_project_key())
vars = project_handle.get_variables()
vars['standard']['varname'] = 'varvalue'
project_handle.set_variables(vars) -
taraku Dataiker, Alpha Tester, Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Registered Posts: 53 Dataiker
Thanks @Marlan
for showing how the default project key works! - I was able to use the "default project key" in my Custom Python step in a Scenario. Now I can make copies of my project and run the scenario without having to update the formerly hard-coded project key!
# obtain a handle on the instance and default project key
client = dataiku.api_client()
project = dataiku.api_client().get_project(dataiku.default_project_key())vs. hard-coded:
# obtain a handle on your instance and project
client = dataiku.api_client()
project = client.get_project('variables102final')