Refresh Input Data

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

Have you checked this?

https://plot.ly/dash/live-updates

1 Like

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

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: