Impossible to select another row in Dash Data Table when view is changed

EDIT: Please see sample data below
I have a dash data Table that gets updated by two different dropdown callbacks

I enabled multi select as such:
image

When I change a value in one of my dropdowns the table updates with different rows but I cannot select any row now as if the previous selection was locked in memory

image

In order to make it work , I have to go back to the previous dropdown value combinations, then unselect that row and then change the dropdowns .

Is there a way to clear the selection of dash data table when a callback is fired?

Here’s the Inspect Source error details on chrome when I keep click and nothing gets selected:

Sample Data

import dash

import dash_core_components as dcc

import dash_html_components as html

from dash.dependencies import Input, Output

import pandas as pd 

from dash_table import DataTable

app = dash.Dash()

country = pd.Series(['germany', 'france','germany', 'france'])

city = pd.Series(['munich', 'paris','berlin', 'lyon'])

pop = pd.Series([100, 200,300, 400])

frame = pd.DataFrame({'country': country, 'city':city, 'pop': pop})

app.layout = html.Div([

        html.Div([

            dcc.Dropdown(

            id='country',

            value = 'germany',

            clearable=False,

            options = [{'label': str(c), 'value': c} for c in frame.country.unique()],

            multi=False

        ),

        dcc.Dropdown(

            id='city',

            clearable=True,

            multi=True

        )]),

        html.Div([

            DataTable(

                id='tbl',

                columns=[{"name": "...", "id": "..."}],

                row_selectable='multi'

                )])

])

# populate simulation dropdown filter

@app.callback(

    Output('city', 'options'),

    Input('country', 'value')

)

def update_filter(country):

    new_frame = frame[(frame.country == country)]

    opt = [{'label': str(c), 'value': c} for c in new_frame.city.unique()]

    return opt

@app.callback(

    Output('tbl', 'columns'),

    Output('tbl', 'data'),

    Input('country', 'value'),

    Input('city', 'value')

)

def update_table(country, city):

    # session descriptive info

    new_frame = frame[(frame.country == country) & (frame.city.isin(city))]

    cols = [{'name': i.capitalize(), 'id': i} for i in new_frame.columns]

    data = new_frame.to_dict('records')

    return cols, data

if __name__ == '__main__':

    app.run_server(debug=True)

Hey @Kapla That sounds like a bug. Can you post a minimal example that reproduces this?

I’ve uploaded the error message in the Inspect source, let me know if that’'s enough to explain the issue.

The error message is not that helpful. What I’d like is a minimal code example that I can copy and paste and get the same error when I run it. See this post for more on how to do that: How to Get your Questions Answered on the Plotly Forum

Ok let me get to that . But I think I understand what creates the issue but Have no idea how to fix it.
Basically if the table has 2 rows and i select the second row index(1), and update my dropdowns which renders a table with one row only and try to select index(0) it wont work.

Now if I modify any value in the dropdowns that renders a table with the same amount of indices or more than the initial table view where I selected my value, it works fine.

I’ve just updated the question with sample data. Try adding both values in the second drop down, then select the second row, remove one value from the second drop down and then try selecting the first row. It won’t allow you

Ah, yes, I think it’s the same issue as reported here: Bugs with pagination/selections when number of data rows changes · Issue #924 · plotly/dash-table · GitHub

This was a very helpful MWE! Might be good to add as another example in that github issue. Just be sure to handle the case when city is None - for example, I added:

    if city is None:
        return dash.no_update
1 Like

Will do! So there’s no real work around for now ? it’s just a bug …

Well, I’ve tried updating selected_rows and/or selected_row_ids, but I haven’t found anything that works.