python - Dash - 间隔不调用回调
问题描述
我正在使用 Dash - Plot.ly 创建仪表板,我需要定期更新。我找到了dcc.Interval()
应该完成这项工作的组件,但是发生了一种奇怪的行为。如果代码正确,回调只会被调用一次,如果代码中出现错误,比如引用不存在的变量,就会出现循环行为。对可能出什么问题有任何想法吗?
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly
from dash.dependencies import Input, Output
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout=html.Div([dcc.Interval(id='interval-component',
interval=1*1000, n_intervals=0)], id="acoolId")
@app.callback(
Output('acoolId', 'children'),
[Input('interval-component', 'n_intervals')])
def timer(n):
# print(asdlol) # if this line is enabled, the periodic behavior happens
return [html.P("ASD " + str(n))]
if __name__ == '__main__':
app.run_server(debug=True)
解决方案
问题是您的回调正在用children
ID 替换元素的"acoolID"
,这就是您的interval
组件所在的位置。因此,回调触发一次,并替换回调的输入,使其无法再次触发。
将您的布局更改为这样的内容,以便children
您更新的是不同的组件:
app.layout = html.Div(
children=[
dcc.Interval(id='interval-component',
interval=1 * 1000,
n_intervals=0),
html.Div(id="acoolId", children=[]),
]
)
我已经对此进行了测试,回调现在可以正常工作。
推荐阅读
- node.js - docker 容器中的 Npm 安装找不到我的自定义注册表
- python - 通过 python subprocess ssh 在远程机器上运行 GUI 程序
- android - 如何使用 Material Design 主题制作圆形按钮?
- rest - Apache Ignite:可以将自定义 Rest Method 部署到服务网格
- c++ - c++类型转换类成员运算符问题
- linux - 安装 Steam 后系统监视器不显示 CPU 历史记录、网络历史记录?
- three.js - 不透明对象上的 Three.js 透明度伪影
- raspberry-pi - 在没有互联网连接的情况下在本地运行 Home Assistant (HASSIO)
- networkx - Networkx Dijkstra Shortest Path 存在,但太长了 - 预先给我一个近似值的算法
- laravel - Laravel 护照与 Laravel Sactum