dash组件间隔属性n_intervals值在回调中显示出不同的显示

发布于 2025-01-21 05:16:59 字数 1256 浏览 0 评论 0原文

我有这个简短的代码,该代码使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相守太难 2025-01-28 05:16:59

dccinterval.n_intervals奇怪地显示出来,因为这不是使用间隔组件的正确方法。另一种方式,使用n,即代表Input间隔的函数参数是正确的方法。但是,您已将输入定义有些错误。与其引用组件的变量,即dccinterval,需要引用该组件的id,如以下:

Input('interval-component', 'n_intervals')

如果要更新间隔的值,则需要设置一个回调,将其用作output之类的回调:

Output('interval-component', 'n_intervals')

函数将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, using n, which is the function argument representing the interval from the Input, is the correct way. However, you've defined your Input a bit incorrectly. Rather than referencing the variable for the component, which is dccinterval, you need to reference the id of that component, like this:

Input('interval-component', 'n_intervals')

If you want to update the interval's value, you need to set up a callback that uses it as an Output like this:

Output('interval-component', 'n_intervals')

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文