How can I update my pandas dataframe variables by changes in the user data table on dash?
I was thinking of using global dataframes and changing them through callbacks… but not sure if that would be stable
I use hidden div’s to store my variables, but you can also use dcc.Store; I my case I convert to JSON (this may not be necessary but it works for me). So I use the following to convert my panda data_df
to json.
This is the conversion needed to assign data via callback’s Output
:
data_df.to_json(orient='split')
To convert data passed in via callback’s Input
, I do the following:
data_df = pd.read_json(input_data_variable, orient='split')
Sweet thanks a lot - I appreciate it
Are you able to show your actual callbacks as well if possible
All the `children` components are Hidden Div's in my layout.
import pandas as pd
...
@app.callback(
output=Output('slider-container', 'children'),
inputs=[Input('hilo-data-container', 'children'),
Input('session-data-container', 'children')])
def show_slider(hilo_props, session_props):
# hilo_props and session_props inputs are panda df's converted to JSON
hilo_df = pd.read_json(hilo_props, orient='split')
session_df = pd.read_json(session_props, orient='split')
# Do some stuff
return some_panda_df.to_json(orient='split')
This is what I used… It works if I just call update_sometable, in which case my table gets updated. However it won’t update my table when I click the button through the dash app. Any idea what could be missing?
@app.callback(Output('dccStore', 'data'),
[Input('updateButton', 'n_clicks')],
[State('my-dropdown', 'value')])
def update_sometable(n_clicks, dropdown_selection):
def update_table(dropdown_selection, sometable):
sometable.loc[0, 'column'] = 5
return sometable
if n_clicks > 0:
if not dropdown_selection:
return {}
update_table(dropdown_selection, sometable)
return {}
I mean call update_sometable in a jupyter notebooks cell
Actually this is fine, it is saved in the app cache
But for some reason I cannot get the State of the data table which is odd
Are you able to show an example where the input is the state of a data table?
For example, this will not work:
@app.callback(Output('dccStore', 'data'),
[Input('updateButton', 'n_clicks')],
[State('my-dropdown', 'value'), State('mytable', 'data')])
def update_sometable(n_clicks, dropdown_selection, rows):
def update_table(dropdown_selection, sometable, rows):
sometable.loc[0, 'column'] = rows[0]['column']
return sometable
if n_clicks > 0:
if not dropdown_selection:
return {}
update_table(dropdown_selection, sometable, rows)
return {}
Not sure what you mean by “where the input is the state of a data table”…But in your code above, you don’t have a return
after your update_table
call. You could tweak your code as follows:
if n_clicks > 0:
if not dropdown_selection:
return {}
else:
return update_table(dropdown_selection, sometable)
If you want to update a datatable from a callback, or to obtain the state of the data in the datatable in a callback, the following illustrate this.
1 - update datatable ( i.e. id=my-datatable
) data parameter.
@app.callback([Output('dccStore', 'data'),
[Output('my-datatable', 'data')],
[Input('updateButton', 'n_clicks')],
[State('my-dropdown', 'value')])
def update_sometable(n_clicks, dropdown_selection):
2 - get the state of the datatable data parameter:
@app.callback(Output('dccStore', 'data'),
[Input('updateButton', 'n_clicks')],
[State('my-dropdown', 'value'),
State('my-datatable', 'data')])
def update_sometable(n_clicks, dropdown_selection, my-datatable_data):