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

Commit

Permalink
Merge pull request #43 from ioam/view_param_fix
Browse files Browse the repository at this point in the history
View parameters store callback per Parameterized instance
  • Loading branch information
jbednar authored May 9, 2017
2 parents 8d5af7e + f427562 commit 170cbf0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
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

0 comments on commit 170cbf0

Please sign in to comment.