Skip to content
This repository has been archived by the owner on Nov 28, 2019. It is now read-only.

View parameters store callback per Parameterized instance #43

Merged
merged 1 commit into from
May 9, 2017
Merged
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
8 changes: 4 additions & 4 deletions paramnb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def action_cb(button):

kw['continuous_update']=self.p.continuous_update

if hasattr(p_obj, 'callback'):
if hasattr(p_obj, 'callbacks'):
kw.pop('value', None)

if hasattr(p_obj, 'get_range'):
Expand All @@ -209,7 +209,7 @@ def action_cb(button):

w = widget_class(**kw)

if hasattr(p_obj, 'callback') and value is not None:
if hasattr(p_obj, 'callbacks') and value is not None:
self._update_trait(p_name, p_obj.renderer(value), w)

def change_event(event):
Expand Down Expand Up @@ -237,8 +237,8 @@ def change_event(event):
else:
self._changed[p_name] = new_values

if hasattr(p_obj, 'callback'):
p_obj.callback = functools.partial(self._update_trait, p_name)
if hasattr(p_obj, 'callbacks'):
p_obj.callbacks[id(self.parameterized)] = functools.partial(self._update_trait, p_name)
else:
w.observe(change_event, 'value')

Expand Down
9 changes: 5 additions & 4 deletions paramnb/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ class _View(param.Parameter):
and may optionally supply the desired size of the viewport.
"""

__slots__ = ['callback', 'renderer']
__slots__ = ['callbacks', 'renderer']

def __init__(self, default=None, callback=None, renderer=None, **kwargs):
self.callback = None
self.callbacks = {}
self.renderer = (lambda x: x) if renderer is None else renderer
super(_View, self).__init__(default, **kwargs)

def __set__(self, obj, val):
super(_View, self).__set__(obj, val)
if self.callback:
self.callback(self.renderer(val))
obj_id = id(obj)
if obj_id in self.callbacks:
self.callbacks[obj_id](self.renderer(val))


class HTML(_View):
Expand Down