You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Today you can panel.bind to the value trait of an ipywidgets. But ipywidgets sometimes don't have a value trait or sometimes it has lots of extra, interesting traits.
In https://discourse.holoviz.org/t/panel-works-with-anywidget/6466 I demonstrate how you can easily create an observer, A Parameterized object with parameters corresponding to the ipywidgets that observe for trait changes. Such an observer makes it easy to .watch, @depends, bind and more to the ipywidget.
Please consolidate this functionality and add it to Param.
It should probably be easy to only observe a specified selection of traits (white list) as well as a specified selection of traits not to watch (black list).
# pip install panel ipywidgets_bokeh anywidgetimportanywidgetimporttraitletsclassCounterWidget(anywidget.AnyWidget):
_esm=""" export function render({ model, el }) { let getCount = () => model.get("count"); let button = document.createElement("button"); button.classList.add("counter-button"); button.innerHTML = `Count is ${getCount()}`; button.addEventListener("click", () => { model.set("count", getCount() + 1); model.save_changes(); }); model.on("change:count", () => { button.innerHTML = `Count is ${getCount()}`; }); el.appendChild(button); } """_css=""" .counter-button { background-color: pink; font-size: 48px} .counter-button:hover { background-color: pink; } """count=traitlets.Int(0).tag(sync=True)
counter=CounterWidget()
# HELP FUNCTIONALITY to convert Traitlets Classes/ Events to Param Classes/ Eventsimportparam_ipywidget_classes= {}
_any_widget_traits=set(anywidget.AnyWidget().traits())
defcreate_observer(obj, traits=None)->param.Parameterized:
"""Returns a Parameterized class with parameters corresponding to the traits of the obj Args: traits: A list of traits to observe. If None all traits not on the base AnyWidget will be observed. """ifnottraits:
traits=list(set(obj.traits())-_any_widget_traits)
name=type(obj).__name__ifnamein_ipywidget_classes:
observer_class=_ipywidget_classes[name]
else:
observer_class=param.parameterized_class(name, {trait: param.Parameter() fortraitintraits})
_ipywidget_classes[name] =observer_classvalues= {trait: getattr(obj, trait) fortraitintraits}
observer=observer_class(**values)
obj.observe(lambdaevent: setattr(observer, event["name"], event["new"]), names=traits)
returnobserver# THE PANEL APPimportpanelaspnpn.extension("ipywidgets")
observer=create_observer(counter)
defsome_output(count):
returnf"The count is {count}!"component=pn.Column(counter, pn.bind(some_output, observer.param.count))
pn.template.FastListTemplate(
site="Panel",
title="Works with AnyWidget",
main=[component],
).servable()
panel-works-with-anywidget.mp4
The text was updated successfully, but these errors were encountered:
Today you can
panel.bind
to thevalue
trait of anipywidgets
. But ipywidgets sometimes don't have avalue
trait or sometimes it has lots of extra, interesting traits.In https://discourse.holoviz.org/t/panel-works-with-anywidget/6466 I demonstrate how you can easily create an observer, A Parameterized object with parameters corresponding to the
ipywidgets
that observe for trait changes. Such an observer makes it easy to.watch
,@depends
,bind
and more to the ipywidget.Please consolidate this functionality and add it to Param.
It should probably be easy to only observe a specified selection of traits (white list) as well as a specified selection of traits not to watch (black list).
panel-works-with-anywidget.mp4
The text was updated successfully, but these errors were encountered: