Hi there,
I’m somewhat new to dash and python so forgive me and let me know if there’s something that could be simplified or needs to be changed.
Problem
I want to display a number in text and update that number every 10 minutes. I have successfully done this, but the problem is that I want it to load immediately and then update every ten minutes… but then it takes 10 minutes to load this text when the application is first started.
I have a feeling that I need to add a while/if for the callback but I’m not sure. Here is a link to the result
See below for the code
#Imports for dash
import dash
from dash.dependencies import Output, Event
import dash_core_components as dcc
import dash_html_components as html
#imports for database connection and data processing
from sshtunnel import SSHTunnelForwarder
import pymysql as db
import pandas as pd
# ssh variables go here
host =
localhost =
ssh_username =
private_key =
# database variables go here
user=
password=
database=
#This is the function for processing a query
def query(q):
#function for getting data from database
def get_data():
df_test = query('call example') #calls a procedure which returns a number
data = df_test['test'].loc[0] # selects the result from the dataframe
data_insert = 'This month we have {} new clients!!!'.format(data) #inserts the result into the string
return data_insert
app = dash.Dash(__name__)
app.layout = html.Div(
[
html.Div(id='my-div', children='''
'''.format(get_data())),
dcc.Interval(
id='update',
interval=10000 # I would like for this to be 10 minutes
)
]
)
@app.callback(Output('my-div', 'children'),
events=[Event('update', 'interval')])
def update_data(): # function for returning the new data
return get_data()
if __name__ == '__main__':
app.run_server(debug=True)