小编Lea*_*ash的帖子

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

我是 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)

html python dynamic python-2.7 plotly-dash

8
推荐指数
1
解决办法
1180
查看次数

标签 统计

dynamic ×1

html ×1

plotly-dash ×1

python ×1

python-2.7 ×1