Background callback doesn't allow usage of dash.callback_context

As far as I can understand from Dash 2.6.0 changelog, background callback does support dash.callback_context.
But when I try to apply this to my application with diskcache as background_callback_manager on local pc, it doesn’t work.

It would be nice if someone can give me a clear example on how to use dash.callback_context with background callback.

My example dash app:

testing.py

from dash import html,callback,DiskcacheManager,Dash,ctx
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output
import diskcache

cache = diskcache.Cache(“./cache”)
background_callback_manager = DiskcacheManager(cache)

app = Dash(name,
suppress_callback_exceptions=True,
background_callback_manager=background_callback_manager)
server = app.server

app.layout = html.Div(
[
dbc.Button(“Click me”,id=“button”),
dbc.Button(“Or Click me”,id=“button2”),
html.P(id=‘result’)
])

@callback(Output(‘result’,‘children’),
Input(‘button’,‘n_clicks’),
Input(‘button2’,‘n_clicks’),
background=True,
prevent_initial_call=True
)
def testing_funct(click1,click2):
if ctx.triggered_id==“button”:
a_string = “Button_1_clicked”
else:
a_string = “Button_2_clicked”
return a_string

if name == ‘main’:
app.run(debug=True)

The error raised was:

Raise exceptions.MissingCallbackContextException(dash.exceptions.MissingCallbackContextException: dash.callback_context.triggered_id is only available from a callback!

Hi @DeKhaos

I ran your example without an error. What version of dash are you using?

Here is the code formatted correctly in case anyone else wants to see if they can duplicate the error.

(Note - to format the code, enclose the code with three backticks or click on the </> symbol in the toolbar)

import dash
from dash import html,callback,DiskcacheManager,Dash,ctx
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output
import diskcache
print(dash.__version__)


cache = diskcache.Cache("./cache")
background_callback_manager = DiskcacheManager(cache)

app = Dash(__name__,
           suppress_callback_exceptions=True,
           background_callback_manager=background_callback_manager)
server = app.server

app.layout = html.Div(
    [
     dbc.Button("Click me",id="button"),
     dbc.Button("Or Click me",id="button2"),
     html.P(id='result')
     ])

@callback(Output('result','children'),
          Input('button','n_clicks'),
          Input('button2','n_clicks'),
          background=True,
          prevent_initial_call=True
           )
def testing_funct(click1,click2):
    if ctx.triggered_id=="button":
        a_string = "Button_1_clicked"
    else:
        a_string = "Button_2_clicked"
    return a_string

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



Thank you @AnnMarieW for your reply. I use Window 10, and my environment are dash 2.6.1 and python 3.9.12.

HI @DeKhaos

Try upgrading to dash 2.6.2 :crossed_fingers:

1 Like

@DeKhaos,

Just curious, what happens if you swap the @callback with @app.callback?

Even there was no specific mention in the changelog, update to dash 2.6.2 actually fixed the problem for me and the problem that I asked a while back on GitHub :smile:: Background callbacks not working properly with Input dict grouping

Thank you, @AnnMarieW.

1 Like

Same error occurs for me in dash 2.6.1, since as the document mentioned, both are the same way to use callback wrapper for dash app.

My guess is either somehow something got deleted or overwritten in your Dash package. Upgrading or reinstalling fixed the issue.

1 Like