Hi @chriddyp,
I am new to dash, right now stuck in the below code,
As in the code, there are two drop downs,
my requirement is,
a) if (dropdown1==‘A’) and (dropdown2==‘X’):
----> display slider with id=‘a’ (Requirement)
b) if (dropdown1==‘B’) and (dropdown2==‘X’):
----> display slider with id=‘b’ (Requirement)
c) And I need to display the value of selected slider. (Dynamically)
But now, with this code below, on selecting dropdown respective slider is displayed. But the value of the slider is not displayed dynamically.
Could you please help me out?
Thank you!
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input,Output
app=dash.Dash()
app.layout = html.Div([
html.Div([dcc.Dropdown(id='first-dropdown',options=[{'label': 'All', 'value': 'All'},{'label': 'A', 'value': 'A'},{'label': 'B', 'value': 'B'}, {'label': 'C', 'value': 'C'},{'label': 'D', 'value': 'D'}],value='A')],style={'width':200,'marginLeft':10,'marginTop':10}),
html.Br(),
html.Div([dcc.Dropdown(id='second-dropdown',options=[{'label': 'All', 'value': 'All'}, {'label': 'X', 'value': 'X'}, {'label': 'Y', 'value': 'Y'}, {'label': 'Z', 'value': 'Z'}],value='X')],style={'width':200,'marginLeft':10,'marginTop':10}),
html.Br(),
html.Div(id='controls-container',children=[dcc.Slider(id='a')]),
html.Div(id='controls-container1',children=[dcc.Slider(id='b')]),
html.Br(),html.Br(),
html.Div(id='disp_values')
])
@app.callback(Output('controls-container', 'children'),[Input('first-dropdown', 'value'),Input('second-dropdown', 'value')])
def toggle_container(first_dropdown,second_dropdown):
if first_dropdown == 'A' and second_dropdown == 'X':
sig_var=['a','b','c'] # Output from certain function
sig_coeff=[0.15555,0.25333,0.05222] # Output from certain function
if 'a' in sig_var:
val_var=round(sig_coeff[sig_var.index('a')],4)
min_var=round(val_var-0.1,4)
max_var=round(val_var+0.1,4)
return dcc.Slider(id='a',min=min_var, max=max_var, step=0.001, value=val_var, marks={min_var:str(min_var),val_var:str(val_var),max_var:str(max_var)})
else:
return {'display':'none'}
@app.callback(Output('controls-container1', 'children'),[Input('first-dropdown', 'value'),Input('second-dropdown', 'value')])
def toggle_container1(first_dropdown,second_dropdown):
if first_dropdown == 'B' and second_dropdown == 'X':
sig_var=['a','b','c'] # Output from certain function
sig_coeff=[0.75555,0.85333,0.08222] # Output from certain function
if 'b' in sig_var:
val_var=round(sig_coeff[sig_var.index('b')],4)
min_var=round(val_var-0.1,4)
max_var=round(val_var+0.1,4)
return dcc.Slider(id='b',min=min_var, max=max_var, step=0.001, value=val_var, marks={min_var:str(min_var),val_var:str(val_var),max_var:str(max_var)})
else:
return {'display':'none'}
@app.callback(Output('disp_values', 'children'),[Input('a', 'value'),Input('b', 'value')])
def toggle_container2(val_a,val_b):
print(val_a,val_b)
return html.Label([val_a,val_b])
app.css.append_css({'external_url': 'https://codepen.io/chriddyp/pen/bWLwgP.css'})
if __name__ == '__main__':
app.run_server(debug=False,port=8000)