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

pn.state.curdoc is None inside of a popup-function #6332

Closed
jerry-kobold opened this issue Jul 18, 2024 · 5 comments
Closed

pn.state.curdoc is None inside of a popup-function #6332

jerry-kobold opened this issue Jul 18, 2024 · 5 comments
Milestone

Comments

@jerry-kobold
Copy link

jerry-kobold commented Jul 18, 2024

Recently HoloViews added the capability to open a popup when selecting points in a plot. However the function invoked this way does not seem to propagate the pn.state.curdoc correctly, which results in a break in any logic that depends on this.

ALL software version info

OS X Sonoma M1

bokeh==3.4.2
holoviews==1.19.1
panel==1.4.4
pandas==2.2.2

Description of expected behavior and the observed behavior

When you add a subscriber to a stream, that subscriber has access to the current Bokeh document. However, when the popup function is invoked, this document is set to None.

Complete, minimal, self-contained example code that reproduces the issue

import holoviews as hv
import numpy as np
import pandas as pd
import panel as pn

from holoviews.streams import Selection1D

hv.extension("bokeh")
pn.extension()

data = np.random.default_rng().random(size=(10, 2))
df = pd.DataFrame(data, columns=["x", "y"])

def make_plot():
    return hv.Points(df, kdims=["x", "y"]).opts(framewise=True, tools=["box_select"])

dm = hv.DynamicMap(make_plot)

def debug(*args, **kwargs):
    print("debug:", pn.state.curdoc)

def popup(index):
    print("popup:", pn.state.curdoc)

sel_stream = Selection1D(source=dm, popup=popup)
sel_stream.add_subscriber(debug)

pn.Column(dm).servable()

If you run this with panel serve and select some points in the plot, the debug function will print a valid document, while the popup function will not.

@philippjfr
Copy link
Member

Thanks, this is likely a HoloViews issue.

@philippjfr philippjfr transferred this issue from holoviz/panel Jul 18, 2024
@philippjfr philippjfr modified the milestones: 1.19.1, 1.19.2 Jul 18, 2024
@jerry-kobold
Copy link
Author

jerry-kobold commented Jul 18, 2024

Thanks, I wasn't sure where to post it, so I went with the panel repo because it touched pn.state.

@jerry-kobold
Copy link
Author

FWIW, I was able to work around this problem by doing something like this:

def popup(curdoc, index):
    pn.state.curdoc = curdoc
    # do stuff with index that depends on curdoc

sel_stream = Selection1D(source=dm, popup=partial(popup, pn.state.curdoc))

@philippjfr
Copy link
Member

Yep that should work, any chance you'd consider submitting a fix to HoloViews, I think all that's needed is adding something like:

from panel.io.state import set_curdoc
...
with set_curdoc(self.plot.document):
    popup = popup(**stream.contents)

here:

https://github.com/holoviz/holoviews/blob/main/holoviews/plotting/bokeh/callbacks.py#L691

@jerry-kobold
Copy link
Author

I'll give it a shot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants