Python Dash - 来自另一个文件的回调值

Python Dash - callback value from another file

我基于 flask 和 dash 创建了一个仪表盘 python
如何以指定的时间间隔刷新从另一个 .py 文件中提取的数据? 我有一个 weather_api.py 文件,其中 returns 来自 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))]),
        ])
])

在文件开头我导入 WEATHER_API - 然后它将引用变量,例如: html.Span("{}".format(WEATHER_API.weather_description))

我希望这些数据每次都被刷新。 10 分钟 - 现在它可以工作,所以数据只是从头开始 - 只要我 运行 Dash,它就会从 API 中“拉”数据一次。是否有可能以某种方式设置 @callback 以每 10 分钟从 WEATHER_API 检索数据 - 就好像每 10 分钟开始 WEATHER_API.py 一样?

是的。您需要 interval 组件。

这里 some examples 用于提供实时更新。

我找到了一个解决方案 - 您可以轻松地 return 来自另一个脚本的函数值 - 您只需要一个正确的参考:

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()))

一个有用的例子: https://dash.plotly.com/live-updates