Do you know the History of Data Science? READ MORE

Global variable

dhyadav79
Level 1
Global variable

Hi,

How can we create global variable in prepare recipe or scenarios ?

 

Thanks

0 Kudos
4 Replies
AlexT
Dataiker
Dataiker

Hi,

By global variables,

I assume you mean instance-level variables correct? These can be set using the dssclient.set_variables. This requires admin permissions.

import dataiku
from dataiku import pandasutils as pdu
import pandas as pd

client = dataiku.api_client()
global_variables = client.get_variables()
global_variables["today_year"] = "2021"
client.set_variables(global_variables)

#validate for testing only
global_variables = client.get_variables()

print(global_variables)

 

For project global variables these need to be set with the following and global variables which are referred to as "standard" variables there are also local variables, which are not included in bundles.

import dataiku
from dataiku import pandasutils as pdu
import pandas as pd

client = dataiku.api_client()

project = current_project = client.get_project(dataiku.default_project_key())
project_variables = project.get_variables()
#set global project variables 
project_variables ['standard']['new_var_name'] = 'value'
#set local project variables
project_variables ['local']['new_var_name'] = 'value'
project.set_variables(project_variables)


#validate for testing only
variables = project.get_variables()
print(variables)
0 Kudos
dhyadav79
Level 1
Author

Thanks Alex for response.

Could you please help let me if we can  create global variable at scenario (schedule the job) or at visual receipe level

 

Thanks

Dheerendra

0 Kudos
AlexT
Dataiker
Dataiker

Yes, you can also add global variables, project and scenario variables via a scenario step. As seen below. Visual recipe would not set global variables but they can be used by visual recipes see :

https://doc.dataiku.com/dss/latest/variables/index.html

Screenshot 2021-07-29 at 11.42.37.png

0 Kudos
dhyadav79
Level 1
Author

Many Thanks Alex!!!

0 Kudos