Duplicate Callback Outputs not working

Hi, I am trying to have same output i.e., graph from two different callbacks. For this I am using Duplicate Callback Outputs I have gone through the documentation about it at
https://dash.plotly.com/duplicate-callback-outputs
and have tried to implement in my case as follows:

@app.callback(Output('graph', 'figure'),Input('my-dropdown', 'value'), Input('geojson', 'hover_feature'), prevent_initial_call=True)

def update_graph(b1, b2):
    triggered_id = ctx.triggered_id
    print(triggered_id)
    if triggered_id == 'my-dropdown':
         return draw_graph()
    elif triggered_id == 'geojson':
         return state_hover()

def draw_graph(parcel_foreign_id_x_selected):
    dff = df[df.parcel_foreign_id_x==parcel_foreign_id_x_selected]
    fig = px.line(data_frame=dff, x="s1product_end_time", y=["ndvi_avg", "vhvv_avg","cohvv_avg","cohvh_avg"],markers=True, width=1000,height=600,
                      labels={"s1product_end_time": "Date"})
    return fig

def state_hover(features):
    if features is not None:
        clickfeatureresult =f"{features['properties']['parcel_id']}"
        dfff= df[df.parcel_foreign_id_x==clickfeatureresult]
        fig = px.line(data_frame=dfff, x="s1product_end_time", y=["ndvi_avg", "vhvv_avg","cohvv_avg","cohvh_avg"],markers=True, width=1000,height=600,
                      labels={"s1product_end_time": "Date"})
    return fig

The problem is when I run this the graph doesnot get updated against any of the inputs. The error I get are

TypeError: state_hover() missing 1 required positional argument: ‘features’
TypeError: draw_graph() missing 1 required positional argument: ‘parcel_foreign_id_x_selected’

One reason it might be is my function is draw_graph(parcel_foreign_id_x_selected) and state_hover(features) but I am using draw_graph() and state_hover() in the def update_graph(b1, b2) but if I put draw_graph(parcel_foreign_id_x_selected) and state_hover(features) in the update_graph() it says features and parcel_foreign_id_x_selected are not defined.

Can anyone please guide what could be the problem here.

The problem is that you are trying to use global variables. Have a read here:

2 Likes

Thanks, replaced the b1 and b2 with my inputs of features and parcel_foreign_id_x_selected and it worked.

If anyone else is also facing this problem here is how I modified it

@app.callback(Output('graph', 'figure'),Input('my-dropdown', 'value'), Input('geojson', 'hover_feature'), prevent_initial_call=True)

def update_graph(parcel_foreign_id_x_selected, features):
    triggered_id = ctx.triggered_id
    print(triggered_id)
    if triggered_id == 'my-dropdown':
         return draw_graph(parcel_foreign_id_x_selected)
    elif triggered_id == 'geojson':
         return state_hover(features)