Sign up to take part
Registered users can ask their own questions, contribute to discussions, and be part of the Community!
Registered users can ask their own questions, contribute to discussions, and be part of the Community!
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
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!
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!
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!
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!
Thanks very much dimitri. It worked ๐. there is a tiny mistake though, this sentence should be
project = client.get_project('projectKey')
Thanks again.
Lj
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!
I see, thanks dimitri.
Regards,
Lj
one more thing, I am using dataiku internal api which is "import dataiku". In that case how should I fix this problem?
Regards,
Lj
THank you. This helped me in solving my problem