Hey all, I am having some trouble with pattern matching callbacks in Dash. First, let me explain what I am trying to do, and then I’ll show you some code.
I am creating a simple dashboard for a research study. On the left side of the page should be a list of study participants. When you click on the participant, it will expand a list of dates that the participant did some activities. Then, when you click on a date, it will expand and show the activities that the participant did on that day. This expanding list is already functional. No help is needed on the expanding list.
When the user clicks on an activity, I want to show some content related to that activity - typically data, plots, etc. Here is a hand-drawn image showing my example layout:
As you can see, when the user clicks on a button/link in the left-hand expanding menu, I want to show some content on the right-hand side. When I generate the expanding menu, I give each button a unique id. That code looks something like this:
this_activity_layout = html.Div(
dbc.Button(f"{activity.activity_name}", id={
"type" : "activity_button",
"participant_id" : activity.participant_id,
"unique_button_id" : str(uuid.uuid4())
}, color="link"),
style={'text-indent':'6em'}
)
The final layout of the app comes together looking something like this:
app.layout = html.Div(
[
dbc.Row(
[
dbc.Col(
#All the layout stuff for the left-hand menu is in here.
),
dbc.Col(html.Div(id="container")) #This empty column is the right-hand side of the page
]
)
]
)
Then, I want to listen for those button clicks and update the right-hand side of the page accordingly. For now, just for the sake of simplicity, all I want to do is display some text. (Of course I want to do something more than that in the future, but I am having trouble just getting text to display on the right hand side at the moment).
Here is my callback function:
@app.callback(
Output("container", "children"),
Input({"type": "activity_button"}, "n_clicks")
)
def on_button_click(n):
ctx = dash.callback_context
if not ctx.triggered:
button_id = 'No clicks yet'
else:
button_id = ctx.triggered[0]['prop_id'].split('.')[0]
return html.Div(button_id)
As you can probably guess, nothing is happening when the buttons are clicked. No text is displayed on the right hand side of the page.
Can y’all help me figure out where I am going wrong? Thanks!