Good morning Community,
i’m facing an issue managing some Dash components.
This is my goal :
- load a static table from my sql database
- let the users filter results with some dropdown menus
- let the user edit the table
- keep the edited cell until a session is running
In my DataTable proprieties, i’ve set Editable=True
My code looks like this ( semplified version ) :
original_data = pd.read_sql(my_query, my_connection)
@app.callback(
Output('final-table', 'data'),
[Input('filter01_dropdown', 'value'),
Input('filter02_dropdown'', 'value')]
)
def filter_df(selected_filter_01, selected_filter_02):
if not selected_filter_01 and not selected_filter_02:
# Return all rows
return original_data.to_dict('records')
# Else, return selected rows
filtered = original_data.query('field_01 in selected_filter_01 or field_02 in @selected_filter_02')
return filtered.to_dict('records')
My app works well until a new dropdown values is selected.
Every time i change a dropdown value, my callback is triggered and my edited cells are deleted.
How to keep my edited cell after changes in dropdows menu ?
I’ve read the documentation about chained callbacks but i can’t figure out a solution.
This is my attempt:
# add a store components
dcc.Store(id='store-intermediate-table')
# use the store components to store the output
@app.callback(
Output('store-intermediate-table', 'data'),
[Input('filter01_dropdown', 'value'),
Input('filter02_dropdown'', 'value')]
)
def filter_df(selected_filter_01, selected_filter_02):
if not selected_filter_01 and not selected_filter_02:
# Return all rows
return original_data.to_dict('records')
# Else, return selected rows
filtered = original_data.query('field_01 in selected_filter_01 or field_02 in @selected_filter_02')
return filtered.to_dict('records')
@app.callback(
Output('final-table', 'data'),
Input('store-intermediate-table', 'data'),
State('final-table', 'data')
)
def render_table(stored_values, state_data):
final_table = pd.DataFrame(stored_values)
return final_table.to_dict('records')
I think i’m very close to my desidered output.
Thanks in advance