Hello,
The script below does not display any values in the dropdown menu.
I am trying to display values in the menu dynamically via callback as the dropdown values change depending on the value of the tab.
I have written an example code below that reproduces the error.
Could someone point me in the right direction please?
import pandas as pd
import numpy as np
from dash import Dash, dcc, html, Input, Output, callback
import dash_bootstrap_components as dbc
import plotly.express as px
app = Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
tabs_content = html.Div([
dbc.Row(dbc.DropdownMenu(id = 'dpvars')),
dbc.Row(dcc.Graph(id="graph1")),
])
app.layout = dbc.Container(
[
html.H1("Iris Dataset"),
html.Hr(),
dbc.Tabs(
[
dbc.Tab(label="Length", tab_id="length")
],
id="tabs",
active_tab="length",
),
html.Div(id="tab-content", className="p-4"),
]
)
@app.callback(
Output("tab-content", "children"),
[Input("tabs", "active_tab")],
)
def render_tab_content(active_tab):
if active_tab:
if active_tab == "length":
return tabs_content
@app.callback(Output("graph1", "figure"),
Output("dpvars", "options"),
Output("dpvars", "value"),
[Input("tabs", "active_tab")])
def generate_graphs(active_tab):
iris_df = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
if active_tab == 'length':
menu_list = ['length', 'petal']
keep_cols = []
for each_var in menu_list:
keep_cols.append([col for col in iris_df.columns if each_var in col])
keep_cols2 = [col for sublist in keep_cols for col in sublist]
keep_cols2 = np.unique(keep_cols2).tolist()
value = menu_list[0]
df = iris_df[keep_cols2].copy()
fig = px.line(df)
return fig, [{"label": i, "value": i} for i in menu_list], value
if __name__ == "__main__":
app.run_server()