Multiple SELECT parameters using getChoicesFromPython ?

Mariat
Mariat Registered Posts: 5 ✭✭✭

Hi,

I wanted to use python to dynamically define my choices for a certain parameter of the plugin. I have created also a resource folder to store the python code (computechoices.py). Although I get the following error 'param1 has an invalid type field'. Anything that I am doing wrong?

{"name": "param1",
"label": "label description",
"paramsPythonSetup": "computechoices.py",
"params": [
{"type": "SELECT",
"name": "choices",
"getChoicesFromPython": true}
]
}

def do(payload, config, plugin_config, inputs):
if payload.get('param2') == 'a' && payload.get('param3') == 'b':
choices = [
{"value": "x", "label": "X"},
{"value": "y","label": "Y"}]
return {"choices": choices}

if payload.get('param2') == 'a' && payload.get('param3') == 'c':
choices = [
{"value": "z", "label": "Z"}]
return {"choices": choices}

Answers

  • AgatheG
    AgatheG Dataiker, Registered Posts: 37 Dataiker

    Hi Mariat,

    It seems like the structure of your json file (I assume a recipe.json file?) is not the proper one.

    It should be something like this:

    {

    ...

    "paramsPythonSetup": "computechoices.py",
    "params": [
    {

    "type": "SELECT",
    "name": "param_1",

    "label": "label description",
    "getChoicesFromPython": true

    }
    ],

    ...

    }

    You can have a look at the documentation here: https://doc.dataiku.com/dss/latest/plugins/reference/params.html#dynamic-select-using-python

    Hope this helps,

    Agathe

  • Mariat
    Mariat Registered Posts: 5 ✭✭✭

    Thank you AgatheG,

    that solved the problem. Although now i see the list with the choices is not active. It seems it didn't manage to pass the if statements of the do python function. Any idea why this is happening?

  • AgatheG
    AgatheG Dataiker, Registered Posts: 37 Dataiker
    edited July 17

    It seems the Python code you provided is not valid.

    You could retry with this code instead:

    def do(payload, config, plugin_config, inputs):
        if payload.get('param2') == 'a' and payload.get('param3') == 'b':
            choices = [
                {"value": "x", "label": "X"},
                {"value": "y","label": "Y"}
            ]
            return {"choices": choices}
    
        if payload.get('param2') == 'a' and payload.get('param3') == 'c':
            choices = [{"value": "z", "label": "Z"}]
            return {"choices": choices}

    Please notice that this code does not handle the case where param2 is not a and param3 is neither b nor c, so the dropdown would be left unpopulated in this scenario. Unsure if this is desired/by design.

  • Mariat
    Mariat Registered Posts: 5 ✭✭✭
    edited July 17

    Hi,

    i tried to adjust the code in the do function, but still i don't manage to get into the if statements. It seems like the following expression doesn't work. As param2 i'm using the name of a parameter and as 'a' one of the possible values this parameter can take.

    payload.get('param2') == 'a' 

  • AgatheG
    AgatheG Dataiker, Registered Posts: 37 Dataiker
    edited July 17

    Hi again,

    I am sorry, I was too focused on fixing the small syntax errors of the snippet code and did not pay enough attention to the actual content. We indeed need to make small minor changes in it to match your use case.

    Assuming that you want to dynamically change the options for param1 based on the values taken by param2 and param3, with param2 and param3 taking their respective options from a static list given in your recipe.json file via the field selectChoices, the following code should do it properly:

    def do(payload, config, plugin_config, inputs):
        if config.get('param2') == 'a' and config.get('param3') == 'b':
             choices = [
                {"value": "x", "label": "X"},
                {"value": "y","label": "Y"}
            ]
            return {"choices": choices}
    
        if config.get('param2') == 'a' and config.get('param3') == 'c':
            choices = [{"value": "z", "label": "Z"}]
            return {"choices": choices}

    " height="20" loading="lazy">"> However, some disclaimers apply here:

    1. Notice that if param2 is not a or param3 is neither b nor c, the dropdown for param1 will not be populated. If this is the intended behavior, you can use directly the code I provided above.

    2. The ability to update custom dropdowns in a plugin recipe from the values of other dropdowns (custom or not) is available since 9.0.5 release only

    Hope this helps!

    Agathe

  • Mariat
    Mariat Registered Posts: 5 ✭✭✭

    Sorry for responding late. I'm using DSS 9.0.4, maybe that's why i'm not able to use the multiple parameters from Python.

Setup Info
    Tags
      Help me…