I’m trying to use dbc.Checklist
to filter a dash.data_table
and I want to make it looks cleaner so I decided to add Checklist in dmc.Accordition
. Something as below:
from dash import Dash, dash_table
import pandas as pd
import dash_mantine_components as dmc
import dash_bootstrap_components as dbc
from dash.dependencies import Input ,Output
df = pd.read_csv('https://raw.githubusercontent.com/hoatranobita/holiday_challenge/main/solar.csv')
app = Dash(__name__, external_stylesheets=[dbc.themes.LUX])
app.layout = dbc.Container([
dbc.Row([
dbc.Col([
dmc.Accordion(
children=[
dmc.AccordionItem([
dmc.AccordionControl("State"),
dmc.AccordionPanel([
dbc.Checklist(
options=[{"label": x, "value": x} for x in df['State'].unique()],
value=[],
id="checklist-input"),
html.Div(id='city')]),
],
value="customization",
),
]),
],width=2),
dbc.Col([
dash_table.DataTable(df.to_dict('records'),
columns = [{"name": i, "id": i} for i in df.columns],
id='tbl')
],width=10)
], className='p-2 align-items-center')
],fluid=True)
@app.callback(Output('city','children'),
Input('checklist-input','value'))
def update_table(checklist):
if checklist:
dff = df[df['State'].isin(checklist)]
return dmc.Accordion(
children=[
dmc.AccordionItem([
dmc.AccordionControl("City"),
dmc.AccordionPanel([
dbc.Checklist(
options=[
{"label": x, "value": x} for x in dff['City'].unique()
],
value=[],
id="checklist-input-2")
])
],
value="customization2")
])
@app.callback(Output('tbl','data'),
[Input('checklist-input','value'),
Input('checklist-input-2','value')],
)
def update_table(checklist, checklist2):
if checklist != [] and checklist2 != []:
dff = df[df['State'].isin(checklist)]
dff = dff[dff['City'].isin(checklist2)]
return dff.to_dict(orient='records')
elif checklist != [] and checklist2 == []:
dff = df[df['State'].isin(checklist)]
return dff.to_dict(orient='records')
else:
return df.to_dict(orient='records')
if __name__ == '__main__':
app.run_server(debug=False)
But I have 2 questions as below:
- Is there any way to add
html.Div
(City Checklist) right under each first checklist. For example, when I choosingCalifornia
, City Checklist will be under of it. - How to keep the second checklist not be refreshed when I choose both of the first Checklist.
Thank you.