在回调执行期间禁用 Plotly Dash 组件,然后在回调完成后重新启用它

问题描述 投票:0回答:1

我有 Plotly Dash 组件,需要在触发回调后禁用。回调完成运行后需要启用相同的组件。我已经尝试过下面的方法。虽然组件被禁用,但回调函数完成后并没有启用。


from dash.exceptions import PreventUpdate
from dash_extensions.enrich import (
    Output,
    DashProxy,
    html,
    Input,
    MultiplexerTransform,
)
from time import sleep

app = DashProxy(__name__, transforms=[MultiplexerTransform()])

app.layout = html.Div(
    [
        html.Button(id="button", children="input-button", style={"color": "#FF0000"}),
        html.Div(id="trigger", children=None, style={"display": "none"})
    ],
)

@app.callback(
    [
        Output("button", "disabled"),
        Output("button", "style"),
        Output("trigger", "children"),
    ],
    [
        Input("trigger", "children")
    ],
    prevent_initial_call=True
)
def enable_components_after_export(trigger):
    if trigger == 1:
        return [
            False, {"color": "#FF0000"}, 0
        ]

    raise PreventUpdate


@app.callback(
    [
        Output("button", "disabled"),
        Output("button", "style")
    ],
    [
        Input("button", "n_clicks"),
    ],
    prevent_initial_call=True
)
def disable_components_on_export_button_click(button):
    if (button is not None and button > 0):
        return [
            True, {"color": "#808080"}
        ]

    raise PreventUpdate

@app.callback(
    [
        Output("trigger", "children")
    ],
    [
        Input("button", "n_clicks"),
    ],
    prevent_initial_call=True
)
def callback_function(button):
    if button is not None and button > 0:
        sleep(5)
        return [1]
    
    raise PreventUpdate

if __name__ == "__main__":
    app.run(debug=False)

在回调期间禁用组件,然后在完成回调后重新启用它们的正确方法是什么?另外,我记得 Dash 不支持多个回调输出,这就是使用 DashProxy 和 MultiplexerTransform 的原因,尽管我不确定这是否导致了我的问题。

套餐版本

dash                      2.12.1                   pypi_0    pypi       
dash-bootstrap-components 1.5.0                    pypi_0    pypi       
dash-core-components      2.0.0                    pypi_0    pypi       
dash-extensions           0.1.11                   pypi_0    pypi       
dash-html-components      2.0.0                    pypi_0    pypi       
dash-renderer             1.9.1                    pypi_0    pypi 
python user-interface callback plotly-dash
1个回答
0
投票

我不确定这里使用

DashProxy
是否可以解决问题,但无论如何,由于 2.9 Dash 版本允许 重复回调输出 :多个回调可以针对相同的输出,唯一的事情就是指定
allow_duplicate=True
在回调输出中,例如。

@app.callback(
    [
        Output("button", "disabled", allow_duplicate=True),
        Output("button", "style", allow_duplicate=True),
        Output("trigger", "children", allow_duplicate=True),
    ],
    [
        Input("trigger", "children")
    ],
    prevent_initial_call=True
)

请注意,您也可以使用简单的 后台回调 来实现此目的(即一个回调而不是三个)。

您可以通过设置来配置回调在后台运行

background=True
回调。使用
background=True
进行回调 使用您配置的后端管理器来运行回调逻辑。您可以将此管理器实例作为
dash.Dash
关键字参数提供给
background_callback_manager
应用程序构造函数,或作为
manager
装饰器的
@dash.callback
参数。

这个想法是使用

running
参数,它允许根据回调状态设置组件属性(即在回调运行时禁用按钮):

@app.callback(
    Output("trigger", "children"),
    Input("button", "n_clicks"),
    background=True,
    running=[
        (Output("button", "disabled"), True, False),
        (Output("button", "style"), {"color": "#808080"}, {"color": "#FF0000"}),
    ],
    prevent_initial_call=True
)
def callback_function(button):
    if button is not None and button > 0:
        time.sleep(5)
        return [1]
    
    raise PreventUpdate
© www.soinside.com 2019 - 2024. All rights reserved.