Hey @JoseMarqueses,
You can use “callback context” in order to fire the callback when a specific property is triggered. It uses the global variable dash.callback_context
which is available in callbacks to access the list of changed properties within a callback. You can learn more about it in the Dash documentation FAQs here.
I’ve modified your code below to use callback context to only fire the children after you press the button, I think that’s what your post above is looking for. Let me know if that’s on point. Cheers.
import dash
import dash_html_components as html
import dash_core_components as dcc
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State
import pdb
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.config.suppress_callback_exceptions = True
app.layout = html.Div([
html.Div(id='test'),
dbc.Button("Add slider", id="button"),
html.Div([
], id='range-sliders'),
html.Div(id='output-container-range-slider')
])
@app.callback(
Output('range-sliders', 'children'),
[Input('button', 'n_clicks')],
[State('range-sliders', 'children')]
)
def addslider(click, children):
print("2")
if children:
if click:
children[0]['props']['value'] = [0, 20]
return children
else:
children.append(html.Div([
dcc.RangeSlider(
id='range1',
min=0,
max=20,
step=0.5,
value=[5, 15]
),
dcc.RangeSlider(
id='range2',
min=0,
max=20,
step=0.5,
value=[5, 15]
)]))
return children
@app.callback(
Output("output-container-range-slider", "children"),
[Input("button", "n_clicks"),
Input("range-sliders", "children")],
)
def update_output(clicks, value):
print("1")
ctx = dash.callback_context
if "range-sliders.children" in ctx.triggered[0]['prop_id']:
return 'You have selected "{}"'.format(value[0]['props']['children'][0]['props']['value'])
if __name__ == '__main__':
app.run_server()