I am experiencing some unexpected behaviour with my dropdown call backs, in that the dropdowns are not being populated with the options. Instead I get a dropdown that is scrollable and appears to have the right number of options but I just can’t see them:
The four dropdowns represent four columns in my data frame and what I am trying to do is populate them with the unique values in the data frame, but at the same time dependent on the options selected in the other dropdowns. So if I selected Toyota in box 1, I should only see Toyota models in box 2. And if I select Rav4 in box 2, then only Toyota in box1 and the various different colours that apply to Rav4’s in box3 (based on data in the df) etc. (hope that makes sense!)
How I’ve tried to approach this is having four callback functions similar to this one:
@app.callback(Output('brandDropdown', 'options'),
[Input('brandDropdown', 'value'),
Input('modelDropdown', 'value'),
Input('bodyTypeDropdown', 'value'),
Input('colorDropdown', 'value')])
def brandOptions(brand, model, bodyType, color):
request = 'brandOptions'
options = dashboard_functions.dataRequest(df_data, brand, model, bodyType, color, request)
return options
The dataRequest function looks like this (extract of relevant bits):
def dataRequest(df_data, brand, model, type, color, request):`
brandSelection = []
modelSelection = []
typeSelection = []
colorSelection = []
#if no options selected then default to "All"
if brand == []:
brandSelection = df_data['Car Make'].unique()
else:
brandSelection = brand
if model == []:
modelSelection = df_data['Car Model'].unique()
else:
modelSelection = model
if type == []:
typeSelection = df_data['Car Body'].unique()
else:
typeSelection = type
if color == []:
colorSelection = df_data['Exterior Color'].unique()
else:
colorSelection = color
#filter data based on options selected in the four dropdowns
df_data = df_data.loc[df_data['Car Make'].isin(brandSelection) & \
df_data['Car Model'].isin(modelSelection) & \
df_data['Car Body'].isin(typeSelection) & \
df_data['Exterior Color'].isin(colorSelection)]
#Get latest available options for each of the dropdowns
brandOptions = df_data['Car Make'].unique()
modelOptions = df_data['Car Model'].unique()
bodyTypeOptions = df_data['Car Body'].unique()
colorOptions = df_data['Exterior Color'].unique()
if request == 'brandOptions':
print(brandOptions)
return brandOptions
Now when I first run my dash the Brand dropdown is empty (same as above screenshot) but interestingly the print statement is outputting the correct values:
['Toyota' 'Hyundai' 'Chevrolet' 'Ford' 'BMW' 'Nissan' 'Mercedes-Benz'
'Mazda' 'Honda' 'Dodge' 'Lexus' 'INFINITI' 'Subaru' 'Kia' 'Scion'
'Volkswagen']
But why are these not being fed back to my Dropdown as selectable options?
Thanks
Chris