Programmatically firing a callback

is there a way to fire a callback function from a python function?

def fun1(input1):
      ...
     callbackfun(input)

@app.callback(Output('output', 'children'))
def callbackfun(input):
      ...
     return data
1 Like

One easy way is to just refactor your code:

def fun1(input1):
      ...
     callbackfun(input)

def callbackfun(input):
      ...
     return data

app.callback(Output('output', 'children'))(callbackfun)
2 Likes

There is also the undecorated package: https://github.com/mapleoin/undecorated and in Python 3 the __wrapped__ property: https://stackoverflow.com/a/33024739/4142536

it does indeed invoke the callbackfun but the returned data is not set to the Output

I think @chriddyp was assuming you only wanted to evaluate the results of the callback rather than update the client. You can’t arbitrarily trigger a callback from the server that updates a client.

In general, the server needs know where to send the output. Dash does this using standard HTTP requests, so a server always responds to a client making a callback request, which means it knows where to send the output to. If you look at the response payload of a Dash callback it doesn’t event have the id of the output element in it, because this information is known already by the client making the request. (hopefully @chriddyp will correct me if any of that isn’t quite accurate)

2 Likes

thank you for clarifying that, and sorry i didn’t explain it well from the beginning.

so there is no way to do it ?

It just doesn’t make any sense given the client-server nature of Dash. You have to be operating within the context of handling a request from a client in order to return something to the client. Outside of this context how does the server know where to send the output to? (There could be multiple clients currently interacting with the site, even if there was a way to address them, who would you send it to?)

1 Like

@hamou Perhaps you should describe what you actually want to do more broadly. If you’re trying to update a component, you could make it dependent on the output you’re currently calculating. Then the function will run as soon as the current one is finished.

2 Likes

There’s also the Interval component which, when used as an input to a callback, causes the callback to be called periodically. If you just need to the callback to be triggered every so often, this could do the job.

sorry for raising a concern in this forum

I am facing a problem in using drop downs to update my graph. I can see the drop down in the app but switching between them doesnot update the graph. Also, there is no graph displayed on the app.

what I did:
I added the name of the columns on my data set as the choices in the drop down menu. But there is only the layout and no graph and hence no update.

Could you please elaborate with an example ? I’m trying to trigger a callback using a custom function, and after an interval, trigger it again. Please help

from dash import dcc, html
import dash
import time

app = dash.Dash()

app.layout = html.Div([
    html.Button(id="starteroo", n_clicks=0),
    html.Div(id="circ", style={
        "width": "100px",
        "height": "100px",
        "background": "yellow",
    "border-radius": "50%"})
])


def fun1():
    for i in range(1,5):
        time.sleep(2)
        callbackfun(i)

def callbackfun(i):
    if i % 2 ==0:

        return {"width": "100px",
                "height": "100px",
                "background": "green",
                "border-radius": "50%"}
    else:
        return {
                    "width": "100px",
                    "height": "100px",
                    "background": "red",
                    "border-radius": "50%"}

app.callback(dash.dependencies.Output('circ', 'style'))

if __name__ == "__main__":
    app.run_server(debug=True, port=8050)

Thanking you in advance