Dropdown exceptin

Hi community.

Im new to dash. Im trying to make a callback that populates a dropdown with column names from a dashtable, when a specific tab is selected. But for some reason. When i make a list with more then 1 item. It raises an exception. It only works for one item :confused:

dash._grouping.SchemaLengthValidationError: Schema: [<Output dashtable-columns.options>]
Path: ()
Expected length: 1
Received value of length 2:
[{‘label’: ‘1’, ‘value’: ‘1’}, {‘label’: ‘2’, ‘value’: ‘2’}]

Can anyone figure out what im doing wrong? thx

@dash_app.callback(
    [
        `cOutput(component_id="dashtable-columns", component_property="options")
    ],
    [
        Input(component_id="tabs", component_property="value"),
        Input(component_id="table-dataset", component_property="columns")
    ]
)
def update_sel_col_for_curr_dataset(main_tab, df_value):
    if main_tab == "tab-dataset":
        return [{"label": v["name"], "value": v["id"]} for v in df_value]
    else:
        raise PreventUpdate

Callback:

Layout:
  dcc.Dropdown(
      id="dashtable-columns",
      options=[],
      value="value",
      clearable=False,
      multi=True,
      persistence=True,
  ),

Hello @Legenden,

Welcome!

This is due to your Output being wrapped in a list, if you remove that is should populate correctly. :slight_smile:

@dash_app.callback(
        cOutput(component_id="dashtable-columns", component_property="options"),
    [
        Input(component_id="tabs", component_property="value"),
        Input(component_id="table-dataset", component_property="columns")
    ]
)
def update_sel_col_for_curr_dataset(main_tab, df_value):
    if main_tab == "tab-dataset":
        return [{"label": "1", "value": "1"}, {"label": "2", "value": "2"}]
    else:
        raise PreventUpdate

With callbacks, whatever form you declare them in, is the form they are expected to be returned.

Thx for the reply :slight_smile:

Actually im diung this with list comprehension for the dicts. The this is, if there is only 1 dicts in the list. Then it works fine. Also, the option in the layout is a list. So it should be a list that is returned?

Yes, the dropdown options should be a list.

My comment was about your declaration of the Output and not the list options you were returning.

Take a look at the image here:

image

If you are declaring your output to be in list form in your callback, you must have a list response.

It was returning when you had just one selected because the length of the response was just 1, but once you added a second column, your list response was no longer one.

You can either remove the list in the callback definition, or place the new options in an enclosing list. :slight_smile:

1 Like

It works! Thanks a bunch! Much appreciated :slight_smile:

1 Like