print_subject should print the subject name and not its associated ID number.
Given Dash’s current implementation, I could probably get the label by adding subject_dropdown’s options as a State to the callback and then selecting the label by matching the value.
Is there an easier way to do this? Am I missing something?
ah ok…based on that, and without any other insight into your code, your solution to pass the dropdown’s options as a state parameter is probably the best. Or is it easier to alter your query to use the label vice an index #?
I think the only option is doing it with State, as mentioned above. For example:
app.layout = html.Div(
children = [
html.Div(id="print_subject"),
dcc.Dropdown(
id = "subject_dropdown",
options = [{'label':'Biology','value':1},{label:'Physics','value':2}],
),
]
)
@app.callback(
Output("print_subject","children"),
[Input("subject_dropdown","value")],
[State("subject_dropdown","options")]
)
def print_the_label(value_chosen, opt):
print(opt) # to see what you're dealing with
the_label = [x['label'] for x in opt if x['value'] == value_chosen]
return the_label[0]