在 Dash 框架中动态添加 HTML 输入

Lea*_*ash 8 html python dynamic python-2.7 plotly-dash

我是 Dash 的新手,我需要一些帮助。我需要创建一个动态数量的输入,但我不知道如何为每个滑块组件绑定一个动态数量的回调。我只能创建动态数量的滑块,但正如您在代码中看到的,我需要对回调的数量进行硬编码。有没有更好的方法来使它更具动态性?

@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)
Run Code Online (Sandbox Code Playgroud)

hou*_*anb 1

从 Dash 1.11.0 开始,您可以使用模式匹配回调来执行此操作。在你的情况下,你会使用带有MATCH.