Using html button to switch between dataframes

Hello all, I am trying to use the HTML button to switch which dataset/dataframe to work with. However I am having problems and unexpected performance

Here is the code snippet

df_1 = pd.read_csv("first_dataset.csv")
df_2 = pd.read_csv("second_dataset.csv")

# flag for switching between data
current_df = ""

app.layout = html.Div([
   html.Button("First", id="first-button"),
   html.Button("Second", id="second-button"),
   dcc.Dropdown(
      id="dropdown",
      options=[]),
   # further code is for showing data in a data table and a graph, this code is excluded

@app.callback(
   Output(component_id = "dropdown", component_property="options"),
   Input(component_id = "first-button", component_property="n_clicks"),
   Input(component_id = "second-button", component_property="n_clicks"),
)

def update_dropdown(button_one, button_two):
   change_id = [p["prop_id"] for p in dash.callback_context.triggered][0]
   if "first-button" in change_id:
      selection = df_1["names"].unique()
      output = [{"label": i, "value":i} for i in selection]
      current_df = "first"
   elif "second-button" in change_id:
      selection = df_2["names"].unique()
      output = [{"label": i, "value":i} for i in selection]
      current_df = "second"
   return output

@app.callback(
   Input(component_id="dropdown", component_property="value"),
   Output(component_id="datatable", component_property="data"),
)

def populate_datatable(selection):
   if current_df = "first":
      df == df_1.copy()
   elif current_df = "second":
      df == df_2.copy()
   return df.to_dict("records")

Is this the appropriate way to use multiple datasets for graphing?

Technically looks fine. Personally, I’d prefer splitting the callback into two separate callbacks, one for each button. That way, I won’t have to keep track of the “current” dataframe being used across callbacks. What issues are you running in to exactly? it’s a bit unclear if you’re seeing an unexpected behavior with your implementation or just asking for general advice.

Hi DeejL, I am new so I am really just looking for general advice! I didn’t think of having multiple callbacks for different buttons so i’ll give that a shot to see if things get better on my end. Could you give a short example on how you would use multiple callbacks to set an active dataframe? Thanks for the reply.