I am trying to update a Dropdown based on the value from another Dropdown.
It updates second dropdown for ‘treatments’ based on ‘department’ selected from first dropdown.
Here is the function i prepared to make options for second dropdown.
def get_diseases_from_department(department_input):
try:
# take copy of dataframe to avoid 'SettingwithCopyWarning' from pandas on every operation on dataframe
diseases_of_department = complete_data.copy()
# now get an array of unique treatments for the disease from the department input
diseases_of_department = diseases_of_department.loc[(diseases_of_department.loc[:, 'Department'] == department_input)].loc[:,
'Treatment'].unique()
# make list for options
diseases_of_department = [{'label': i, 'value': i} for i in diseases_of_department]
# if an error happens with input, return empty options.
except ValueError:
diseases_of_department = [{'label': '', 'value': ''}]
# return this now
return diseases_of_department
Here is my callback that takes input from first callback and updates options
of another callback:
@his_app.callback(Output(component_id='disease_input', component_property='options'),
[Input(component_id='department_input', component_property='value')])
def update_diseases_from_department(department_input_los):
# getting the list of dictionaries from above function
diseases_of_department = get_diseases_from_department(department_input=department_input_los)
return diseases_of_department