Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically adding HTML inputs in Dash framework

I am new to Dash and I am in need of some assistance. I need to create a dynamic number of inputs, but I have no idea how to bind a dynamic number of callback for each slider component. I am only able to create a dynamic number of sliders, but as you can see in the code, I need to hard code the number of callbacks. Is there a better way to to this so that it is more dynamic?

@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)
like image 435
LearningDash Avatar asked Jun 21 '18 06:06

LearningDash


People also ask

Can we add multiple input components to a dash callback function?

In Dash, any "output" can have multiple "input" components. Here's a simple example that binds five inputs (the value property of two dcc.

Does dash use HTML?

Dash is a web application framework that provides pure Python abstraction around HTML, CSS, and JavaScript. Instead of writing HTML or using an HTML templating engine, you compose your layout using Python with the Dash HTML Components module ( dash. html ).

Is CSS a dash component?

Dash's component libraries, like dash_core_components and dash_html_components , are bundled with JavaScript and CSS files. Dash automatically checks with component libraries are being used in your application and will automatically serve these files in order to render the application.

Is @app_callback is the callback decorator?

2 . @app_callback is the callback decorator .


1 Answers

As of Dash 1.11.0 you have Pattern Matching Callbacks that do just this. In your case you'd use the one with the MATCH.

like image 57
houtanb Avatar answered Nov 08 '22 12:11

houtanb