当在Python Dash中选择一个选项时,动态更改下拉选项

发布于 2025-02-13 22:27:46 字数 516 浏览 1 评论 0原文

我有4个值的下拉值,['all','a','b','c'],我可以选择其中的许多。

dcc.Dropdown(
    id='test',
    options=[{'label': i, 'value': i} for i in ['All', 'a', 'b', 'c']],
    value='All',
    clearable=False,
    multi=True,
),

现在,是否可以动态更改选项,以便在使用“全部”时,将自动删除所有其他选定的条目? 类似的内容:

@app.callback(
    Output('test', 'options'),
    Input('test', 'value'),
)
def update_ticker_options(value):
    if 'All' in value:
        ticker = 'All'
    return ticker

该应用程序不是从此代码开始的,因此它不起作用。

I have a dropdown of 4 values, ['All', 'a', 'b', 'c'], where I can select many of them.

dcc.Dropdown(
    id='test',
    options=[{'label': i, 'value': i} for i in ['All', 'a', 'b', 'c']],
    value='All',
    clearable=False,
    multi=True,
),

Now, is it possible to dynamically change the options, such that when 'All' is used, all other selected entries are removed automatically?
Something like this:

@app.callback(
    Output('test', 'options'),
    Input('test', 'value'),
)
def update_ticker_options(value):
    if 'All' in value:
        ticker = 'All'
    return ticker

The app is not starting with this code, so it is not working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

美羊羊 2025-02-20 22:27:47

谢谢易卜拉欣,

我将您的答案进行了进一步修改,因为我希望它显示“全部”,而不是所有其他价值观。该代码可以完成工作:

app.layout = html.Div(
    dcc.Dropdown(
        id='test',
        options=[{'label': i, 'value': i} for i in ['All', 'aaa', 'bbb', 'ccc']],
        value='All',
        clearable=False,
        multi=True,
    ),
)


@app.callback(
    Output('test', 'value'),
    Input('test', 'value'),
)
def update_ticker_options(value):
    ticker = value
    if 'All' in value and value[0] != 'All':
        ticker = ['All']
    elif 'All' in value:
        ticker = value[1]
    return ticker

Thank you Ibrahim,

I modified your answer even a bit further, since I want it to show 'All' and not all other values. This code does the work:

app.layout = html.Div(
    dcc.Dropdown(
        id='test',
        options=[{'label': i, 'value': i} for i in ['All', 'aaa', 'bbb', 'ccc']],
        value='All',
        clearable=False,
        multi=True,
    ),
)


@app.callback(
    Output('test', 'value'),
    Input('test', 'value'),
)
def update_ticker_options(value):
    ticker = value
    if 'All' in value and value[0] != 'All':
        ticker = ['All']
    elif 'All' in value:
        ticker = value[1]
    return ticker
ˉ厌 2025-02-20 22:27:47

我根据所需的功能编辑了您的示例代码。基本上,您需要的是将输出参数更改为value,如下所示。应该发生的事情是,当选择所有被选中时,所有其他选项都应添加我在列表中作为示例中的所有选项。该列表应返回到下拉列表中的value参数。

app.layout = html.Div(children=[
            dcc.Dropdown(
                id='dropdown',
                options=[{'label': i, 'value': i} for i in ['All', 'a', 'b', 'c']],
                value='a',
                clearable=False,
                multi=True,
            ),
])

@app.callback(
    Output('dropdown', 'value'),
    Input('dropdown', 'value'),
)
def update_ticker_options(value):
    if 'All' in value:
        value = ['a', 'b', 'c']
    return value

I edited your example code based on the functionality you need. Basically what you need is to change the Output parameter to be value as shown below. What should happen is when all is selected all the other options should be added which I included in a list as an example. This list should be returned to the value parameter in the dropdown.

app.layout = html.Div(children=[
            dcc.Dropdown(
                id='dropdown',
                options=[{'label': i, 'value': i} for i in ['All', 'a', 'b', 'c']],
                value='a',
                clearable=False,
                multi=True,
            ),
])

@app.callback(
    Output('dropdown', 'value'),
    Input('dropdown', 'value'),
)
def update_ticker_options(value):
    if 'All' in value:
        value = ['a', 'b', 'c']
    return value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文