Merge pull request #6493 from SylvainCorlay/python_widget_registry

Creating a widget registry on the Python side.
This commit is contained in:
Thomas Kluyver 2014-10-30 16:10:50 -07:00
commit adeefa8578
11 changed files with 52 additions and 13 deletions

View File

@ -1,4 +1,4 @@
from .widget import Widget, DOMWidget, CallbackDispatcher
from .widget import Widget, DOMWidget, CallbackDispatcher, register
from .widget_bool import Checkbox, ToggleButton
from .widget_button import Button

View File

@ -75,12 +75,26 @@ def _show_traceback(method):
ip.showtraceback()
return m
def register(key=None):
"""Returns a decorator registering a widget class in the widget registry.
If no key is provided, the class name is used as a key. A key is
provided for each core IPython widget so that the frontend can use
this key regardless of the language of the kernel"""
def wrap(widget):
l = key if key is not None else widget.__module__ + widget.__name__
Widget.widget_types[l] = widget
return widget
return wrap
class Widget(LoggingConfigurable):
#-------------------------------------------------------------------------
# Class attributes
#-------------------------------------------------------------------------
_widget_construction_callback = None
widgets = {}
widget_types = {}
@staticmethod
def on_widget_constructed(callback):

View File

@ -13,7 +13,7 @@ Represents a boolean using a widget.
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from .widget import DOMWidget
from .widget import DOMWidget, register
from IPython.utils.traitlets import Unicode, Bool, CaselessStrEnum
from IPython.utils.warn import DeprecatedClass
@ -27,11 +27,13 @@ class _Bool(DOMWidget):
disabled = Bool(False, help="Enable or disable user changes.", sync=True)
@register('IPython.Checkbox')
class Checkbox(_Bool):
"""Displays a boolean `value`."""
_view_name = Unicode('CheckboxView', sync=True)
@register('IPython.ToggleButton')
class ToggleButton(_Bool):
"""Displays a boolean `value`."""

View File

@ -6,10 +6,11 @@ Represents a container that can be used to group other widgets.
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
from .widget import DOMWidget
from .widget import DOMWidget, register
from IPython.utils.traitlets import Unicode, Tuple, TraitError, Int, CaselessStrEnum
from IPython.utils.warn import DeprecatedClass
@register('IPython.Box')
class Box(DOMWidget):
"""Displays multiple widgets in a group."""
_view_name = Unicode('BoxView', sync=True)
@ -44,6 +45,7 @@ class Box(DOMWidget):
child._handle_displayed()
@register('IPython.Popup')
class Popup(Box):
"""Displays multiple widgets in an in page popup div."""
_view_name = Unicode('PopupView', sync=True)
@ -52,6 +54,7 @@ class Popup(Box):
button_text = Unicode(sync=True)
@register('IPython.FlexBox')
class FlexBox(Box):
"""Displays multiple widgets using the flexible box model."""
_view_name = Unicode('FlexBoxView', sync=True)

View File

@ -14,13 +14,14 @@ click events on the button and trigger backend code when the clicks are fired.
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from .widget import DOMWidget, CallbackDispatcher
from .widget import DOMWidget, CallbackDispatcher, register
from IPython.utils.traitlets import Unicode, Bool, CaselessStrEnum
from IPython.utils.warn import DeprecatedClass
#-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------
@register('IPython.Button')
class Button(DOMWidget):
"""Button widget.

View File

@ -13,7 +13,7 @@ Represents an unbounded float using a widget.
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from .widget import DOMWidget
from .widget import DOMWidget, register
from IPython.utils.traitlets import Unicode, CFloat, Bool, CaselessStrEnum, Tuple
from IPython.utils.warn import DeprecatedClass
@ -43,14 +43,17 @@ class _BoundedFloat(_Float):
self.value = min(max(new, self.min), self.max)
@register('IPython.FloatText')
class FloatText(_Float):
_view_name = Unicode('FloatTextView', sync=True)
@register('IPython.BoundedFloatText')
class BoundedFloatText(_BoundedFloat):
_view_name = Unicode('FloatTextView', sync=True)
@register('IPython.FloatSlider')
class FloatSlider(_BoundedFloat):
_view_name = Unicode('FloatSliderView', sync=True)
orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
@ -61,6 +64,7 @@ class FloatSlider(_BoundedFloat):
slider_color = Unicode(sync=True)
@register('IPython.FloatProgress')
class FloatProgress(_BoundedFloat):
_view_name = Unicode('ProgressView', sync=True)
@ -163,6 +167,7 @@ class _BoundedFloatRange(_FloatRange):
self.lower = low
@register('IPython.FloatRangeSlider')
class FloatRangeSlider(_BoundedFloatRange):
_view_name = Unicode('FloatSliderView', sync=True)
orientation = CaselessStrEnum(values=['horizontal', 'vertical'],

View File

@ -15,13 +15,14 @@ Represents an image in the frontend using a widget.
#-----------------------------------------------------------------------------
import base64
from .widget import DOMWidget
from .widget import DOMWidget, register
from IPython.utils.traitlets import Unicode, CUnicode, Bytes
from IPython.utils.warn import DeprecatedClass
#-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------
@register('IPython.Image')
class Image(DOMWidget):
"""Displays an image as a widget.

