Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

[Solved] Using a long interval, render once before interval starts

Hello All,

Quick question about live updates. I used the example at https://plot.ly/dash/live-updates to build the following app (on heroku): https://safe-island-13543.herokuapp.com. I set the interval to 5 seconds, but then it takes 5 seconds for the graph to render the first time. The data in my database gets updated every 15 minutes, so there is no reason to use such a short interval. Does anyone have an example of this I could follow?

Thanks,

Barry

Ignore me.

Just use your figure generation function in dcc.Graph, e.g:

app.layout = html.Div(children = [
    dcc.Graph(                         
        id = 'whatever',             
        figure = generate_figure()     
    ),                                 
    dcc.Interval(                      
        id = 'interval-component',     
        interval = 5 * 1000            
    )                                  
])                                     

Classic ask the question and instantly realize you can solve it yourself :stuck_out_tongue:

I was having the same issue, thanks for the solution.

One problem though: I can’t have a callback on the generate_figure function. Currently, I just write the same function two times, once with the callback and once without. Is there some cleaner workaround for that?

It’s hard to say without seeing a reproducable code snippet, but perhaps you can do something like:

def generate_figure():
    ...
    return figure

app.layout = generate_figure()

app.callback(...)(generate_figure)

Instead of @app.callback. For more about how the @ works in the @app.callback signature and how you can get around it, see this explanation on decorators: https://stackoverflow.com/questions/739654/how-to-make-a-chain-of-function-decorators/1594484#1594484