Quantcast
Channel: 🎛️ Dash - Plotly Community Forum
Viewing all articles
Browse latest Browse all 6271

Keep edited DataTable after dropdown menu changes

$
0
0

Good morning Community,
i’m facing an issue managing some Dash components.

This is my goal :

  1. load a static table from my sql database
  2. let the users filter results with some dropdown menus
  3. let the user edit the table
  4. 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

4 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 6271

Latest Images

Trending Articles



Latest Images