我是 Dash 的新手,我需要一些帮助。我需要创建动态数量的输入,但我不知道如何为每个 slider 组件绑定(bind)动态数量的回调。我只能创建动态数量的 slider ,但正如您在代码中看到的,我需要硬编码回调的数量。有没有更好的方法来让它更有活力?
@app.callback(
Output('slider-container', 'children'),
[Input('button', 'n_clicks')])
def add_sliders(n_clicks):
return \
html.Div([
html.Div([
html.Div(dcc.Slider(id='slider-{}'.format(i))),
dcc.Input(
id='input-{}'.format(i),
placeholder='Graph Name',
type='text',
value=''
),
html.Div(id='output-{}'.format(i), style={'marginTop': 30})
]) for i in range(n_clicks)]
)
# up to 10 sliders
for i in range(10):
@app.callback(
Output('slider-{}'.format(i), 'children'),
[Input('slider-{}'.format(i), 'value'),
Input('input-{}'.format(i), 'value')])
def update_output(slider_i_value, input_i_value):
return str(slider_i_value) + str(input_i_value)
最佳答案
从 Dash 1.11.0 开始,您拥有 Pattern Matching Callbacks就是这样做的。在您的情况下,您将使用带有 MATCH
的那个.
关于python - 在 Dash 框架中动态添加 HTML 输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50961759/