Generating a Dropdown in a Dataiku App Using the Python Do Function
Sometimes you want to dynamically generate a custom dropdown in your application. It could be using the values from a dataset column or using the six last month's values from today.
You can actually do this using the "edit project variable" tile of a Dataiku application.
Click on "Use custom UI" below the input for auto-generated controls, you'll see you have an input to add a python code.
The python function should look like this:
def do(payload, config, plugin_config, inputs): choices = [ { "value" : "val1", "label" : "Value 1"}, { "value" : "val2", "label" : "Value 2"} ] return {"choices": choices}
Note: If you load a dataset from it, don't forget to mention the project key as each app instance has its own project key.
Then in the auto-generated controls, you must specify the choices has been generated from the python:
[ { "type": "SELECT", "name": "variable_name", "getChoicesFromPython": true } ]
You can use SELECT or MULTISELECT for the types.
Check these screenshots to see an example where I wanted to update a project variable to filter a dataset according to one of the country codes available in the input dataset.
Fig 1: You use a filter recipe, filtering on columns using a project variable value.
Fig 2: Use the custom UI in Edit project variable tile of a Dataiku App to write the python do function.
Fig 3: That’s it you have made an App where the dropdown to select the country is generated from a dataset.
Answers
-
Hi, Thanks for sharing this information. I am also performing the similar operation but instead of reading the dataset, i am reading the list of scenarios in my project.
I have a query on how can I fetch the value from the drop down which the user selects and trigger the same scenario in the project. Can you provide some guidance on it?
-
Thanks,
could you please elaborate on the parameters used in the do function ?
What is the expected type ? What is the meaning ? -
Hi,
The easiest would be to print the parameters as their content/definition depends on the context of where the dynamic select is defined and monitor the corresponding logs (could be backend.log or the job logs, … again depending on the context.
Here is a brief description of the parameters :
- payload : provided by the javascript.
- In custom forms : the user can set it and give it any value.
- In autoconfig-form-element: it will have the values for :
- parameterType
- parameterName
- customChoices
- config : list of all parameters and their value
- plugin config : list of all parameters of the plugin and their value
- inputs : if we are inside a recipe, the list of the inputs of the recipe. Empty in all other cases.
I hope this helps.
Regards,
Mickaël | R&D at Dataiku
- payload : provided by the javascript.
-
Hi,
Are you also in the context of a Dataiku application ? If yes you mean using the previously selected value in a scenario that can be triggered in the form ?
If it's indeed the context of your question the selected values will be set a project's local variables. You can access them in the scenario as any project variable.
I hope this helps.
Regards,
Mickaël | R&D at Dataiku