voila-dashboards / voila

Voilà turns Jupyter notebooks into standalone web applications
https://voila.readthedocs.io
Other
5.33k stars 497 forks source link

Persistent loop of Matplotlib figure animation results in flickering output when run in a thread #1341

Open rht opened 1 year ago

rht commented 1 year ago

Description

The Mesa agent-based modeling library is looking to replace its self-hosted Tornado-based visualization server with Voilà. I have made a prototype in https://github.com/rht/mesa-examples/tree/voila. However, I encountered the flickering issue reported in #431. To summarize, it is like running the Game of Life simulation with play and stop buttons. The play and stop work, except that the display flickers.

The long-running loop: https://github.com/rht/mesa-examples/blob/99a68386226f3fe5be3953d25a84bd92f8b7065c/examples/boltzmann_wealth_model/run_voila.py#L161-L170. If I run the loop without threading or multiprocessing, it runs just fine. Each loop lasts for about 300 ms. My hypothesis is that the solutions in #431 does not apply because there is only 1 plot being constantly re-rendered, whereas in my case, I have 3 objects being constantly rerendered:

I have tried the plt.draw() as recommended in https://github.com/voila-dashboards/voila/issues/431#issuecomment-542390982, but it didn't work out. I have also tried adding clear_output(wait=True), and it didn't work out.

I haven't tried on JupyterLab yet, and have been focusing to make it work with voila --no-browser --debug. I apologize in advance if this issue is not concise nor self-contained.

Context

rht commented 1 year ago

I solved the problem for my use case by using widgets.Play, and thus simplifying the looping, threading, and observe situation. The flickering and out-of sync of the display is still there, but given that #431 is still open, I will close this as a duplicate of it.

maartenbreddels commented 1 year ago

Hi,

thanks for opening the issue, and you are right https://github.com/voila-dashboards/voila/issues/431 is probably related. Not that this is not a Voila issue specifically, but an ipywidget problem. The long term solution is https://github.com/jupyter-widgets/ipywidgets/pull/3759 I think.

In solara (when using solara-server) this will already be used (see https://github.com/widgetti/solara/pull/68 ) Although I think we could create an alternative output widget with this behavior without introducing a breaking change that should also work in the Jupyter notebook and Voila.

Regards,

Maarten

rht commented 1 year ago

Thank you for the pointer to Solara! It's great that it is built on top if ipywidgets, and the demo looks slick.

rht commented 1 year ago

Context for this issue: https://github.com/projectmesa/mesa/discussions/1622.

maartenbreddels commented 5 months ago

See also https://github.com/jupyter-widgets/ipywidgets/issues/2358#issuecomment-1866978693 which means that with ipykernel 6.29.0 this mighe work (please let us know here to confirm)

rht commented 5 months ago

Tested on ipykernel==6.29.0 and mesa==2.1.5: the flickering is still there, except that it happens much less often, in about every ~15 s or 30 s or so. Whereas on ipykernel==6.26.0 it won't even play the animation.