From dedbc3b0e1dc3e0c04c2d987f6f11bf113f036ec Mon Sep 17 00:00:00 2001 From: Jonathan Frederic Date: Tue, 25 Feb 2014 09:46:27 -0800 Subject: [PATCH 1/5] Changed children list to CTuple. Also removed some unused refences in the widget code and added support for tuples in the pack/unpack code. --- IPython/html/widgets/widget.py | 8 ++++---- IPython/html/widgets/widget_bool.py | 2 +- IPython/html/widgets/widget_container.py | 6 +++--- IPython/html/widgets/widget_float.py | 2 +- IPython/html/widgets/widget_int.py | 2 +- IPython/html/widgets/widget_selectioncontainer.py | 2 +- IPython/html/widgets/widget_string.py | 2 +- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/IPython/html/widgets/widget.py b/IPython/html/widgets/widget.py index 4c576b387..6db671568 100644 --- a/IPython/html/widgets/widget.py +++ b/IPython/html/widgets/widget.py @@ -308,7 +308,7 @@ class Widget(LoggingConfigurable): their model ids. Return value must be JSON-able.""" if isinstance(x, dict): return {k: self._pack_widgets(v) for k, v in x.items()} - elif isinstance(x, list): + elif isinstance(x, (list, tuple)): return [self._pack_widgets(v) for v in x] elif isinstance(x, Widget): return x.model_id @@ -322,7 +322,7 @@ class Widget(LoggingConfigurable): their model ids.""" if isinstance(x, dict): return {k: self._unpack_widgets(v) for k, v in x.items()} - elif isinstance(x, list): + elif isinstance(x, (list, tuple)): return [self._unpack_widgets(v) for v in x] elif isinstance(x, string_types): return x if x not in Widget.widgets else Widget.widgets[x] @@ -412,7 +412,7 @@ class DOMWidget(Widget): be added to. """ class_list = class_names - if isinstance(class_list, list): + if isinstance(class_list, (list, tuple)): class_list = ' '.join(class_list) self.send({ @@ -433,7 +433,7 @@ class DOMWidget(Widget): be removed from. """ class_list = class_names - if isinstance(class_list, list): + if isinstance(class_list, (list, tuple)): class_list = ' '.join(class_list) self.send({ diff --git a/IPython/html/widgets/widget_bool.py b/IPython/html/widgets/widget_bool.py index cce6b9811..211ba312c 100644 --- a/IPython/html/widgets/widget_bool.py +++ b/IPython/html/widgets/widget_bool.py @@ -14,7 +14,7 @@ Represents a boolean using a widget. # Imports #----------------------------------------------------------------------------- from .widget import DOMWidget -from IPython.utils.traitlets import Unicode, Bool, List +from IPython.utils.traitlets import Unicode, Bool #----------------------------------------------------------------------------- # Classes diff --git a/IPython/html/widgets/widget_container.py b/IPython/html/widgets/widget_container.py index ddfccd560..c254f6623 100644 --- a/IPython/html/widgets/widget_container.py +++ b/IPython/html/widgets/widget_container.py @@ -14,7 +14,7 @@ Represents a container that can be used to group other widgets. # Imports #----------------------------------------------------------------------------- from .widget import DOMWidget -from IPython.utils.traitlets import Unicode, Bool, List, Instance +from IPython.utils.traitlets import Unicode, CTuple, Instance #----------------------------------------------------------------------------- # Classes @@ -24,8 +24,8 @@ class ContainerWidget(DOMWidget): # Keys, all private and managed by helper methods. Flexible box model # classes... - children = List(Instance(DOMWidget)) - _children = List(Instance(DOMWidget), sync=True) + children = CTuple(Instance(DOMWidget)) + _children = CTuple(Instance(DOMWidget), sync=True) def _children_changed(self, name, old, new): """Validate children list. diff --git a/IPython/html/widgets/widget_float.py b/IPython/html/widgets/widget_float.py index ec1868e9a..cad915eb0 100644 --- a/IPython/html/widgets/widget_float.py +++ b/IPython/html/widgets/widget_float.py @@ -14,7 +14,7 @@ Represents an unbounded float using a widget. # Imports #----------------------------------------------------------------------------- from .widget import DOMWidget -from IPython.utils.traitlets import Unicode, CFloat, Bool, List, Enum +from IPython.utils.traitlets import Unicode, CFloat, Bool, Enum #----------------------------------------------------------------------------- # Classes diff --git a/IPython/html/widgets/widget_int.py b/IPython/html/widgets/widget_int.py index 5a706bf5e..4c9aa2c0d 100644 --- a/IPython/html/widgets/widget_int.py +++ b/IPython/html/widgets/widget_int.py @@ -14,7 +14,7 @@ Represents an unbounded int using a widget. # Imports #----------------------------------------------------------------------------- from .widget import DOMWidget -from IPython.utils.traitlets import Unicode, CInt, Bool, List, Enum +from IPython.utils.traitlets import Unicode, CInt, Bool, Enum #----------------------------------------------------------------------------- # Classes diff --git a/IPython/html/widgets/widget_selectioncontainer.py b/IPython/html/widgets/widget_selectioncontainer.py index 0f9156ab8..ef9155992 100644 --- a/IPython/html/widgets/widget_selectioncontainer.py +++ b/IPython/html/widgets/widget_selectioncontainer.py @@ -15,7 +15,7 @@ pages. # Imports #----------------------------------------------------------------------------- from .widget_container import ContainerWidget -from IPython.utils.traitlets import Unicode, Dict, CInt, List, Instance +from IPython.utils.traitlets import Unicode, Dict, CInt #----------------------------------------------------------------------------- # Classes diff --git a/IPython/html/widgets/widget_string.py b/IPython/html/widgets/widget_string.py index e3633365d..e3505c5b4 100644 --- a/IPython/html/widgets/widget_string.py +++ b/IPython/html/widgets/widget_string.py @@ -14,7 +14,7 @@ Represents a unicode string using a widget. # Imports #----------------------------------------------------------------------------- from .widget import DOMWidget, CallbackDispatcher -from IPython.utils.traitlets import Unicode, Bool, List +from IPython.utils.traitlets import Unicode, Bool #----------------------------------------------------------------------------- # Classes From 7855de2f132c2dd964a175ab95416af3e4e054f1 Mon Sep 17 00:00:00 2001 From: Jonathan Frederic Date: Tue, 25 Feb 2014 13:50:58 -0800 Subject: [PATCH 2/5] s/CTuple/Tuple --- IPython/html/widgets/widget_container.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/IPython/html/widgets/widget_container.py b/IPython/html/widgets/widget_container.py index c254f6623..528e42bac 100644 --- a/IPython/html/widgets/widget_container.py +++ b/IPython/html/widgets/widget_container.py @@ -14,7 +14,7 @@ Represents a container that can be used to group other widgets. # Imports #----------------------------------------------------------------------------- from .widget import DOMWidget -from IPython.utils.traitlets import Unicode, CTuple, Instance +from IPython.utils.traitlets import Unicode, Tuple, Instance #----------------------------------------------------------------------------- # Classes @@ -24,8 +24,8 @@ class ContainerWidget(DOMWidget): # Keys, all private and managed by helper methods. Flexible box model # classes... - children = CTuple(Instance(DOMWidget)) - _children = CTuple(Instance(DOMWidget), sync=True) + children = Tuple(Instance(DOMWidget)) + _children = Tuple(Instance(DOMWidget), sync=True) def _children_changed(self, name, old, new): """Validate children list. From c89ec19fcaa80f47766ac6e965143d21291285f6 Mon Sep 17 00:00:00 2001 From: MinRK Date: Tue, 25 Feb 2014 14:36:46 -0800 Subject: [PATCH 3/5] don't check shape of ContainerWidget.children --- IPython/html/widgets/widget_container.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/IPython/html/widgets/widget_container.py b/IPython/html/widgets/widget_container.py index 528e42bac..f8661fc42 100644 --- a/IPython/html/widgets/widget_container.py +++ b/IPython/html/widgets/widget_container.py @@ -14,18 +14,27 @@ Represents a container that can be used to group other widgets. # Imports #----------------------------------------------------------------------------- from .widget import DOMWidget -from IPython.utils.traitlets import Unicode, Tuple, Instance +from IPython.utils.traitlets import Unicode, Tuple, Instance, TraitError #----------------------------------------------------------------------------- # Classes #----------------------------------------------------------------------------- + +class TupleOfDOMWidgets(Tuple): + """Like Tuple(Instance(DOMWidget)), but without checking length.""" + def validate_elements(self, obj, value): + for v in value: + if not isinstance(v, DOMWidget): + raise TraitError("Container.children must be DOMWidgets, not %r" % v) + return value + class ContainerWidget(DOMWidget): _view_name = Unicode('ContainerView', sync=True) # Keys, all private and managed by helper methods. Flexible box model # classes... - children = Tuple(Instance(DOMWidget)) - _children = Tuple(Instance(DOMWidget), sync=True) + children = TupleOfDOMWidgets() + _children = TupleOfDOMWidgets(sync=True) def _children_changed(self, name, old, new): """Validate children list. From cc1ba8b1f7dd19dc30e6844be5b73b56b41596ff Mon Sep 17 00:00:00 2001 From: MinRK Date: Tue, 25 Feb 2014 15:00:48 -0800 Subject: [PATCH 4/5] remove incorrect is instance check in children_changed --- IPython/html/widgets/widget_container.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/IPython/html/widgets/widget_container.py b/IPython/html/widgets/widget_container.py index f8661fc42..92a7f9c0f 100644 --- a/IPython/html/widgets/widget_container.py +++ b/IPython/html/widgets/widget_container.py @@ -45,7 +45,7 @@ class ContainerWidget(DOMWidget): http://www.peterbe.com/plog/uniqifiers-benchmark which provides the inspiration for using this implementation. Below I've implemented the `f5` algorithm using Python comprehensions.""" - if new is not None and isinstance(new, list): + if new is not None: seen = {} def add_item(i): seen[i.model_id] = True From d8b6a1a0ba8e28ceac14240487698fb396825255 Mon Sep 17 00:00:00 2001 From: MinRK Date: Tue, 25 Feb 2014 19:08:43 -0800 Subject: [PATCH 5/5] don't validate ContainerWidget.children just use a plain Tuple, so we don't have to define a new Trait --- IPython/html/widgets/widget_container.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/IPython/html/widgets/widget_container.py b/IPython/html/widgets/widget_container.py index 92a7f9c0f..ccbe4d8b2 100644 --- a/IPython/html/widgets/widget_container.py +++ b/IPython/html/widgets/widget_container.py @@ -14,27 +14,20 @@ Represents a container that can be used to group other widgets. # Imports #----------------------------------------------------------------------------- from .widget import DOMWidget -from IPython.utils.traitlets import Unicode, Tuple, Instance, TraitError +from IPython.utils.traitlets import Unicode, Tuple, TraitError #----------------------------------------------------------------------------- # Classes #----------------------------------------------------------------------------- -class TupleOfDOMWidgets(Tuple): - """Like Tuple(Instance(DOMWidget)), but without checking length.""" - def validate_elements(self, obj, value): - for v in value: - if not isinstance(v, DOMWidget): - raise TraitError("Container.children must be DOMWidgets, not %r" % v) - return value - class ContainerWidget(DOMWidget): _view_name = Unicode('ContainerView', sync=True) - # Keys, all private and managed by helper methods. Flexible box model - # classes... - children = TupleOfDOMWidgets() - _children = TupleOfDOMWidgets(sync=True) + # Child widgets in the container. + # Using a tuple here to force reassignment to update the list. + # When a proper notifying-list trait exists, that is what should be used here. + children = Tuple() + _children = Tuple(sync=True) def _children_changed(self, name, old, new): """Validate children list.