1-to-1 widget / view mapping

This commit is contained in:
Jonathan Frederic 2014-01-14 16:24:35 +00:00
parent c1c7bb7563
commit 047a90538f
8 changed files with 61 additions and 37 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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)
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)

View File

@ -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)

View File

@ -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.')