Skip to content

Avoid event accumulation in webagg backend. #27160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

raphaelquast
Copy link
Contributor

PR summary

This PR is intended to address the problem of event-accumulation during image-processing in the webagg backend
which can result in an extreme lag that makes "fast updated" events like motion_notify_event etc. unusable at the moment.

Not 100% sure, but this might also improve problems concerning blitting in jupyter-notebooks (e.g. #19116) but would require sending "ack" messages in ipympl. The implementation is made in a way such that ipympl (or more generally backends that don't send "ack" messages) are not affected.

The following gifs show the outcome of this change: (left: current behavior, right: result of this PR)

🐍 code to run example
import matplotlib as mpl
mpl.use("webagg")
mpl.rcParams['webagg.port']=8988
mpl.rcParams['webagg.open_in_browser']=True

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(14, 8))
ax.plot([0, 1], [0, 1], 'r')
ln, = ax.plot([0, 1], [0, 0], 'g', animated=True)
ax.figure.canvas.draw()

fig._last_blit_bg = fig.canvas.copy_from_bbox(ax.bbox)

def on_resize(event):
    ax.figure.canvas.draw()
    fig._last_blit_bg = fig.canvas.copy_from_bbox(ax.bbox)

def on_move(event):
    ax.figure.canvas.restore_region(fig._last_blit_bg)
    ln.set_ydata([event.ydata, event.ydata])
    ax.draw_artist(ln)
    ax.figure.canvas.blit(ax.bbox)

fig.canvas.mpl_connect('motion_notify_event', on_move)
fig.canvas.mpl_connect('resize_event', on_resize)

plt.show()

PR checklist

@tacaswell
Copy link
Member

Comments from call:

  • add ability to opt-in to this behavior with a string control (so we can support other stratgies)
  • look into de-bouncing in blit as well

attn @ianhi

@ghost
Copy link

ghost commented Sep 25, 2024

I am using an interactive ipympl widget in an application, that would profit a lot from blitting, so i have been waiting for this PR to be merged. Thank you for the promising work done.
I just wanted to ask now, if we can expect this to be merged eventually, or if development has stopped, or is happening somewhere else, or ... ?

@raphaelquast
Copy link
Contributor Author

Hey, just a quick comment from my side:
I would be really happy if this PR is merged sometime soon...
Unfortunately however, I have absolutely no time right now to continue working on the remaining issues that need to be resolved and it will stay this way for a few months more...

I am happy if someone takes over in the meantime, otherwise I'll try to continue working on it as soon as i find some time (best guess is sometime early next year).

@sjmarwitz you could also monkey-patch the fix in the meantime to check if it does the job for you... I implemented a workaround for EOmaps ( see here)

@ghost
Copy link

ghost commented Sep 26, 2024

@raphaelquast Thanks for your quick response and for pointing me to your monkey-path fix. I tried it and immediately hit the problem with ipympl, that was already mentioned in the PR summary. Actually, when using ipympl, after the initial display of a figure, it won't update the canvas/figure, not even zooming or panning is possible. It seems, no 'ack' messages are being received (and sent?).

I would have liked to contribute a fix somehow, given the limited amount of time i have, but looking into the ipympl code, i got lost, as i don't "speak" js and the like and are not familiar with web technologies.


# Keep track of the number of received ack messages.
if self._num_received_images is None:
self._num_received_images = 0 # To support no "ack" messages.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know what this comment means.

Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
raphaelquast and others added 2 commits March 5, 2025 09:39
Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants