How to update a table?


I have a very large database, I want to show a table with the last 10 data of the database; this database is constantly updated, is there a way to update the table every so often to show the latest data that has just been recorded, as the graphics use dcc.interval, is it possible to use the same with a table ?.


If you are only interested in the last n records in a database you should seriously consider to find out how to only ask/grab the last n entries. This will speed up the app, lower the data traffic etc…

If you do not wish to take my advice, you can load in your data into a pandas dataframe and do something like (I will use pseudo code for example)

def grab_data():
   data = sql_call('the database', 'the query') #pseudo code for 
   df = pandas.DataFrame(data)

   last_entries = df.tail(n=10) #this will show the last 10 entries in the dataframe
return last_entries

Then use the dcc.Interval component to trigger a callback which calls this function every interval. It will grab all the data and spit out the last couple of entries. These you can then use to whatever table shenanigans you so wish. :slight_smile:

I do something similar, and I chose not to rely on dash to query the database in the app due to performance issues. Instead, I have a cron process that writes a csv file of the entries I want and have dash load the csv into a dcc.Store(). I then track the modified_timestamp to update the data in the dashboard. Dash is smart enough to know when the backend data has updated.

Another reason I did this was because my interpretation of Interval is that it is based on the user session. So one user may see the updated data before another if the interval is long.

So it looks like this,

# html layout
dcc.Store(id='data-store',storage_type='session'), # check out storage_type documentation

@app.callback(Output('data-store', 'data'),[
], [State('data-store','data')])
def update_data(timestamp,df):
    return df.reset_index().drop('index',axis=1).to_json()

#callback to access the data and do something
def do_something(data):
    # do something with the data and return the output. 
    return something