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)
In Dash, any "output" can have multiple "input" components. Here's a simple example that binds five inputs (the value property of two dcc.
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 ).
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.
2 . @app_callback is the callback decorator .
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With