Update Dash Datatable with selecting multiple dropdown values

Hi there,
I am failing to get the table updated via the dropdown, like whenever I change the dropdown value the table is not changing. I have set the dropdown to be multi and from the dropdown i am using a function to generate a dataframe, the dataframe will have varying number of rows based on the number of items selected in dropdown. How do I get a dynamic output which is changing based on the selected dropdown.

when I selected the single value it is updating the table. But, when I selected the multiple values it is showing an error for example: KeyError: “[(‘Small glass’, ‘Big glass’, ‘H (Height)’)] not in index”.

Here is my dropdown
dcc.Dropdown(id=‘my-dropdown’,
options=[{‘label’: i, ‘value’: i} for i in XX],
value= ‘Big glass’,
multi = True),
and here is my update function:
@app.callback(Output(‘table’, ‘rows’), [Input(‘my-dropdown’, ‘value’)])
def Update_Datatable(selected):
return(Function(XX[[selected]])).to_dict(‘records’)

Does anyone have any ideas about this problem? Please help me. Thank you!

regards,
Kittu.

1 Like

try the following

def update_table(value):
M= #define columns or empty dt
    for i in XX:
        if i in value:
            M=pd.concat(current table and new values)
return M.to_dict('records')

Be sure to define the column names somewhere though. you may need a dummy within which to store the tables you need.
Cheers!

Thank you Ola.

But the problem is when I selecting the multiple values(dataframe columns) in the dropdown it is considering the values as a single value and showing an error ***“[(‘Small glass’, ‘Big glass’, ‘H (Height)’)] not in index”.***.

When I select a value ’Small glass’ it is recognising it in column index of a dataframe and providing the results. But, when I try to add another column ‘Big glass’ it is not considering as a seperate column, it is combining both the columns as a single column and showing an error that it is not in the dataframe index.

My results should be to calculate results for each column(selecting the value in the dropdown) and getting the results by adding the other columns(one after another).

I got the results for each column individually and for all values when selected once. So please help me how to get the results by adding values one after another. Thank you once again for your reply!

regards,
Kittu.

yes, value would indeed be a list… but the for and if loops i mentioned should overcome that.

Remember to concat/make dict reference with ‘i’ and not ‘value’.

yes, Thank you.
I understand your point. But I have to use another function to generate the results. return(Function(XX[[selected]])).to_dict(‘records’). I am trying it but unable to find the way. Do you have any idea?