✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

Automatically reload data from frequently updated csv

Hi All,

I have a csv which gets appended with new data every couple of seconds.
I would like to have a data table based on this csv so that the data table is always showing the newest data first in the csv.

So far I was only able to have the new data in the data table by quitting from the app and rerunning the application. Any idea how it can be done automatically? Below is my code as it is currently:

import dash
import dash_table
import pandas as pd

app = dash.Dash(name)

df = pd.read_csv(‘C:/Users/AZT/Documents/csvfile20190903.csv’, encoding=‘cp1250’)
df = df.reindex(index=df.index[::-1])

def serve_layout():
return dash_table.DataTable(
data=df.to_dict(‘records’),
columns=[
{‘name’: i, ‘id’: i} for i in df.columns
],
style_data_conditional=[
{
‘if’: {
‘column_id’: ‘Attribute6’,
‘filter_query’: ‘{Attribute6} = 2’,
},
‘backgroundColor’: ‘pink’
},
{
‘if’: {
‘column_id’: ‘Attribute6’,
‘filter_query’: ‘{Attribute6} < 2’,
},
‘backgroundColor’: ‘lightblue’
},
]
)

app.layout = serve_layout

if name == ‘main’:
app.run_server(debug=True)

Thank you in advance!

There’s no way (as far as I know) to make it refresh on update. However, you can have it reload the CSV every x seconds.

See this link: https://dash.plot.ly/live-updates

in your app.layout, add an interval component.

Then, write a function that reads the csv and returns the data. Above this function, put a callback that takes the interval as the input and data as the output.

It’s working for me now. Let me know if you have other questions.

Thank you @klamike!

Can you share the code of your solution?

@AZT Hi AZT, did you found solution for your problem. I have the similar problem. My data table based on csv gets new data every couple of seconds. So I want to read this csv file with pandas, and update pandas table every 60 seconds. I tried this with while True, but it did’t work.
If you found solution, can you sand me the code. Thank you.