如何在选择另一个下拉值时删除一个下拉值,反之亦然?

how to delete value of one dropdown when the other dropdown value is selected and vice versa?

我有两个使用 dcc.Dropdown() 的下拉菜单。如果我 select 从下拉菜单中选择一个值,我想取消 select 另一个下拉菜单中的任何内容,反之亦然。

这里是我定义两个回调的方法,当一个下拉值被 selected 时,每个回调将另一个下拉值的值设置为空。我收到循环依赖错误的错误。感谢您的帮助。

例如,我有两个下拉列表,dts-filename-dropdown 和 dts-longterm-filename-dropdown。

@app.callback(
    [
    Output("dts-filename-dropdown", "value")
    ],
    [
     Input("dts-longterm-filename-dropdown", "value")
    ],
)
def close_tfo_dts_filename(dts_longterm_filename):
    if dts_longterm_filename is not None:
        dts_filename=[]
        return dts_filename
    else:  
        raise dash.exceptions.PreventUpdate
        
        
@app.callback(
    [
    Output("dts-longterm-filename-dropdown", "value")
    ],
    [
     Input("dts-filename-dropdown", "value")
    ],
)
def close_longterm_dts_filename(dts_longterm_filename):
    if dts_filename is not None:
        dts_longterm_filename=[]
        return dts_longterm_filename
    else:  
        raise dash.exceptions.PreventUpdate

您可以在输入和输出相同的地方使用 circular callbacks。这是您的案例的示例:

@app.callback(
    [
        Output("dts-longterm-filename-dropdown", "value"),
        Output("dts-filename-dropdown", "value")
    ],
    [
        Input("dts-longterm-filename-dropdown", "value"),
        Input("dts-filename-dropdown", "value"),
    ],
)
def close_longterm_dts_filename(dts_longterm_filename, dts_filename):
    ctx = dash.callback_context

    if not ctx.triggered:
        raise PreventUpdate
    else:
        button_id = ctx.triggered[0]['prop_id'].split('.')[0]
        if button_id == "dts-filename-dropdown":
            return [], dts_filename
        else:
            return dts_longterm_filename, []