Hi, I am new to Dash. I created a Dash with 2 tabs, continent and country. In the country tab, I want to create a chained callback for the country dropdown by filtering continent. However, I received a ValueError: cannot assign without a target object. I have read through Dash App With Chained Callbacks, and there is no issue when I run the chained callback without inserting it into dcc.Tab. Please help as any advice is greatly appreciated.
My code:
app = Dash(__name__)
tab1 = html.Div([
html.H2("Select Continent(s)"),
dcc.Dropdown(
id="continent_dropdown",
options=sorted(df["Continent"].unique()),
multi=True,
searchable=True
),
html.Br(),
html.Div("Geospatial Data (by Continent)"),
html.Br(),
dcc.Loading(dcc.Graph(id="graph1"), type="cube")
])
tab2 = html.Div([
html.H2("Select Country(ies)"),
dcc.Dropdown(
id="country_dropdown",
options=sorted(df["Country"].unique()),
multi=True,
searchable=True
),
html.H4("Filter by Continent"),
dcc.Dropdown(
id="continent_filter_dropdown",
options=sorted(df["Continent"].unique()),
multi=True,
searchable=True
),
html.Br(),
html.Div("Geospatial Data (by Country)"),
html.Br(),
dcc.Loading(dcc.Graph(id="graph2"), type="cube")
])
def choropleth_map(data):
fig = px.choropleth(data, #remaining choropleth code......)
return fig
app.layout = html.Div([
dcc.Tabs(
id="tabs",
value="continent",
children=[
dcc.Tab(
label="Continent",
value="continent",
children=tab1
),
dcc.Tab(
label="Country",
value="country",
children=tab2
)
]
)
])
@app.callback(
Output("graph1", "figure"),
Input("continent_dropdown", "value")
)
def display_continent(continent_value):
dff = copy.deepcopy(df)
if continent_value:
dff = df[df["Continent"].isin(continent_value)]
return choropleth_map(dff)
@app.callback(
Output("country_dropdown", "options"),
Input("continent_filter_dropdown", "value")
)
def set_country_options(continent_value):
dff = copy.deepcopy(df)
if continent_value is not None:
dff = dff.query("Continent = @continent")
return sorted(dff["Country"].unique())
@app.callback(
Output("graph2", "figure"),
Input("country_dropdown", "value")
)
def display_country(country_value):
dff = copy.deepcopy(df)
if country_value:
dff = df[df["Country"].isin(country_value)]
return choropleth_map(dff)
if __name__ == "__main__":
app.run(debug=True)