[英]Python Dash - callback value from another file
I create a dashboard based on flask and dash python我基于 flask 和破折号 python 创建了一个仪表板
How can I refresh data pulled from another .py
file with a specified interval?如何以指定的时间间隔刷新从另一个
.py
文件中提取的数据? I have a weather_api.py file that returns data from openweathermap API and I refer to it such as:我有一个 weather_api.py 文件,它从 openweathermap API 返回数据,我引用它如:
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 eg: html.Span("{}".format(WEATHER_API.weather_description))
在文件的开头,我导入
WEATHER_API
- 然后它将引用变量,例如: html.Span("{}".format(WEATHER_API.weather_description))
I would like this data to be refreshed every eg 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.我希望这些数据每隔 10 分钟刷新一次——现在它可以工作,所以数据只是从头开始——只要我运行 Dash,它就会从 API 中“拉”一次数据。 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?是否有可能以某种方式设置
@callback
以每 10 分钟从WEATHER_API
检索数据 - 就像每 10 分钟启动WEATHER_API.py
一样?
Yes.是的。 You need the interval component.
您需要间隔组件。
Here are some examples of it in use to provide live updates.以下是它用于提供实时更新的一些示例。
I found a solution - you can easily return a function value from another script - all you need is a proper reference:我找到了一个解决方案 - 您可以轻松地从另一个脚本返回 function 值 - 您只需要一个正确的参考:
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一个有用的例子: https://dash.plotly.com/live-updates
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.