我有一个应用程序,它包含一个带有回调功能的按钮,可以创建无限数量的下拉列表,它将自动标识为‘DropDown-I’. 困难在于,我似乎无法在另一个回调函数中实际使用我在这些下拉列表中输入的值(那只是try 打印它们).
我如何检索这些值,或者您将如何检索这些值?
显然,第value=dcc.Dropdown(id=dropdown_id).value
部分不起作用.
import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
app = dash.Dash(__name__)
app.layout = html.Div([
html.Button("Add Dropdown and Number Field", id="add-button"),
html.Div(id="input-container", children=[]),
html.Div(id="output"),
])
@app.callback(
Output("input-container", "children"),
Input("add-button", "n_clicks"),
State("input-container", "children")
)
def add_input(n_clicks, existing_children):
if n_clicks is None:
return existing_children
new_input = dbc.Row([
dbc.Col(dcc.Dropdown(
options=[
{'label': 'Option 1', 'value': 'option-1'},
{'label': 'Option 2', 'value': 'option-2'},
# Add more dropdown options as needed
],
value='option-1',
id=f'dropdown-{n_clicks}'
)),
dbc.Col(dcc.Input(
type='number',
value=0,
id=f'weight-{n_clicks}'
)),
])
existing_children.append(new_input)
return existing_children
@app.callback(
Output("output", "children"),
Input("add-button", "n_clicks"),
State("input-container", "children")
)
def process_dropdowns(n_clicks, dropdown_children):
if n_clicks is None:
return []
# Create a list to store the selected values from each dropdown
selected_values = []
# Iterate through the dropdowns to retrieve their values
for i, child in enumerate(dropdown_children):
dropdown_id = f'dropdown-{i+1}'
selected_value = dcc.Dropdown(id=dropdown_id).value
selected_values.append(selected_value)
# Process the selected values or use them as needed
return f"Selected Dropdown Values: {', '.join(selected_values)}"
if __name__ == "__main__":
app.run_server(debug=False)