Community Conundrum 28: News Engagement is live! Read More

How to update a global variable in Scenario

Level 2
How to update a global variable in Scenario

I have a variable defined as below:

{
"Sample_ratio": "0.01"
}

and in Scenario I try to update it as follows:

scenario = Scenario()

scenario.set_global_variables(Sample_ratio='0.001')

but when I print out, the value is still 0.01

print("global variable sample_ratio equals to " + dataiku.get_custom_variables()["Sample_ratio"])

[2020/06/14-10:37:46.623] [Exec-4267] [INFO] [dku.utils]  - global variable sample_ratio equals to 0.01

Any idea why ? 

My purpose is every time after the scenario run, the global variable"Sample_ratio" has an augment of 0.001

 

Thanks, 

Lj

0 Kudos
8 Replies
Dataiker
Dataiker

Hi @zhlj,

scenario.set_global_variables() updates global variables set at the instance level. (These variables can be edited in Administration -> Settings -> Variables).

I suspect your variable Sample_ratio to be defined as a project variable. If the same variable is defined both at the instance and the project level, dataiku.get_custom_variables() will display the value of the project level variable in priority. Therefore, your scenario edits the value of Sample_ratio at the instance level, but the value you print comes from the project variable which hasn't been edited.

To edit the project variable, you need to use set_variables() like in the example below:

project = client.get_project(dataiku.get_custom_variables()['projectKey'])
project_variables = project.get_variables()
project_variables['standard']['Sample_ratio'] = 0.001
project.set_variables(project_variables)

Have a great day!

Level 2
Author

Hi dimitri,

Thanks for reply. The variable I defined should be global variable (as attached PNG file), and that is the only variable I have defined. I tried your method, it didn't work, the output of "Sample_ratio" is still 0.01.

0 Kudos
Dataiker
Dataiker

Hi @zhlj ,

Based on your screenshot I can see that the variable your trying to edit is well a project global variable, which means that set_global_variables() cannot be used in this situation.

To edit this value, I confirm that you should use the set_variables() method of a DSSProject object.

From the internal API, you should add these 2 lines to access the client API, followed then by the code sample shared above.

import dataiku
client = dataiku.api_client()

project = client.get_project(dataiku.get_custom_variables()['projectKey'])
project_variables = project.get_variables()
project_variables['standard']['Sample_ratio'] = 0.001
project_variables = project.set_variables(project_variables)

If your issue remains, could you please share your script?

Have a great day!

0 Kudos
Level 2
Author

Thanks very much dimitri. It worked 😀. there  is a tiny mistake though,  this sentence should be 

project = client.get_project('projectKey')

Thanks again. 

Lj 

0 Kudos
Dataiker
Dataiker

Hi @zhlj,

I'm happy to hear you managed to solve your issue 🙂

Just for information, dataiku.get_custom_variables()['projectKey'] should also work. It retrieves the current project key when used from within a project and when using the internal API.

So the two following lines are equivalent:

project = client.get_project(dataiku.get_custom_variables()['projectKey']) # Do not replace 'projectKey', the current projectKey is sent to get_project()
project = client.get_project('projectKey') # Or just replace manually 'projectKey' with the project key of the project you want to access

Cheers!

Level 2
Author

I see, thanks dimitri. 

Regards,

Lj

0 Kudos
Level 2
Author

one more thing, I am using dataiku internal api which is "import dataiku". In that case how should I fix this problem?

Regards,

Lj

0 Kudos
Level 3

THank you. This helped me in solving my problem