I am using the below given code to remove the dropdown option when selecting a specific value (Delta Emax and Delta IC50
) in another select option. The code does remove the dropdown option but still keep giving the label=Select drug
on the display page. I want to remove both label and dropdown option when I am selecting else
option. And have both label and dropdown when selecting if
drug_options = [{"label": drug, "value": drug} for drug in sorted(set(all_drugs))]
dbc.Label("Select drug:"),
dcc.Dropdown(id="drug_selector", options=drug_options, placeholder="All"),
@app.callback(Output("drug_selector", "style"), Input("x_selector2", "value"))
def update_controls(selected_x):
if selected_x in ["Delta Emax", "Delta IC50"]:
return {}
else:
return {'display': 'none'}
CodePudding user response:
There are two ways you can do it depending upon the use case.
You can try putting both Label and Dropdown components inside a Div component and change the styling for that Div, like this:
drug_options = [{"label": drug, "value": drug} for drug in sorted(set(all_drugs))]
html.Div(id='remove-or-not',
children=[
dbc.Label("Select drug:"),
dcc.Dropdown(id="drug_selector", options=drug_options, placeholder="All"),
]
)
@app.callback(Output("remove-or-not", "style"), Input("x_selector2", "value"))
def update_controls(selected_x):
if selected_x in ["Delta Emax", "Delta IC50"]:
return {}
else:
return {'display': 'none'}
Or you could give an Id to Label component and add it inside the callback too. This way is more suitable if you want to handle the styling for each component differently:
drug_options = [{"label": drug, "value": drug} for drug in sorted(set(all_drugs))]
dbc.Label(id='label',children=["Select drug:"]),
dcc.Dropdown(id="drug_selector", options=drug_options, placeholder="All"),
@app.callback(
[Output("drug_selector", "style"),Output('label','style')],
Input("x_selector2", "value"))
def update_controls(selected_x):
if selected_x in ["Delta Emax", "Delta IC50"]:
return [{},{}]
else:
return [{'display': 'none'},{'display': 'none'}]