# Refresh Input Data

**URL:** <https://community.plotly.com/t/refresh-input-data/5436>\
**Category:** Dash Python\
**Created:** [August 17, 2017, 12:24pm UTC](https://community.plotly.com/t/refresh-input-data/5436 "2017-08-17T12:24:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sra1T](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@Sra1T](https://community.plotly.com/u/Sra1T)\
**Post date:** [August 17, 2017, 12:24pm UTC](https://community.plotly.com/t/refresh-input-data/5436/1 "2017-08-17T12:24:24Z")

</div>

**How to refresh my Dash app data with latest data set?**

Problem Statement:

1. I connect to a Database/csv (which changes with time)
2. Plot the Data
3. Refresh the data on every page reload (to accommodate new entries)
4. To provide Live dashboards

---

<div class="post-metadata">

**Author:** ![raghunath](https://avatars.discourse-cdn.com/v4/letter/r/7993a0/32.png) [@raghunath](https://community.plotly.com/u/raghunath)\
**Post date:** [August 17, 2017, 12:35pm UTC](https://community.plotly.com/t/refresh-input-data/5436/2 "2017-08-17T12:35:05Z")

</div>

Have you checked this?

[https://plot.ly/dash/live-updates](https://plot.ly/dash/live-updates)

---

<div class="post-metadata">

**Author:** ![Sra1T](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@Sra1T](https://community.plotly.com/u/Sra1T)\
**Post date:** [August 17, 2017, 12:59pm UTC](https://community.plotly.com/t/refresh-input-data/5436/4 "2017-08-17T12:59:43Z")

</div>

thanks raghunath, I have checked that part, here is my template

```
> import dash
> import dash_core_components as dcc
> import dash_html_components as html
> import pandas as pd
> import numpy as np
> from plotly import __version__
> from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
> import plotly.figure_factory as ff
> import plotly.graph_objs as go
> import urllib
> import urllib.parse
> import Levenshtein
> import datetime as dt
> from dateutil.relativedelta import *
> from pymongo import MongoClient
> import locale
> locale.setlocale(locale.LC_MONETARY, 'en_IN')
> 
> 
> app = dash.Dash()
> 
> 
> 
> client = MongoClient('xxxxx') #production
> db=client['yyyyyyy']
> data=pd.DataFrame()
> df=pd.DataFrame()
> pdf=pd.DataFrame()
> data2correct=pd.DataFrame()
> collection=db.lr
> 
> def data_in():
> #data read and filling the global variables df,pdf,data and data2correct happens here
> 
> data_in()
> 
> def serve_layout():
> data_in()
> return html.Div([
> html.Div([
> html.H1('The time is: ' + str(dt.datetime.now())),
> html.Label('Date Range'),
>         
> html.Label('From'),
>         
> html.Div([
> dcc.Input(
> id='SD',
> type='Date',
> value=dt.date.today()-dt.timedelta(days=15)
> #SD=dt.date.today()-dt.timedelta(days=15)
> )
> ],
> style={'width': '48%', 
> 'margin-right':'20%', 
> 'margin-top':'2%', 
> 'display': 'inline-block'}),
> 
> html.Label('Till'),
>         
> html.Div([
> dcc.Input(
> id='ED',
> type='Date',
> value=dt.date.today()
> #ED=dt.date.today()
> )
> ],
> style={'width': '48%', 
> 'margin-right':'20%', 
> 'margin-top':'2%', 
> 'display': 'inline-block'})
> 
> ]),
>     
> #html.Button('Click Me', id='my-button'),
> 
> html.Hr(),
>     
> html.Label('Summary'),
>     
> dcc.Graph(id='fig1'),
> #dcc.Graph(id='Tbl'),
> dcc.Graph(id='fig2'),
> dcc.Graph(id='fig3'),
> 
> 
> ])
> 
> app.layout = serve_layout
> app.css.append_css({"external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"})
>  
> @app.callback(
> dash.dependencies.Output('fig1', 'figure'),
> [dash.dependencies.Input('SD','value'),
> dash.dependencies.Input('ED', 'value')])
> def update_graph(SD,ED):
> metrics on df
> 
> @app.callback(
> dash.dependencies.Output('fig2', 'figure'),
> [dash.dependencies.Input('SD','value'),
> dash.dependencies.Input('ED', 'value')])
> def update_graph(SD,ED):
> metrics on pdf
> 
> 
> @app.callback(
> dash.dependencies.Output('fig3', 'figure'),
> [dash.dependencies.Input('SD','value'),
> dash.dependencies.Input('ED', 'value')])
> def update_graph(SD,ED):
> metrics on data2correct
> 
> 
> 
> 
> 
>     
> if __name__ == ' __main__':
> app.run_server(host='0.0.0.0',debug=True,use_reloader=False)

```

I’m calling the function data\_in inside serve\_layout so that it computes all the four global variables for every reload of the page and thus can be used inside in the call backs followed.

Or should I schedule a cron job to accommodate the in-time data

please suggest me what I should do to get it done

---

<div class="post-metadata">

**Author:** ![chriddyp](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/chriddyp/32/16855_2.png) [@chriddyp](https://community.plotly.com/u/chriddyp)\
**Post date:** [August 17, 2017, 2:16pm UTC](https://community.plotly.com/t/refresh-input-data/5436/5 "2017-08-17T14:16:51Z")

</div>

> [@Sra1T](#):
>
> all the four global variables for every reload of the page and thus can be used inside in the call backs followed.

It is not safe to use `global` variables in dash. Instead, I recommend storing the `df` inside a hidden `div` inside the `layout` and using that hidden `div` as its own `Input` in the remaining callback functions.  
For more information, see:

- Using a hidden div: [Add reactive expressions / blocks · Issue #49 · plotly/dash · GitHub](https://github.com/plotly/dash/issues/49#issuecomment-311511286)
- Why `global` variables aren’t safe: [App not resetting with page refresh - #7 by chriddyp](https://community.plotly.com/t/app-not-resetting-with-page-refresh/5020/7)
