# dbc.Checklist in dmc.Accordition

**URL:** <https://community.plotly.com/t/dbc-checklist-in-dmc-accordition/72792>\
**Category:** Dash Python\
**Created:** [February 17, 2023, 3:18am UTC](https://community.plotly.com/t/dbc-checklist-in-dmc-accordition/72792 "2023-02-17T03:18:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hoatran](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/hoatran/32/21447_2.png) [@hoatran](https://community.plotly.com/u/hoatran)\
**Post date:** [February 17, 2023, 3:18am UTC](https://community.plotly.com/t/dbc-checklist-in-dmc-accordition/72792/1 "2023-02-17T03:18:21Z")

</div>

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:

```auto
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)

```

 ![Screenshot 2023-02-17 100238](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/5/8/58f40b13a9782531aca350807060d27091b03113.png)

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 choosing `California`, 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.

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [February 17, 2023, 9:50am UTC](https://community.plotly.com/t/dbc-checklist-in-dmc-accordition/72792/2 "2023-02-17T09:50:24Z")

</div>

HI @hoatran this looks similar to the file explorer, have you seen this topic?

> [@File Explorer Tree Generator for local files](https://community.plotly.com/t/file-explorer-tree-generator-for-local-files/68732):
>
> Hey all, I needed a reactive file explorer that generated a file tree given a path, so here it is. Big thanks to @AnnMarieW for help building this out! [Screen Recording 2022-10-08 at 5.09.47 PM] Here is the code: import os from dash\_iconify import DashIconify import dash\_mantine\_components as dmc class FileTree: def \_\_init\_\_(self, filepath: os.PathLike): """ Usage: component = FileTree('Path/to/my/File').render() """ self.filepath = filepath def re…

---

<div class="post-metadata">

**Author:** ![hoatran](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/hoatran/32/21447_2.png) [@hoatran](https://community.plotly.com/u/hoatran)\
**Post date:** [February 17, 2023, 11:29am UTC](https://community.plotly.com/t/dbc-checklist-in-dmc-accordition/72792/3 "2023-02-17T11:29:06Z")

</div>

Thank you, it seems not easy thing to understand 🥲

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [February 17, 2023, 11:14pm UTC](https://community.plotly.com/t/dbc-checklist-in-dmc-accordition/72792/4 "2023-02-17T23:14:42Z")

</div>

@hoatran, man, this one had me thinking a while.

In the end I used pattern machting callbacks and a helper function. It’s not perfect as the City- Accordion is still visible even if no State is chosen- this only works at startup. Maybe you can alter the visibility in the callback. Feel free to ask for clarification as this is maybe not best coding style 🙈

```python
from dash import Dash, dash_table, html, Input, Output, MATCH, ALL, ctx
import pandas as pd
import dash_mantine_components as dmc
import dash_bootstrap_components as dbc

df = pd.read_csv('https://raw.githubusercontent.com/hoatranobita/holiday_challenge/main/solar.csv')

def create_panel(idx, state):
    return dmc.AccordionPanel(
        [
            dbc.Checklist(
                options=[{"label": state, "value": state}],
                value=[],
                id={'type': "checklist-input", 'index': idx}
            ),
            html.Div(id={'type': "city", 'index': idx})
        ]
    )

app = Dash( __name__ , external_stylesheets=[dbc.themes.LUX])

app.layout = dbc.Container([
    dbc.Row([
        dbc.Col(
            [
                dmc.Accordion(
                    children=[
                        dmc.AccordionItem(
                            [dmc.AccordionControl("State")] +
                            [create_panel(idx, state) for idx, state in enumerate(df['State'].unique())],
                            value="customization",
                        ),
                    ]
                ),
            ],
            width=2
        ),
        dbc.Col([
            dash_table.DataTable(
                df.to_dict('records'),
                columns=[{"name": i, "id": i} for i in df.columns],
                id={'type': 'tbl', 'index': 0}
            ),
        ], width=10)
    ], className='p-2 align-items-center')
], fluid=True)

@app.callback(
    Output({'type': 'city', 'index': MATCH}, 'children'),
    Input({'type': 'checklist-input', 'index': MATCH}, 'value'),
    prevent_initial_call=True
)
def update_table(checklist):
    # get the triggering index to make sure the indices of checklist-input and checklist-input-2
    # are the same
    idx = ctx.triggered_id['index']

    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={'type': 'checklist-input-2', 'index': idx})
                ])
            ],
                value="customization2")
        ])

@app.callback(
    Output({'type': 'tbl', 'index': ALL}, 'data'),
    Input({'type': 'checklist-input', 'index': ALL}, 'value'),
    Input({'type': 'checklist-input-2', 'index': ALL}, 'value'),
    prevent_initial_call=True
)
def update_table(checklist, checklist2):
    # flatten checklist and checklist2 - due to using pattern matching callbacks with wildcard ALL
    # these are a list of lists
    cl_flat = [item for sublist in checklist for item in sublist]
    cl2_flat = [item for sublist in checklist2 for item in sublist]

    if cl_flat != [] and cl2_flat != []:
        dff = df[df['State'].isin(cl_flat)]
        dff = dff[dff['City'].isin(cl2_flat)]
        return [dff.to_dict(orient='records')]

    elif cl_flat != [] and cl2_flat == []:
        dff = df[df['State'].isin(cl_flat)]
        return [dff.to_dict(orient='records')]
    else:
        return [df.to_dict(orient='records')]

if __name__ == ' __main__':
    app.run_server(debug=True)

```

![table](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/9/5/95cd40789d7a19ab20be03e047006cf155dc65f3.gif)

mred accordion

---

<div class="post-metadata">

**Author:** ![hoatran](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/hoatran/32/21447_2.png) [@hoatran](https://community.plotly.com/u/hoatran)\
**Post date:** [February 18, 2023, 4:01am UTC](https://community.plotly.com/t/dbc-checklist-in-dmc-accordition/72792/5 "2023-02-18T04:01:22Z")

</div>

@AIMPED : Thank you so much, you made my day 😁. I will use it in my future projects.
