Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use dash callback without an Input

I am trying to call a plotly-dash callback without the Input and the method won't fire.

This is a dashboard I am trying to build using dash. In the past when I use a callback with both the Input and Output everything works fine but when I tried using only output the result is not displayed on the dashboard.

html.Div(
    [

        html.P(
            "Tweet Count",
            className="twelve columns indicator_text"
        ),
        html.P(
            id = 'tweet_value',
            className="indicator_value"
        ),
    ],
    className="four columns indicator",

)

@app.callback(
Output("tweet_value","children")

)
def total_tweet_callback():

    return 100   
like image 460
Izuagbala Avatar asked Jan 15 '19 04:01

Izuagbala


People also ask

How does callback work in Dash?

Whenever an input property changes, the function that the callback decorator wraps will get called automatically. Dash provides this callback function with the new value of the input property as its argument, and Dash updates the property of the output component with whatever was returned by the function.

Can you have multiple callbacks in Dash?

If a Dash app has multiple callbacks, the dash-renderer requests callbacks to be executed based on whether or not they can be immediately executed with the newly changed inputs. If several inputs change simultaneously, then requests are made to execute them all.

Can we add multiple input components to a Dash callback function?

Currently multiple inputs will make the functionality not work. I've put multi=true for my dcc dropdown - hasn't worked successfully yet. This is the code I have used. This is an example of the data being inputted.

Is Dash easy to build GUI?

Through a couple of simple patterns, Dash abstracts away all of the technologies and protocols that are required to build a full-stack web app with interactive data visualization. Dash is simple enough that you can bind a user interface to your code in less than 10 minutes.


2 Answers

All callbacks are run once when loaded, except disabled explicitly. So a simple solution is to use a dummy input, referring to anything, and just not using it.

I do not agree with the other answer. Directly calling the function would not make it triggered every 'load'. In that way the function only run once when the statement is run. The function is only triggered when the app is started, not every time you click refreshing button of your browser. If you use datetime.datetime.now() there, you can see the difference.

like image 142
zr67800 Avatar answered Sep 19 '22 15:09

zr67800


There is a need for at least one input or event for a callback to get called, as written inside the dash.py code:

Without Input or Event elements, this callback will never get called.

(Subscribing to input components will cause the callback to be called whenever their values change and subscribing to an event will cause the callback to be called whenever the event is fired.)

In your case - if there is no trigger for the callback, why use a callback? If you want total_tweet_callback to run only once on load, simply call it from the layout:

def total_tweet_callback():
    return 100

app.layout = lambda: html.Div(
    [
        html.P(
            "Tweet Count",
            className="twelve columns indicator_text"
        ),
        html.P(
            children=total_tweet_callback(),
            id='tweet_value',
            className="indicator_value"
        ),
    ],
    className="four columns indicator",
)

Notice that:

  1. I removed the decorator from total_tweet_callback.
  2. I gave the layout a function (lambda function in this case) that returns the Div element. This isn't necessarily required, and depends on other aspects of your code. For example, including the lambda: will cause the total_tweet_callback function get called each time the page is reloaded, while removing it will get the value only once when the app is loaded.
like image 22
Shovalt Avatar answered Sep 17 '22 15:09

Shovalt