posit-dev / py-shinywidgets

Render ipywidgets inside a PyShiny app
MIT License
41 stars 2 forks source link

With maps, sometimes there are too many mouse move events #86

Closed wch closed 1 year ago

wch commented 1 year ago

On the Shiny for Python web site, if you run this in one of the live example boxes, there are too many mouse events, and it can get very laggy -- sometimes taking over ten seconds to respond to subsequent input.

On my Mac, I see this behavior:

from shiny import App, ui, reactive
from shinywidgets import register_widget, output_widget
import ipyleaflet as ipyl

app_ui = ui.page_fluid(
    ui.input_select("center", label="Center", choices=["London", "Paris", "New York"]),
    output_widget("map"),
)

def server(input, output, session):
    map = ipyl.Map(zoom=4)
    register_widget("map", map)

    @reactive.Effect()
    def _():
        center = input.center()
        if center == "London":
            map.center = (51.5074, 0.1278)
        elif center == "Paris":
            map.center = (48.8566, 2.3522)
        elif center == "New York":
            map.center = (40.7128, -74.0060)

    count = 0
    @reactive.Effect()
    def _():
        nonlocal count
        count += 1
        print(str(count) + ":  " + input.shinywidgets_comm_send())

app = App(app_ui, server)

In this video, just moving the mouse in and out of the map area results in 115 mouse events -- the counter in the console goes from 357 to 471. When I do more mouse motion, it can get backed up with hundreds or even thousands of mouse events, and it takes a long time for it to finish processing these events.

https://user-images.githubusercontent.com/86978/228678233-b0fb3528-f353-4eee-b9f0-9287cb340dd1.mov

This doesn't happen, or at least doesn't happen nearly as much, with the touchpad. It also doesn't happen on the shinylive.io site, for some reason.

I believe it could be addressed by throttling this: https://github.com/rstudio/py-shinywidgets/blob/2b9bd2d8531e797691f95b3481751120a572fe0c/js/src/comm.ts#L35