panel icon indicating copy to clipboard operation
panel copied to clipboard

Hook working in server but not in notebook

Open xavArtley opened this issue 3 years ago • 1 comments

ALL software version info

panel Version: 0.13.1

jupyterlab Version: 3.3.4

Description of expected behavior and the observed behavior

Panel with holoviews hook interaction working in server mode but not in a notebook

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

import param
import panel as pn
import holoviews as hv
from functools import partial
pn.extension()
hv.extension("bokeh")

class LegendSelection(param.Parameterized):
    muted = param.Dict(default={})

ls = LegendSelection()
def add_callback_to_bkplot(bkplot):
    def cbpy(attr, old, new, legend_label):
        new_dict = ls.muted.copy()
        new_dict.update({legend_label: new})
        ls.muted = new_dict
        print(ls.muted)
    [li.renderers[0].on_change("muted", partial(cbpy, legend_label=li.label["value"])) for li in bkplot.legend.items]

def hook(plot, elem):
    add_callback_to_bkplot(plot.handles["plot"])
    
ov = hv.Overlay([hv.Curve(([0,1],[1,1]), label="Test"), hv.Curve(([0,1],[2, 2]), label="Test2")]).opts(hv.opts.Overlay(hooks=[hook]))
pn.Row(ov, ls).servable()

Screenshots or screencasts of the bug in action

bog_nb_server

xavArtley avatar Jun 03 '22 09:06 xavArtley

So this answer is about 2.5 years late, but the problem here is that in a notebook you have to manage event handling yourself, especially inside Bokeh callbacks. The solution here is to do something like this:

        with pn.io.hold(bkplot.document):
            ls.muted = new_dict

I'm not sure there's much of anything we can do here to avoid this.

philippjfr avatar Jan 21 '25 12:01 philippjfr