dash组件间隔属性n_intervals值在回调中显示出不同的显示
我有这个简短的代码,该代码使用dcc.interval组件在回调中更新文本,在那里我尝试打印出N_intervals属性的值。
from dash import Dash, html, dcc, Input, Output
app = Dash()
dccinterval = dcc.Interval(
id='interval-component',
interval=1000,
n_intervals=0
)
app.layout = html.Div(
children=[
html.Div(id='live-update-text'),
dccinterval
]
)
@app.callback(Output('live-update-text', 'children'),
Input(dccinterval, 'n_intervals'))
def update_message(n):
print('current interval in update_message: ', dccinterval.n_intervals)
print('current n in update_message: ', n)
return 'Message: ' + str(n)
if __name__ == '__main__':
app.run_server(debug=True, port=8058)
这是输出:
current interval in update_message: 0
current n in update_message: 0
current interval in update_message: 0
current n in update_message: 1
current interval in update_message: 0
current n in update_message: 2
current interval in update_message: 0
current n in update_message: 3
.
.
.
正如您可能会看到的那样,N_intervals通过输入获得的值正确变化,但是该值直接从间隔组件总是以0为0。我想知道为什么。有人可以帮我吗?我需要弄清楚的原因是,在我的应用程序中,当我尝试通过组件更改N_intervals的值时。也就是说,当我尝试通过执行dccinterval.n_intervals = number
来设置值时,它不起作用。
非常感谢您的任何评论!
I have this short code that use the dcc.Interval component to update a text in the callback, where I tried to print out the value of the n_intervals property.
from dash import Dash, html, dcc, Input, Output
app = Dash()
dccinterval = dcc.Interval(
id='interval-component',
interval=1000,
n_intervals=0
)
app.layout = html.Div(
children=[
html.Div(id='live-update-text'),
dccinterval
]
)
@app.callback(Output('live-update-text', 'children'),
Input(dccinterval, 'n_intervals'))
def update_message(n):
print('current interval in update_message: ', dccinterval.n_intervals)
print('current n in update_message: ', n)
return 'Message: ' + str(n)
if __name__ == '__main__':
app.run_server(debug=True, port=8058)
Here is the output:
current interval in update_message: 0
current n in update_message: 0
current interval in update_message: 0
current n in update_message: 1
current interval in update_message: 0
current n in update_message: 2
current interval in update_message: 0
current n in update_message: 3
.
.
.
As you might see that the value of n_intervals got via the Input is changing correctly, but the value got directly from the interval component always stays at 0. I am wondering why. Can someone please help me with this? The reason I need to figure this out is that in my application when I tried to change the n_intervals’ value via component fails. That is, when I tried to set the value by doing dccinterval.n_intervals = a number
, it does not work.
Thank you very much for any comments!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
dccinterval.n_intervals
奇怪地显示出来,因为这不是使用间隔组件的正确方法。另一种方式,使用n
,即代表Input
间隔的函数参数是正确的方法。但是,您已将输入定义
有些错误。与其引用组件的变量,即dccinterval
,需要引用该组件的id
,如以下:如果要更新间隔的值,则需要设置一个回调,将其用作
output
之类的回调:函数将
return
要将间隔更新为哪个值。编辑:
因此,错误的方法不起作用是因为您在布局中声明的变量,
dccinterval
在这种情况下,一旦应用程序运行,就不会包含所有详细信息。在这种情况下,回调函数的参数,n
具有正确的信息,因为它已通过dash进行了更新。这与仪表板的工作原理有关,以及 - 更深入地 - 反应在引擎盖下工作。dccinterval.n_intervals
is showing up strangely because that's not the correct way to use the interval component. The other way, usingn
, which is the function argument representing the interval from theInput
, is the correct way. However, you've defined yourInput
a bit incorrectly. Rather than referencing the variable for the component, which isdccinterval
, you need to reference theid
of that component, like this:If you want to update the interval's value, you need to set up a callback that uses it as an
Output
like this:and the function will
return
whichever value you want to update the interval to.Edit:
So, the reason the wrong way doesn't work is because the variable you declare in the layout,
dccinterval
in this case, doesn't contain all of the details once the app is running. The callback function's argument,n
in this case, has the right info because it has been updated through Dash. This is related to how Dash works, and how - more deeply - React works under the hood.