✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚾️ It's finally Baseball season! Root for the home team... & Register for our Sports Analytics Webinar!

Keep updating / redrawing graph while function runs

Everything on my front end freezes, including interval updates, while I’m inside a function call.

To be specific, I got a front end rocking some graphs and charts and a time consuming function that crunches some numbers and returns a result. I would like to show intermediate results of the timely function on a graph on the front end as the function is running. I was thinking the function could write the intermediate results to a list in shared memory, and the front end could be continuously pinging that list on interval to draw chart.

Any ideas thanks a tun everyone!
Nathan

By default, Dash runs on a single process so long-running functions will block any other requests from being processed. Your app can handle multiple requests in parallel by using threads, multiple processes, or multiple workers. Here are some options:


Option 1

app.run_server(threaded=True)

Option 2

app.run_server(processes=4)

Option 3

server = app.server

and then run in terminal with:

$ gunicorn --workers 4 app:server

where app refers to a file named app.py and server is the variable as defined above.

Thank you Chris, option 1 works for my situation fantastically. It’s worth mentioning that I also needed to make my ‘return’ function into a ‘yield’ function in order to get those intermediate results out of the function and into dash for plotting.

Thanks,
Nathan

While we are on the subject; would it be possible to make a yield from a function trigger an update to a chart element in dash? Instead of having it run on an interval and refresh the chart even if there is no new data. I was thinking I could have a work around for this by having some kind of binary trigger variable which checks each interval if there is new data and if and only if true, then update graph. But, maybe there is an easier way?

Thanks,
Nathan

That would be the way to do it right now - you can raise an exception inside a callback to prevent the update from propogating to the client.

Thanks Chris, you’re a rockstar. Great product.

1 Like