View File

@ -13,7 +13,7 @@ Represents an unbounded int using a widget.
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from .widget import DOMWidget
from .widget import DOMWidget, register
from IPython.utils.traitlets import Unicode, CInt, Bool, CaselessStrEnum, Tuple
from IPython.utils.warn import DeprecatedClass
@ -56,16 +56,19 @@ class _BoundedInt(_Int):
if new > self.max:
raise ValueError("setting min > max")
@register('IPython.IntText')
class IntText(_Int):
"""Textbox widget that represents a int."""
_view_name = Unicode('IntTextView', sync=True)
@register('IPython.BoundedIntText')
class BoundedIntText(_BoundedInt):
"""Textbox widget that represents a int bounded by a minimum and maximum value."""
_view_name = Unicode('IntTextView', sync=True)
@register('IPython.IntSlider')
class IntSlider(_BoundedInt):
"""Slider widget that represents a int bounded by a minimum and maximum value."""
_view_name = Unicode('IntSliderView', sync=True)
@ -77,6 +80,7 @@ class IntSlider(_BoundedInt):
slider_color = Unicode(sync=True)
@register('IPython.IntProgress')
class IntProgress(_BoundedInt):
"""Progress bar that represents a int bounded by a minimum and maximum value."""
_view_name = Unicode('ProgressView', sync=True)
@ -176,6 +180,7 @@ class _BoundedIntRange(_IntRange):
self.upper = high
self.lower = low
@register('IPython.IntRangeSlider')
class IntRangeSlider(_BoundedIntRange):
_view_name = Unicode('IntSliderView', sync=True)
orientation = CaselessStrEnum(values=['horizontal', 'vertical'],

View File

@ -17,7 +17,7 @@ Represents an enumeration using a widget.
from collections import OrderedDict
from threading import Lock
from .widget import DOMWidget
from .widget import DOMWidget, register
from IPython.utils.traitlets import Unicode, List, Bool, Any, Dict, TraitError, CaselessStrEnum
from IPython.utils.py3compat import unicode_type
from IPython.utils.warn import DeprecatedClass
@ -114,6 +114,7 @@ class _Selection(DOMWidget):
self.value_lock.release()
@register('IPython.ToggleButtons')
class ToggleButtons(_Selection):
"""Group of toggle buttons that represent an enumeration. Only one toggle
button can be toggled at any point in time."""
@ -124,7 +125,7 @@ class ToggleButtons(_Selection):
default_value='', allow_none=True, sync=True, help="""Use a
predefined styling for the buttons.""")
@register('IPython.Dropdown')
class Dropdown(_Selection):
"""Allows you to select a single item from a dropdown."""
_view_name = Unicode('DropdownView', sync=True)
@ -134,13 +135,15 @@ class Dropdown(_Selection):
default_value='', allow_none=True, sync=True, help="""Use a
predefined styling for the buttons.""")
@register('IPython.RadioButtons')
class RadioButtons(_Selection):
"""Group of radio buttons that represent an enumeration. Only one radio
button can be toggled at any point in time."""
_view_name = Unicode('RadioButtonsView', sync=True)
@register('IPython.Select')
class Select(_Selection):
"""Listbox that only allows one item to be selected at any given time."""
_view_name = Unicode('SelectView', sync=True)

View File

@ -14,7 +14,7 @@ pages.
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from .widget_box import Box
from .widget_box import Box, register
from IPython.utils.traitlets import Unicode, Dict, CInt
from IPython.utils.warn import DeprecatedClass
@ -51,12 +51,13 @@ class _SelectionContainer(Box):
else:
return None
@register('IPython.Accordion')
class Accordion(_SelectionContainer):
"""Displays children each on a separate accordion page."""
_view_name = Unicode('AccordionView', sync=True)
@register('IPython.Tab')
class Tab(_SelectionContainer):
"""Displays children each on a separate accordion tab."""
_view_name = Unicode('TabView', sync=True)

View File

@ -13,7 +13,7 @@ Represents a unicode string using a widget.
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from .widget import DOMWidget, CallbackDispatcher
from .widget import DOMWidget, CallbackDispatcher, register
from IPython.utils.traitlets import Unicode, Bool
from IPython.utils.warn import DeprecatedClass
@ -28,17 +28,20 @@ class _String(DOMWidget):
placeholder = Unicode("", help="Placeholder text to display when nothing has been typed", sync=True)
@register('IPython.HTML')
class HTML(_String):
"""Renders the string `value` as HTML."""
_view_name = Unicode('HTMLView', sync=True)
@register('IPython.Latex')
class Latex(_String):
"""Renders math inside the string `value` as Latex (requires $ $ or $$ $$
and similar latex tags)."""
_view_name = Unicode('LatexView', sync=True)
@register('IPython.Textarea')
class Textarea(_String):
"""Multiline text area widget."""
_view_name = Unicode('TextareaView', sync=True)
@ -47,6 +50,7 @@ class Textarea(_String):
self.send({"method": "scroll_to_bottom"})
@register('IPython.Text')
class Text(_String):
"""Single line textbox widget."""
_view_name = Unicode('TextView', sync=True)