Hi everyone!
I have an accordion in my app that gets populated based on some API calls, and I can “reload” the accordion at any point to see if the title or content of any item changed, which I do by rebuilding the accordion in a callback. When testing this I noticed that some times the refresh kept the state of the accordion exactly as it was before the refresh, and other times it opened or closed the last accordion item I had interacted with. Here’s a video of what I’m trying to say and the code to reproduce it.
from time import sleep
import dash_mantine_components as dmc
from dash import Dash, _dash_renderer, html, Input, Output
_dash_renderer._set_react_version("18.2.0")
app = Dash(
__name__,
suppress_callback_exceptions=True,
external_stylesheets=[dmc.styles.ALL],
)
app.layout = dmc.MantineProvider([
dmc.Button('Reload', id='reload-accordion'),
html.Div(id='accordion'),
])
@app.callback(
Output('accordion', 'children'),
Input('reload-accordion', 'n_clicks'),
running=[
(
Output('reload-accordion', 'children'),
[dmc.Loader(size='sm', color='white'), ' Reload'],
'Reload',
),
],
prevent_initial_call=True)
def reload_accordion(_clicks):
sleep(1)
return dmc.Accordion([
dmc.AccordionItem([
dmc.AccordionControl("A"),
dmc.AccordionPanel("Content A"),
], value="A"),
dmc.AccordionItem([
dmc.AccordionControl("B"),
dmc.AccordionPanel("Content B"),
], value="B"),
dmc.AccordionItem([
dmc.AccordionControl("C"),
dmc.AccordionPanel("Content C"),
], value="C"),
])
if __name__ == '__main__':
app.run(debug=True)
The only thing I can deduce, although it doesn’t make sense to me, is that when you click items an odd amount of times, it keeps the state of the accordion, and when it’s an even amount of times, it opens or closes the last item you interacted with.
In any case, I was hoping to find a way to control this behavior, mainly, to always keep the state of the accordion after an update. I’m thinking it might be possible because the functionality is already there somewhere, it’s just not consistent.
Thank you in advance for any insight you can provide.