Roee Anuar
Roee Anuar

Reputation: 3436

A timer event for dash in Python

I am using Dash to build a GUI for sending data to a remote server. The update process is asynchronic - as the data could take time to load. After the user presses a button on the dash GUI it saves the data on the remote server as a CSV file, and once the file is saved it tells the server (using an API) to start running the upload process. I would like to show the user the progress of the upload using a callback that will fire every 5 seconds. I couldn't find any documentation on Dash's web site regarding this type of callback. Is there any method for firing a callback every X seconds?

Upvotes: 3

Views: 8414

Answers (1)

Roee Anuar
Roee Anuar

Reputation: 3436

I found the answer on Dash's forums. The element is called an "interval".

Here is a working example which triggers an event every 5 seconds:

import dash_core_components as dcc
import dash_html_components as html
import dash

app = dash.Dash()

app.layout = html.Div([
    dcc.Interval(id='interval1', interval=5 * 1000, n_intervals=0),
    html.H1(id='label1', children='')
])


@app.callback(dash.dependencies.Output('label1', 'children'),
    [dash.dependencies.Input('interval1', 'n_intervals')])
def update_interval(n):
    return 'Intervals Passed: ' + str(n)

app.run_server(debug=False, port=8050)

Upvotes: 14

Related Questions