Hi everybody,
I’m new in Plotly and I need your help. My Idea is a Datatable with a live update every 2min. I’m started with this example here: https://dash.plotly.com/datatable
Ok, it works. I get Data from my csv-file. But how do I get the table to update automatically?
I’ve read any websites with examples on a dcc-object. But, I have not found an example with a Table.
Please give me a hint or an example with this…
Best Regards
Torsten
Hi @Torsten
For updating any Dash component (like a table) you need to add a dcc.Interval in the layout (in any part of the layout), and use the property n_intervals as Input in a callback, everytime the n_intervals change the callback will be executed.
Inside the callback build your table and return it to any html.Div in the layout.
Example of a layout with html.Div and dcc.Interval:
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div(
html.Div([
html.Div(id='output'),
dcc.Interval(
id='interval-component',
interval=1*1000, # in milliseconds
n_intervals=0
)
])
)
Example of a callback:
@app.callback(Output('output', 'children'),
Input('interval-component', 'n_intervals'))
def update(n):
# bild here the table you want to show
table = dash_table.DataTable( ....
return table
Here you will find the documentation about dcc.Interval:
https://dash.plotly.com/live-updates
Hi Eduardo,
Thanks for your response. I have updated my code which looks like this:
import dash
import dash_table
import pandas as pd
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Output, Input
app = dash.Dash(__name__)
app.layout = html.Div([ # html.H4('Data Live Feed'),
html.Div(id='output'),
dcc.Interval(
id='interval-component',
interval = 3*1000,
n_intervals=0)])
@app.callback(Output('output', 'children'),
Input('interval-component', 'n_intervals'))
def update_Table():
df = pd.read_csv(r'C:\Users\freydtt\Programmierung\Python\mydata.csv')
table = dash_table.DataTable(
id='table',
columns=[{"name": i, "id": i} for i in df.columns],
data=df.to_dict('records'))
return table
if __name__ == '__main__':
app.run_server(debug=False)
The Update-Request works fine.
Thanks…
2 Likes