Reputation: 305
I create a dashboard based on flask and dash python
How can I refresh data pulled from another .py
file with a specified interval?
I have a weather_api.py file that returns data from openweathermap API
and I refer to it such as:
html.Div(children=[
html.Div(children=[html.Img(src="http://openweathermap.org/img/wn/{}@2x.png".format(WEATHER_API.icon))]),
html.Div(children=[html.H2("{} ".format(round(WEATHER_API.current_temperature))),
html.Span(["Data", html.Br()]),
html.Span("{}".format(WEATHER_API.weather_description))]),
])
])
at the beginning of the file I import WEATHER_API
- and then it will refer to variables e.g.: html.Span("{}".format(WEATHER_API.weather_description))
I would like this data to be refreshed every e.g. 10 minutes - now it works so that the data is only from the beginning - as soon as I run the Dash, it "pulls" data from the API only once. Is it possible to somehow set up @callback
to retrieve data from WEATHER_API
every 10 minutes - as if starting WEATHER_API.py
every 10 minutes?
Upvotes: 0
Views: 2127
Reputation: 305
I found a solution - you can easily return a function value from another script - all you need is a proper reference:
import script.py
app.layout = html.Div(
[
html.Div(id="number-output"),
dcc.Interval(id='interval-component',
interval=1000, # in millisecondsn_intervals=0
),
]
)
@app.callback(Output('number-output', 'children'),
[Input('interval-component', 'n_intervals')])
def update_output(n):
return html.Span('Output: {}'.format(script.test()))
A useful example: https://dash.plotly.com/live-updates
Upvotes: 1
Reputation: 6596
Yes. You need the interval component.
Here are some examples of it in use to provide live updates.
Upvotes: 1