Skip to content
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

Support async callbacks for popup #6390

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions holoviews/plotting/bokeh/callbacks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import base64
import inspect
import time
from collections import defaultdict
from functools import partial
Expand All @@ -19,6 +20,7 @@
Range1d,
)
from panel.io.notebook import push_on_root
from panel.io.server import async_execute
from panel.io.state import set_curdoc, state
from panel.pane import panel

Expand Down Expand Up @@ -663,15 +665,14 @@ def _update_selection_event(self, event):
self._selection_event = event
self._processed_event = not event.final
if event.final and self._skipped_partial_event:
self._process_selection_event()
self._skipped_partial_event = False
async_execute(self._process_selection_partial_event)

def on_msg(self, msg):
super().on_msg(msg)
if hasattr(self, '_panel'):
self._process_selection_event()
async_execute(self._process_selection_event)

def _process_selection_event(self):
async def _process_selection_event(self):
event = self._selection_event
if event is not None:
if self.geom_type not in (event.geometry["type"], "any"):
Expand All @@ -690,7 +691,10 @@ def _process_selection_event(self):
popup_is_callable = callable(popup)
if popup_is_callable:
with set_curdoc(self.plot.document):
popup = popup(**stream.contents)
if inspect.iscoroutinefunction(popup):
popup = await popup(**stream.contents)
else:
popup = await asyncio.to_thread(popup, **stream.contents)

# If no popup is defined, hide the bokeh panel wrapper
if popup is None:
Expand Down Expand Up @@ -747,6 +751,10 @@ def _process_selection_event(self):
push_on_root(self.plot.root.ref['id'])
self._existing_popup = popup_pane

async def _process_selection_partial_event(self):
await self._process_selection_event()
self._skipped_partial_event = False


class TapCallback(PopupMixin, PointerXYCallback):
"""
Expand Down
24 changes: 21 additions & 3 deletions holoviews/tests/ui/bokeh/test_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,24 @@
expect(locator).to_have_count(2)


@skip_popup
@pytest.mark.usefixtures("bokeh_backend")
def test_stream_popup_async_callbacks(serve_hv):
async def popup_form(x, y):
return pn.widgets.Button(name=f"{x},{y}")

points = hv.Points(np.random.randn(10, 2)).opts(tools=["tap"])
hv.streams.Tap(source=points, popup=popup_form)

page = serve_hv(points)
hv_plot = page.locator('.bk-events')
hv_plot.click()
expect(hv_plot).to_have_count(1)

locator = page.locator(".bk-btn")
expect(locator).to_have_count(2)


@skip_popup
@pytest.mark.usefixtures("bokeh_backend")
def test_stream_popup_visible(serve_hv, points):
Expand Down Expand Up @@ -407,9 +425,9 @@
expect(hv_plot).to_have_count(1)

box = hv_plot.bounding_box()
start_x, start_y = box['x'] + 10, box['y'] + box['height'] - 10
mid_x, mid_y = box['x'] + 10, box['y'] + 10
end_x, end_y = box['x'] + box['width'] - 10, box['y'] + 10
start_x, start_y = box['x'] + 1, box['y'] + box['height'] - 1
mid_x, mid_y = box['x'] + 1, box['y'] + 1
end_x, end_y = box['x'] + box['width'] - 1, box['y'] + 1

page.mouse.move(start_x, start_y)
hv_plot.click()
Expand Down
Loading