如何在单个列表中获得动态选择的下拉列表的所有选择的值?

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

如何在单个列表中获得动态选择的下拉菜单的所有值?我尝试使用for循环迭代在回调内部进行回调,但是无法获取所需的列表。

def a_function的问题,其中存在回调内部的回调。

如何获得动态更新的多个下拉列表的单个列表?

import dash
import dash_core_components as dcc
import dash_html_components as html

step = html.Div(
        children=[
            "Menu:",
            dcc.Dropdown(options=[{'label': v, 'value': v} for v in ['option1', 'option2', 'option3']])
        ])

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__,external_stylesheets=external_stylesheets)
app.config['suppress_callback_exceptions']=True

div_list = [step]

app.layout = html.Div(
    children=[
        html.H1(children='Hello Dash'),
        html.Div(id='step_list', children=div_list),
        html.Div(id='local'),
        html.Button('Add Step', id='add_step_button', n_clicks_timestamp='0'),
        html.Button('Remove Step', id='remove_step_button', n_clicks_timestamp='0'),
        html.Div(id='tester_div'),
        html.Div(id='tester_div_2')])

@app.callback(
    [dash.dependencies.Output('step_list', 'children'),
    dash.dependencies.Output('local','value')],
    [dash.dependencies.Input('add_step_button', 'n_clicks_timestamp'),
    dash.dependencies.Input('add_step_button', 'n_clicks'),
     dash.dependencies.Input('remove_step_button', 'n_clicks_timestamp')],
    [dash.dependencies.State('step_list', 'children')])
def add_step(add_ts, clicks, remove_ts, div_list):
    add_ts = int(add_ts)
    remove_ts = int(remove_ts)
    if add_ts > 0 and add_ts > remove_ts and len(div_list) < 4:
        div_list += [html.Div(children=[
            "Menu:",
            dcc.Dropdown(id='dropdown_id_{}'.format(clicks), options=[{'label': v, 'value': v} for v in ['select1', 'select2', 'select3']])
        ])]
    if len(div_list) > 1 and remove_ts > add_ts:
        div_list = div_list[:-1]
    return div_list,len(div_list)


@app.callback(
    dash.dependencies.Output('tester_div', 'children'),
    [dash.dependencies.Input('local', 'value')])
def a_function(value):
    all_output = []
    if value:
        for i in range(1,value+1):
            @app.callback(dash.dependencies.Output('tester_div_2','children'),
                          [dash.dependencies.Input('dropdown_id_{}'.format(i), 'value')])
            def drop_output(valued):
                all_output.append(valued)
    return all_output

if __name__ == '__main__':
    app.run_server(debug=False)

在此图像中,输出应在这样的列表中:[option2,select1]

在此图像中,输出应在这样的列表中:[option3,select3,select1,select2]

python dynamic callback output plotly-dash
1个回答
0
投票

Dash不适用于其ID在布局的初始构建中不存在的组件。如果这样的动态添加组件仅在页面首次加载时已经在页面上存在(可能是稍后显示的隐藏组件)时才起作用。据我所知,由于类似的原因,回调中的回调不会起作用,但是您可以在循环中创建回调。

[您需要重新调整布局的设置,如果需要更多帮助,还可以编辑帖子。

© www.soinside.com 2019 - 2024. All rights reserved.