# How to update a table?

**URL:** <https://community.plotly.com/t/how-to-update-a-table/22180>\
**Category:** Dash Python\
**Created:** [April 11, 2019, 4:11pm UTC](https://community.plotly.com/t/how-to-update-a-table/22180 "2019-04-11T16:11:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SaulSalBal](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@SaulSalBal](https://community.plotly.com/u/SaulSalBal)\
**Post date:** [April 11, 2019, 4:11pm UTC](https://community.plotly.com/t/how-to-update-a-table/22180/1 "2019-04-11T16:11:40Z")

</div>

Hello,

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 ?.

Regards.

---

<div class="post-metadata">

**Author:** ![Blaceus](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/blaceus/32/2992_2.png) [@Blaceus](https://community.plotly.com/u/Blaceus)\
**Post date:** [April 12, 2019, 1:24pm UTC](https://community.plotly.com/t/how-to-update-a-table/22180/2 "2019-04-12T13:24:10Z")

</div>

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)

```auto
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. 🙂

---

<div class="post-metadata">

**Author:** ![drwx](https://avatars.discourse-cdn.com/v4/letter/d/cab0a1/32.png) [@drwx](https://community.plotly.com/u/drwx)\
**Post date:** [April 12, 2019, 1:35pm UTC](https://community.plotly.com/t/how-to-update-a-table/22180/3 "2019-04-12T13:35:56Z")

</div>

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.

[https://dash.plot.ly/dash-core-components/store](https://dash.plot.ly/dash-core-components/store)

So it looks like this,

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

#callback
@app.callback(Output('data-store', 'data'),[
    Input('data-store','modified_timestamp'),
], [State('data-store','data')])
def update_data(timestamp,df):
    df=pd.read_csv(
        'data/processed_data.csv',
    )
    return df.reset_index().drop('index',axis=1).to_json()

#callback to access the data and do something
@app.callback(
    Output('something','children'),
    [Input('data-store','data')],
)
def do_something(data):
    # do something with the data and return the output. 
    return something
```
