How to update a global variable in Scenario

zhlj
zhlj Registered Posts: 24 ✭✭✭✭
edited July 16 in Using Dataiku

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

Best Answers

  • dimitri
    dimitri Dataiker, Product Ideas Manager Posts: 33 Dataiker
    edited July 17 Answer ✓

    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!

  • dimitri
    dimitri Dataiker, Product Ideas Manager Posts: 33 Dataiker
    edited July 17 Answer ✓

    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!

Answers

  • dimitri
    dimitri Dataiker, Product Ideas Manager Posts: 33 Dataiker
    edited July 17

    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!

  • zhlj
    zhlj Registered Posts: 24 ✭✭✭✭

    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.

  • zhlj
    zhlj Registered Posts: 24 ✭✭✭✭

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

    Regards,

    Lj

  • zhlj
    zhlj Registered Posts: 24 ✭✭✭✭
    edited July 17

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

    project = client.get_project('projectKey')

    Thanks again.

    Lj

  • zhlj
    zhlj Registered Posts: 24 ✭✭✭✭

    I see, thanks dimitri.

    Regards,

    Lj

  • rsingamsetty
    rsingamsetty Dataiku DSS Core Designer, Dataiku DSS & SQL, Registered Posts: 18 ✭✭✭✭✭

    THank you. This helped me in solving my problem

Setup Info
    Tags
      Help me…