Lookup Error When Using Context_Callback

I’m trying to use context_callback in an if statement in a callback:

@app.callback(
    Output("EventbyType", "figure"),
    Input("EventbyTypeDropdown", "value"),
    Input('refresh-graph', 'n_clicks'),
    State('datepicker-start', 'value'),
    State('datepicker-end', 'value'),
)
def generate_EventbyType_graph(EventbyTypeDropdown, refresh_clicks, date_start, date_end, **kwargs):
    

    if ctx.triggered_id == 'EventbyTypeDropdown':
       

The last line is what gives me an error: LookupError: <ContextVar name=‘callback_context’ at 0x000001C4E5AE1C70>.

I can return a figure easily, but I’m wanting to not only change the figure by filtering from a dropdown, but also be able to refresh the data from a sql server. I’ve read the advanced callbacks page, but I’m clearly misunderstanding how callback_context is supposed to be used. I don’t see any examples of this on the community examples page, so any help would be greatly appreciated.

Try removing the single quotes around the EventbyTypeDropdown in the function.

@app.callback(
    Output("EventbyType", "figure"),
    Input("EventbyTypeDropdown", "value"),
    Input('refresh-graph', 'n_clicks'),
    State('datepicker-start', 'value'),
    State('datepicker-end', 'value'),
)
def generate_EventbyType_graph(EventbyTypeDropdown, refresh_clicks, date_start, date_end, **kwargs):
    

    if ctx.triggered_id == EventbyTypeDropdown:

I removed the quotes, but I still get the error.

Hi @starsidechimp,

an example:

import dash
from dash import html, Input, Output, ctx

app = dash.Dash(__name__)

app.layout = html.Div(
    [
        html.Button(
            'b1',
            id='b1'
        ),
        html.Button(
            'b2',
            id='b2'
        ),
        html.Div(
            id='out'
        ),
    ])


@app.callback(
    Output('out', 'children'),
    [
        Input('b1', 'n_clicks'),
        Input('b2', 'n_clicks'),
    ],
    prevent_initial_call=True
)
# pretty explicit
def update(*args):
    if ctx.triggered_id == 'b1':
        val = 'b1'
    else:
        val = 'b2'
    return val

# actually I would just do this:
# def update(*args):
#     return ctx.triggered_id


if __name__ == "__main__":
    app.run(debug=True)

Found the issue. I had django_plotly_dash imported at the top, even though I wasn’t using it at all. That was causing the error every time. It’s strange though, the django_plotly_dash docs mention callback_context being part of kwargs.

Hi @starsidechimp

Glad you figured out the issue :confetti_ball:

Thanks @AIMPED for providing a nice minimal example for callback_context. For anyone looking for more example apps, you can try searching for ctx. or select callback type “Callback Context” in the Featured examples: Dash Example Index

1 Like

I am seeing the same error, as described here .The error is occurring in line 275 of pages.py

Check your Dash version and make sure you are getting callback_context (or ctx) name from the correct package.
This what works for dash==2.9.3:

trigger_id = dash.callback_context.triggered[0]['prop_id']

Same error. It’s given when initialize Dash. I’ve extend the dash class. Probebly the problem occour only when i create it form another thread, really confused about it someone can help?