Struggling to show 2 lines on a chart when user selects a metric

Hi, so i have this code:

app = dash.Dash(name)

df = pd.read_csv(“wbr_all_data.csv”, parse_dates=[‘snapshot_day’])
metric_columns = [col for col in df.columns if col not in [‘source_fc’, ‘destination_fc’, ‘snapshot_day’,‘arc’,‘sourcegislat’,‘sourcegislon’,‘destinationgislon’,‘destinationgislat’,‘region’]]
df = pd.melt(df, id_vars=[‘region’, ‘arc’, ‘snapshot_day’], value_vars=metric_columns, var_name=‘metric’, value_name=‘value’)
df = df.fillna(0)
print(df.head(5))
df = df.pivot_table(values=‘value’, index=[‘metric’], columns=[‘snapshot_day’], aggfunc=‘sum’, fill_value=0)
df.to_csv(‘test.csv’)
print(df.head(5))

df.columns = df.columns.strftime(‘%Y-%m-%d’)

df.reset_index(inplace=True)

app.layout = html.Div([
dash_table.DataTable(
id=‘table’,
columns=[{“name”: i, “id”: i, “selectable”:True } for i in df.columns],
data=df.to_dict(“records”),
editable=True,
row_selectable=“multi”,
selected_rows=,
page_action=“native”,
page_current=0,
page_size=10,
),
html.Div(id=“line-chart”)
])

traces =
@app.callback(
Output(“line-chart”, “children”),
Input(“table”, “derived_virtual_selected_rows”)
)
def update_figure(derived_virtual_selected_rows):
if derived_virtual_selected_rows:
traces.clear()
for index in derived_virtual_selected_rows:

        metric_name = df.iloc[index, 0]
        metric_data = df.iloc[index, 1:]

        # Create a trace for each selected metric
        trace = {
            'x': metric_data.index,
            'y': metric_data.values,
            'type': 'line',
            'name': metric_name
        }
        traces.append(trace)

    # Return the figure with the updated data and layout
    return dcc.Graph(
        id='line-chart',
        figure={
            'data': traces,
            'layout': {
                'title': 'Selected Metrics',
                'showlegend': True
            }
        }
    )
else:
    return

The code above displays a table as expected and a little square which user can click to select a metric, however when i click one of the metrics, a graph below pops up just fine but when i select another metric, nothing happens. What im trying to do is to have an ability for user to select multiple metrics and see them on the same chart but with different colors. What am i doing wrong here?

HI @aswy6 welcome to the forums.

Could you please format your code for better readability?