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?