From 14815cf0edb38dfe0db0eea12355c2e41ed79257 2015-03-23 19:58:20 From: Jonathan Frederic Date: 2015-03-23 19:58:20 Subject: [PATCH] Dynamic traits. --- diff --git a/IPython/html/widgets/widget.py b/IPython/html/widgets/widget.py index 456133d..417acd7 100644 --- a/IPython/html/widgets/widget.py +++ b/IPython/html/widgets/widget.py @@ -292,6 +292,13 @@ class Widget(LoggingConfigurable): True if the callback should be unregistered.""" self._display_callbacks.register_callback(callback, remove=remove) + def add_trait(self, traitname, trait): + """Dynamically add a trait attribute to the Widget.""" + super(Widget, self).add_trait(traitname, trait) + if trait.get_metadata('sync'): + self.keys.append(traitname) + self.send_state(traitname) + #------------------------------------------------------------------------- # Support methods #------------------------------------------------------------------------- diff --git a/IPython/utils/traitlets.py b/IPython/utils/traitlets.py index f2fb877..52ee364 100644 --- a/IPython/utils/traitlets.py +++ b/IPython/utils/traitlets.py @@ -801,6 +801,12 @@ class HasTraits(py3compat.with_metaclass(MetaHasTraits, object)): else: return trait.get_metadata(key, default) + def add_trait(self, traitname, trait): + """Dynamically add a trait attribute to the HasTraits instance.""" + self.__class__ = type(self.__class__.__name__, (self.__class__,), + {traitname: trait}) + trait.set_default_value(self) + #----------------------------------------------------------------------------- # Actual TraitTypes implementations/subclasses #-----------------------------------------------------------------------------