I have a plotly/dash app.
In my app, I load data at some point
# somewhere in app body
data=get_data()
Later, I present the data in a DataTable
editable=True
.
app.layout = html.Div(
children=[
dash_table.DataTable(
id='myDataTable',
columns=[{"name": i, "id": i} for i in data.columns],
data=data.to_dict('records'),
editable=True,
]),
]),
Later, users can edit the content online and store modifications in a db
@app.callback(
dash.dependencies.Output('a_text_area', 'value'),
[dash.dependencies.Input('myDataTable', 'data')]
)
def loading_data(data):
D=pd.DataFrame(data)
D.to_sql('test_backend',if_exists='replace')
return 'updated'
This works very nicely; I can get my user modifications logged in a db. However, when I refresh the page, user modifications are lost/not reloaded using get_data(). How to ensure get_data() is called every time the page is refreshed?
For updates on page load you could set app.layout
to a function.
If you set app.layout to a function, then you can serve a dynamic layout on every page load.
https://dash.plotly.com/live-updates
Simple example
def serve_layout():
cursor.execute('SELECT CURRENT_TIME')
current_time = cursor.fetchone()
return html.H1(current_time)
app.layout = serve_layout
In the example above the time is retrieved from the database and updates each time the page is refreshed.
In your case you could put your get_data
function call there and afterwards return your layout containing your DataTable
.