Bring Drag & Drop to Dash with Dashboard Engine. 💫 Learn how at our next webinar!

Load Data Immediately, then update every 10min

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.


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

#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='''
            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__':

Hey, never tried it, but have you tried to follow this exemple. Your callback doesn’t look the same as in the exemple.

I think the second section of @jajarbins link is what you want. You can assign a function to app.layout which will get called when the page loads. If you create the interval in that function, it will fire for the first time when the page loads, then continue firing every 10 minutes (or whatever you set it to).

Here’s a simple example that fires every 10 seconds:

from datetime import datetime

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

def serve_layout():
    return html.Div(
        [dcc.Interval(id="interval", interval=10000), html.P(id="output")]

app = dash.Dash()

app.layout = serve_layout

@app.callback(Output("output", "children"), [Input("interval", "n_intervals")])
def display_time(n):
    return"The time is: %H:%M:%S")

if __name__ == "__main__":
1 Like

Thanks @tcbegley and @jajarbins,

The solution slightly altered by @tcbegley worked perfectly! Although the documentation isn’t bad, it still took a while for me to comprehend everything.


I’ve been trying to get this work with a multi-page app, but have no luck. Too long a code to post. Any general idea on why it can be so?

Much appreciated,