Dataiku App - Edit Project Variables

Aastha
Level 2
Dataiku App - Edit Project Variables

Hi,

 

While creating the dataiku app, I want to create several custom variables with drop down options using getChoicesFromPython. Can I create 2 custom variables in the same python code? Or do I need to create separate 'Edit Project Variable' tiles for each variable? For example, if I have below 2 parameters, how can I add python for both in the same python helper code?

 

Auto generated control = [
{
"name": "PARAM1",
"type": "MULTISELECT",
"getChoicesFromPython": true
},

{
"name": "PARAM2",
"type": "MULTISELECT",
"getChoicesFromPython": true
}
]

 

Python helper code:

import dataiku

def do(payload, config, plugin_config, inputs):
d = dataiku.Dataset("dataset1")
df =d.get_dataframe()
df_1 = df.drop_duplicates(subset="column_name", keep='first')
df_1["label"] = df["column_name"].astype(str) 
df_1["value"] = df["column_name"]
choices = df_1[["label", "value"]].to_dict(orient = "records")
return {"choices": choices}

0 Kudos
1 Reply
JordanB
Dataiker

Hi @Aastha,

You do not need separate tiles, you may use one python helper function to dynamically provision choices for two variable inputs. I've provided an example here where each input is identified by it's 'parameterName'. 

 

import dataiku

def do(payload, config, plugin_config, inputs):
    df = dataiku.Dataset("spotify_songs").get_dataframe()
    
    genres = []
    for genre in df.playlist_genre.unique():
        genres.append({ "value" : genre, "label" : genre})
    
    sub_genres = []
    for subgenre in df.playlist_subgenre.unique():
        sub_genres.append({ "value" : subgenre, "label" : subgenre})

    if payload.get("parameterName") == "genres":
            return {"choices" : genres}
    if payload.get("parameterName") == "subgenre":
            return {"choices" : sub_genres}

 

 

Auto generated controls:

 

[
   {
      "name": "genres",
      "type": "SELECT",
      "label": "genre",
      "getChoicesFromPython": true,
   },
   {
      "name": "subgenre",
      "type": "SELECT",
      "label": "subgenre",
      "getChoicesFromPython": true,
   }
]

 

 

Thanks,

Jordan

Setup info

?
Tags (1)
A banner prompting to get Dataiku