From 047a90538f82a7078b83eb85c2e449ee474375bd Mon Sep 17 00:00:00 2001 From: Jonathan Frederic Date: Tue, 14 Jan 2014 16:24:35 +0000 Subject: [PATCH] 1-to-1 widget / view mapping --- IPython/html/widgets/__init__.py | 18 ++++---- IPython/html/widgets/widget_bool.py | 7 +++- IPython/html/widgets/widget_container.py | 3 ++ IPython/html/widgets/widget_float.py | 2 +- IPython/html/widgets/widget_int.py | 2 +- IPython/html/widgets/widget_selection.py | 17 ++++++-- .../html/widgets/widget_selectioncontainer.py | 8 +++- IPython/html/widgets/widget_string.py | 41 ++++++++++--------- 8 files changed, 61 insertions(+), 37 deletions(-) diff --git a/IPython/html/widgets/__init__.py b/IPython/html/widgets/__init__.py index 69b5dee52..e64125480 100644 --- a/IPython/html/widgets/__init__.py +++ b/IPython/html/widgets/__init__.py @@ -1,13 +1,13 @@ from .widget import Widget, DOMWidget -from .widget_bool import BoolWidget +from .widget_bool import CheckBoxWidget, ToggleButtonWidget from .widget_button import ButtonWidget -from .widget_container import ContainerWidget -from .widget_float import FloatWidget -from .widget_float_range import FloatRangeWidget +from .widget_container import ContainerWidget, ModalWidget +from .widget_float import FloatTextWidget +from .widget_float_range import BoundedFloatTextWidget, FloatSliderWidget, FloatProgressWidget from .widget_image import ImageWidget -from .widget_int import IntWidget -from .widget_int_range import IntRangeWidget -from .widget_selection import SelectionWidget -from .widget_selectioncontainer import SelectionContainerWidget -from .widget_string import StringWidget +from .widget_int import IntTextWidget +from .widget_int_range import BoundedIntTextWidget, IntSliderWidget, IntProgressWidget +from .widget_selection import RadioButtonsWidget, ToggleButtonsWidget, DropdownWidget, ListBoxWidget +from .widget_selectioncontainer import TabWidget, AccordionWidget +from .widget_string import HTMLWidget, LatexWidget, TextBoxWidget, TextAreaWidget diff --git a/IPython/html/widgets/widget_bool.py b/IPython/html/widgets/widget_bool.py index 98e03b38d..830f55a23 100644 --- a/IPython/html/widgets/widget_bool.py +++ b/IPython/html/widgets/widget_bool.py @@ -19,11 +19,14 @@ from IPython.utils.traitlets import Unicode, Bool, List #----------------------------------------------------------------------------- # Classes #----------------------------------------------------------------------------- -class BoolWidget(DOMWidget): - view_name = Unicode('CheckboxView', sync=True) +class CheckBoxWidget(DOMWidget): + view_name = Unicode('CheckBoxView', sync=True) # Model Keys value = Bool(False, help="Bool value", sync=True) description = Unicode('', help="Description of the boolean (label).", sync=True) disabled = Bool(False, help="Enable or disable user changes.", sync=True) + +class ToggleButtonWidget(CheckboxWidget): + view_name = Unicode('ToggleButtonView', sync=True) \ No newline at end of file diff --git a/IPython/html/widgets/widget_container.py b/IPython/html/widgets/widget_container.py index af291881f..b1aa4ae12 100644 --- a/IPython/html/widgets/widget_container.py +++ b/IPython/html/widgets/widget_container.py @@ -28,3 +28,6 @@ class ContainerWidget(DOMWidget): description = Unicode(sync=True) button_text = Unicode(sync=True) + +class ModalWidget(ContainerWidget): + view_name = Unicode('ModalView', sync=True) diff --git a/IPython/html/widgets/widget_float.py b/IPython/html/widgets/widget_float.py index dc0a63c13..4979e34a5 100644 --- a/IPython/html/widgets/widget_float.py +++ b/IPython/html/widgets/widget_float.py @@ -19,7 +19,7 @@ from IPython.utils.traitlets import Unicode, Float, Bool, List #----------------------------------------------------------------------------- # Classes #----------------------------------------------------------------------------- -class FloatWidget(DOMWidget): +class FloatTextWidget(DOMWidget): view_name = Unicode('FloatTextView', sync=True) # Keys diff --git a/IPython/html/widgets/widget_int.py b/IPython/html/widgets/widget_int.py index b3218eac1..104d1fe77 100644 --- a/IPython/html/widgets/widget_int.py +++ b/IPython/html/widgets/widget_int.py @@ -19,7 +19,7 @@ from IPython.utils.traitlets import Unicode, Int, Bool, List #----------------------------------------------------------------------------- # Classes #----------------------------------------------------------------------------- -class IntWidget(DOMWidget): +class IntTextWidget(DOMWidget): view_name = Unicode('IntTextView', sync=True) # Keys diff --git a/IPython/html/widgets/widget_selection.py b/IPython/html/widgets/widget_selection.py index 2989394fa..f81896350 100644 --- a/IPython/html/widgets/widget_selection.py +++ b/IPython/html/widgets/widget_selection.py @@ -19,12 +19,23 @@ from IPython.utils.traitlets import Unicode, List, Bool #----------------------------------------------------------------------------- # SelectionWidget #----------------------------------------------------------------------------- -class SelectionWidget(DOMWidget): - view_name = Unicode('DropdownView', sync=True) +class ToggleButtonsWidget(DOMWidget): + view_name = Unicode('ToggleButtonsView', sync=True) # Keys value = Unicode(help="Selected value", sync=True) # TODO: Any support values = List(help="List of values the user can select", sync=True) disabled = Bool(False, help="Enable or disable user changes", sync=True) description = Unicode(help="Description of the value this widget represents", sync=True) - \ No newline at end of file + + +class DropdownWidget(SelectionWidget): + view_name = Unicode('DropdownView', sync=True) + + +class RadioButtonsWidget(SelectionWidget): + view_name = Unicode('RadioButtonsView', sync=True) + + +class ListBoxWidget(SelectionWidget): + view_name = Unicode('ListBoxView', sync=True) diff --git a/IPython/html/widgets/widget_selectioncontainer.py b/IPython/html/widgets/widget_selectioncontainer.py index 0b237d49c..3283e3140 100644 --- a/IPython/html/widgets/widget_selectioncontainer.py +++ b/IPython/html/widgets/widget_selectioncontainer.py @@ -20,8 +20,8 @@ from IPython.utils.traitlets import Unicode, Dict, Int, List, Instance #----------------------------------------------------------------------------- # Classes #----------------------------------------------------------------------------- -class SelectionContainerWidget(DOMWidget): - view_name = Unicode('TabView', sync=True) +class AccordionWidget(DOMWidget): + view_name = Unicode('AccordionView', sync=True) # Keys _titles = Dict(help="Titles of the pages", sync=True) @@ -54,3 +54,7 @@ class SelectionContainerWidget(DOMWidget): return self._titles[index] else: return None + + +class TabWidget(AccordionWidget): + view_name = Unicode('TabView', sync=True) diff --git a/IPython/html/widgets/widget_string.py b/IPython/html/widgets/widget_string.py index a96186c56..9be458d47 100644 --- a/IPython/html/widgets/widget_string.py +++ b/IPython/html/widgets/widget_string.py @@ -22,8 +22,8 @@ from IPython.utils.traitlets import Unicode, Bool, List, Int #----------------------------------------------------------------------------- # Classes #----------------------------------------------------------------------------- -class StringWidget(DOMWidget): - view_name = Unicode('TextBoxView', sync=True) +class HTMLWidget(DOMWidget): + view_name = Unicode('HTMLView', sync=True) # Keys value = Unicode(help="String value", sync=True) @@ -31,16 +31,25 @@ class StringWidget(DOMWidget): description = Unicode(help="Description of the value this widget represents", sync=True) - def __init__(self, **kwargs): - super(StringWidget, self).__init__(**kwargs) - self._submission_callbacks = [] - self.on_msg(self._handle_string_msg) +class LatexWidget(HTMLWidget): + view_name = Unicode('LatexView', sync=True) +class TextAreaWidget(HTMLWidget): + view_name = Unicode('TextAreaView', sync=True) + def scroll_to_bottom(self): self.send({"method": "scroll_to_bottom"}) +class TextBoxWidget(HTMLWidget): + view_name = Unicode('TextBoxView', sync=True) + + def __init__(self, **kwargs): + super(StringWidget, self).__init__(**kwargs) + self._submission_callbacks = [] + self.on_msg(self._handle_string_msg) + def _handle_string_msg(self, content): """Handle a msg from the front-end @@ -49,8 +58,8 @@ class StringWidget(DOMWidget): content: dict Content of the msg.""" if 'event' in content and content['event'] == 'submit': - self._handle_submit() - + for handler in self._submission_callbacks: + handler(self) def on_submit(self, callback, remove=False): """Register a callback to handle text submission (triggered when the @@ -67,25 +76,19 @@ class StringWidget(DOMWidget): if remove and callback in self._submission_callbacks: self._submission_callbacks.remove(callback) elif not remove and not callback in self._submission_callbacks: - self._submission_callbacks.append(callback) - - - def _handle_submit(self): - """Handles when a string widget view is submitted.""" - for handler in self._submission_callbacks: - if callable(handler): - argspec = inspect.getargspec(handler) + if callable(callback): + argspec = inspect.getargspec(callback) nargs = len(argspec[0]) # Bound methods have an additional 'self' argument - if isinstance(handler, types.MethodType): + if isinstance(callback, types.MethodType): nargs -= 1 # Call the callback if nargs == 0: - handler() + self._submission_callbacks.append(lambda sender: callback()) elif nargs == 1: - handler(self) + self._submission_callbacks.append(callback) else: raise TypeError('StringWidget submit callback must ' \ 'accept 0 or 1 arguments.')