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:
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
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)