Can I use a loop to add a dynamic number of cards that would contain subplots?

I am trying to create a dash that would build a list of cards, one after another going downwards, made up of subplots for each student in my class. I would like to have two line graphs and two polar bars for each student in each subplot with their name displayed to the left for each student. Is there a way to do that if you have a list of the students and their individual dataframes?

You could either

  • Add in your layout a list of cards each containing the required chart (and can create this list via a for loop or list comprehension)
  • Put all the charts in one figure (checkout the facet_row option with plotly express to get started quickly)
2 Likes

so you could technically do it kind of like this?

app.layout = html.Div([class_subplots])

@app.callback(
    Output("class_subplots", "figure"),
    Input('current-student_df', "data")
def add_student_subplots(df):
    cared_list=list()
    for s in df['student'].unique():
        new_df = df[df['student']==s]
        card = dbc.Row([dbc.Card([
                                fig = make_subplots(blahblahblah)
                                fig.add_trace(blahblah)
                   ])])
        card_list.append(card)

    return card_list

No, you would need to return a list of cards, each of which has a dcc.Graph with the figure in it. This list would be the child of a Fiv or a Container for example.

1 Like

I know I probably have this wrong…but something closer to this?


app.layout = html.Div([dbc.Container(id=class_subplots])


@app.callback(
    Output("class_subplots", "children"),
    Input('current-student_df', "data")
def add_student_subplots(df):
    cared_list=list()
    for s in df['student'].unique():
        new_df = df[df['student']==s]
        card = [dbc.Card([
                                fig = make_subplots(blahblahblah)
                                fig.add_trace(blahblah)
                   ])
        card_list.append(card)

    return card_list

That looks right :slight_smile: apart from the typo on cared_list

1 Like

Thanks, man. I really appreciate your help.