Hello,
I have created a callback so that when I click on a marker on a map, two tables are created and when I select a row on the first table, the second is updated.
My code is certainly sub-optimal but it nearly works:
@app.callback([Output(component_id='table-index', component_property='data'),
Output(component_id='table-index', component_property='columns')],
[Output('table-company', 'data'), Output('table-company', 'columns')],
[Input('map-flags', 'selectedData')], [Input('table-index', 'selected_rows')])
def display_selected_data(selectedData, selected_rows):
global df
if selectedData:
index = selectedData['points'][0]['pointIndex']
country = df.iloc[index]['abbreviation']
else:
country = 'us'
df2 = df.loc[df['abbreviation'] == country].copy()
df3 = pd.DataFrame.from_dict(df2['index'].values[0])
df3['country_iso3'] = df2['iso3'].iloc[0]
df3 = df3[['country_iso3', 'id' , 'name']]
df3 = df3.sort_values(['id', 'name'], ascending = [True, True])
columns = [{'name': col, 'id': col} for col in df3.columns]
data = df3.to_dict(orient='records')
print(selected_rows)
if len(selected_rows)>0:
index = df3['id'].iloc[selected_rows[0]].split(':')[1]
else:
index = df3['id'].iloc[0].split(':')[1]
symbol_list = ss.get_symbol_list(index=index)
df_inx = pd.DataFrame.from_dict(symbol_list)
df_inx.drop(['shortName', 'market'], axis=1, inplace=True)
df_inx = df_inx.sort_values(['longName', 'symbol'], ascending = [True, True])
columns2 = [{'name': col, 'id': col} for col in df_inx.columns]
data2 = df_inx.to_dict(orient='records')
return data, columns, data2, columns2
The problem is that I could not find a way to reset the selected_rows argument and, whatever I do, it always remembers the previous value. When this value is out of range, the callback crashes.
I have also tried splitting this into two callbacks. The problem in that case is how to automatically update the second table. In addition, as one row is often pre-selected it is not possible to re-select it.
I am sure there is a better way to accomplish this but I am just starting with Dash.
Best,
Ed