我需要一点帮助来理解模式匹配。我正在尝试根据下拉菜单中分类选择的数量生成输入框(或滑块,没关系)。我可以让一些代码工作,但我似乎不能使用这些值。
问题1:当我从菜单中选择对象时,输入框不出现
问题2:输出似乎不能正常工作
问题3:我不知道如何保存和使用每个框中输入的值
我希望从每个输入框中输入的值生成一个像[1,3,5,9]这样的数组,我可以使用它进行后续计算或共享到另一个回调。
提前感谢您的帮助!
from dash import Dash, dcc, html, Input, Output, State, MATCH, ALL
app = Dash(__name__, suppress_callback_exceptions=True)
cols = ['apple', 'orange', 'lemon', 'pear']
app.layout = html.Div([
dcc.Dropdown(id='add-filter', multi=True, options=[{'label': i, 'value': i} for i in cols]),
html.Div(id='dropdown-container', children=[]),
html.Div(id='dropdown-container-output')
])
@app.callback(
Output('dropdown-container', 'children'),
Input('add-filter', 'value'),
State('dropdown-container', 'children'))
def display_inputs(values, children):
n = len(values)
new_inputs = [dcc.Input(type='number', id={'type':'filter-dropdown', 'index':i})
for i in range(n)
]
children.append(new_inputs)
return children
@app.callback(
Output('dropdown-container-output', 'children'),
Input({'type': 'filter-dropdown', 'index': ALL}, 'value')
)
def display_output(values):
return html.Div([
html.Div('Dropdown {} = {}'.format(i + 1, value))
for (i, value) in enumerate(values)
])
if __name__ == '__main__':
app.run_server(debug=True)
from dash import Dash, dcc, html, Input, Output, State, MATCH, ALL
app = Dash(__name__, suppress_callback_exceptions=True)
cols = ['apple', 'orange', 'lemon', 'pear']
app.layout = html.Div([
dcc.Dropdown(id='add-filter', multi=True, options=[{'label': i, 'value': i} for i in cols]),
html.Div(id='dropdown-container', children=[]),
html.Div(id='dropdown-container-output')
])
@app.callback(
Output('dropdown-container', 'children'),
Input('add-filter', 'value'),
State('dropdown-container', 'children'))
def display_inputs(values, children):
n = len(values)
print(values)
return [dcc.Input(type='number', id={'type':'filter-dropdown', 'index':i})
#return [dcc.Slider(0,5,1, id={'type':'filter-dropdown', 'index':i})
for i in range(n)
]
@app.callback(
Output('dropdown-container-output', 'children'),
Input({'type': 'filter-dropdown', 'index': ALL}, 'value')
)
def display_output(values):
a = []
for (i, value) in enumerate(values):
a.append(value)
print(a)
return html.Div([
html.Div('Dropdown {} = {}'.format(i + 1, value))
for (i, value) in enumerate(values)
])
if __name__ == '__main__':
app.run_server(debug=True)