My dropdown won’t update, I’ve tried to create a log to make sure my callback function is called, and it works. But the sub dropdown won’t update.
this is my layout file
import dash_bootstrap_components as dbc
import dash_mantine_components as dmc
import pandas as pd
from dash import dcc, html
import constants, common
def get_category(df):
categories = df['sub_name'].unique().tolist()
selected_category = categories[0]
sub_categories = df[df['sub_name'] == selected_category]['title'].unique().tolist()
category_menu1 = dmc.Select(
data=[{'label': category, 'value': category} for category in categories],
value=selected_category,
label='Category',
id='category_menu1',
searchable=True,
style=dict(width='100%')
)
sub_categories_menu1 = dmc.Select(
data=[{'label': sub_category, 'value': sub_category} for sub_category in sub_categories],
value=sub_categories[0],
label='Sub-Category',
id='sub_categories_menu1',
searchable=True,
style=dict(width='100%')
)
category_card = dmc.Card(
children=[category_menu1],
withBorder=True,
shadow="sm",
radius="md",
)
sub_category_card = dmc.Card(
children=[sub_categories_menu1],
withBorder=True,
shadow="sm",
radius="md",
)
category_col = dmc.Col(
[category_card],
xs=12, sm=12, md=6, lg=6, xl=6,
style=dict()
)
sub_category_col = dmc.Col(
[sub_category_card],
xs=12, sm=12, md=6, lg=6, xl=6,
style=dict()
)
layout = dmc.Grid(
[category_col, sub_category_col]
)
return layout
def province_analysis_layout():
df=constants.DATA_PROVINCE
cat_col = get_category(df.copy())
layout = dmc.Grid([cat_col], justify='center')
return layout
and this is my callbacks file
import dash
import pandas as pd
from dash.dcc import send_data_frame
from dash.dependencies import Input, Output, State
import constants, common
import constants
def add_province_callbacks(app: dash.Dash):
@app.callback(
Output('sub_categories_menu1', 'options'),
[Input('category_menu1', 'value')],
prevent_initial_call=True
)
def update_sub_categories(selected_category, previous_selected_category):
print(f"Selected category: {selected_category}")
df=constants.DATA_PROVINCE
filtered_rows = df[df['sub_name'] == selected_category]
sub_categories = filtered_rows['title'].unique().tolist()
print(f"Sub-categories for '{selected_category}': {sub_categories}") # Added logging
return [{'label': sub_category, 'value': sub_category} for sub_category in sub_categories]