##// END OF EJS Templates
Add the unlink method to javascript links to maintain compatibility with traitlet links
Add the unlink method to javascript links to maintain compatibility with traitlet links

File last commit:

r18533:b46a0ee8
r19390:e461f122
Show More
widget_selection.py
156 lines | 6.3 KiB | text/x-python | PythonLexer
Jonathan Frederic
Renamed *Widget to *,...
r17598 """Selection classes.
Jonathan Frederic
Cleaned up Python widget code.
r14283
Represents an enumeration using a widget.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2013, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
MinRK
Make `SelectionWidget.values` a dict...
r15024
from collections import OrderedDict
Jonathan Frederic
Support multiple types in selection widget.
r14698 from threading import Lock
Sylvain Corlay
registering core widgets
r18531 from .widget import DOMWidget, register
Jonathan Frederic
Added bootstrap3 progress bar classes
r17729 from IPython.utils.traitlets import Unicode, List, Bool, Any, Dict, TraitError, CaselessStrEnum
MinRK
Make `SelectionWidget.values` a dict...
r15024 from IPython.utils.py3compat import unicode_type
Jonathan Frederic
Renamed *Widget to *,...
r17598 from IPython.utils.warn import DeprecatedClass
Jonathan Frederic
Add selection widget
r14242
Jonathan Frederic
Cleaned up Python widget code.
r14283 #-----------------------------------------------------------------------------
# SelectionWidget
#-----------------------------------------------------------------------------
Jonathan Frederic
Renamed *Widget to *,...
r17598 class _Selection(DOMWidget):
MinRK
Make `SelectionWidget.values` a dict...
r15024 """Base class for Selection widgets
``values`` can be specified as a list or dict. If given as a list,
it will be transformed to a dict of the form ``{str(value):value}``.
"""
value = Any(help="Selected value")
values = Dict(help="""Dictionary of {name: value} the user can select.
The keys of this dictionary are the strings that will be displayed in the UI,
representing the actual Python choices.
The keys of this dictionary are also available as value_names.
""")
value_name = Unicode(help="The name of the selected value", sync=True)
MinRK
value_names is read-only
r15025 value_names = List(Unicode, help="""Read-only list of names for each value.
MinRK
Make `SelectionWidget.values` a dict...
r15024
If values is specified as a list, this is the string representation of each element.
Otherwise, it is the keys of the values dictionary.
These strings are used to display the choices in the front-end.""", sync=True)
Jonathan Frederic
sync=True isntead of a keys list
r14588 disabled = Bool(False, help="Enable or disable user changes", sync=True)
description = Unicode(help="Description of the value this widget represents", sync=True)
MinRK
Make `SelectionWidget.values` a dict...
r15024
Sylvain Corlay
forcing value to be in values
r18125
MinRK
Make `SelectionWidget.values` a dict...
r15024 def __init__(self, *args, **kwargs):
Jonathan Frederic
Support multiple types in selection widget.
r14698 self.value_lock = Lock()
MinRK
value_names is read-only
r15025 self._in_values_changed = False
MinRK
Make `SelectionWidget.values` a dict...
r15024 if 'values' in kwargs:
values = kwargs['values']
# convert list values to an dict of {str(v):v}
if isinstance(values, list):
# preserve list order with an OrderedDict
kwargs['values'] = OrderedDict((unicode_type(v), v) for v in values)
jdavidheiser
widget_selection update...
r16192 # python3.3 turned on hash randomization by default - this means that sometimes, randomly
# we try to set value before setting values, due to dictionary ordering. To fix this, force
# the setting of self.values right now, before anything else runs
jdavidheiser
Update widget_selection.py
r16196 self.values = kwargs.pop('values')
MinRK
Make `SelectionWidget.values` a dict...
r15024 DOMWidget.__init__(self, *args, **kwargs)
Sylvain Corlay
forcing value to be in values
r18125 self._value_in_values()
MinRK
Make `SelectionWidget.values` a dict...
r15024
def _values_changed(self, name, old, new):
"""Handles when the values dict has been changed.
Jonathan Frederic
Support multiple types in selection widget.
r14698
MinRK
Make `SelectionWidget.values` a dict...
r15024 Setting values implies setting value names from the keys of the dict.
"""
MinRK
value_names is read-only
r15025 self._in_values_changed = True
try:
self.value_names = list(new.keys())
finally:
self._in_values_changed = False
Sylvain Corlay
forcing value to be in values
r18125 self._value_in_values()
def _value_in_values(self):
MinRK
don't allow empty selection in selection widgets...
r15046 # ensure that the chosen value is one of the choices
Sylvain Corlay
forcing value to be in values
r18125 if self.values:
if self.value not in self.values.values():
self.value = next(iter(self.values.values()))
MinRK
Make `SelectionWidget.values` a dict...
r15024
def _value_names_changed(self, name, old, new):
MinRK
value_names is read-only
r15025 if not self._in_values_changed:
raise TraitError("value_names is a read-only proxy to values.keys(). Use the values dict instead.")
Jonathan Frederic
Support multiple types in selection widget.
r14698
def _value_changed(self, name, old, new):
"""Called when value has been changed"""
if self.value_lock.acquire(False):
try:
MinRK
don't allow empty selection in selection widgets...
r15046 # Reverse dictionary lookup for the value name
MinRK
Make `SelectionWidget.values` a dict...
r15024 for k,v in self.values.items():
if new == v:
# set the selected value name
self.value_name = k
return
MinRK
undo failed changes...
r15401 # undo the change, and raise KeyError
self.value = old
MinRK
don't allow empty selection in selection widgets...
r15046 raise KeyError(new)
Jonathan Frederic
Support multiple types in selection widget.
r14698 finally:
self.value_lock.release()
MinRK
Make `SelectionWidget.values` a dict...
r15024 def _value_name_changed(self, name, old, new):
"""Called when the value name has been changed (typically by the frontend)."""
Jonathan Frederic
Support multiple types in selection widget.
r14698 if self.value_lock.acquire(False):
try:
MinRK
don't allow empty selection in selection widgets...
r15046 self.value = self.values[new]
Jonathan Frederic
Support multiple types in selection widget.
r14698 finally:
self.value_lock.release()
Jonathan Frederic
1-to-1 widget / view mapping
r14592
Sylvain Corlay
jupyter -> IPython
r18533 @register('IPython.ToggleButtons')
Jonathan Frederic
Renamed *Widget to *,...
r17598 class ToggleButtons(_Selection):
Jonathan Frederic
Added some doc strings on the widgets....
r17602 """Group of toggle buttons that represent an enumeration. Only one toggle
button can be toggled at any point in time."""
Jonathan Frederic
s/view_name/_view_name
r14701 _view_name = Unicode('ToggleButtonsView', sync=True)
Jonathan Frederic
Create base widget classes
r14670
Jonathan Frederic
Added Bootstrap specific classes,...
r17728 button_style = CaselessStrEnum(
values=['primary', 'success', 'info', 'warning', 'danger', ''],
default_value='', allow_none=True, sync=True, help="""Use a
predefined styling for the buttons.""")
Sylvain Corlay
jupyter -> IPython
r18533 @register('IPython.Dropdown')
Jonathan Frederic
Renamed *Widget to *,...
r17598 class Dropdown(_Selection):
Jonathan Frederic
Added some doc strings on the widgets....
r17602 """Allows you to select a single item from a dropdown."""
Jonathan Frederic
s/view_name/_view_name
r14701 _view_name = Unicode('DropdownView', sync=True)
Jonathan Frederic
1-to-1 widget / view mapping
r14592
Jonathan Frederic
Added bootstrap3 progress bar classes
r17729 button_style = CaselessStrEnum(
values=['primary', 'success', 'info', 'warning', 'danger', ''],
default_value='', allow_none=True, sync=True, help="""Use a
predefined styling for the buttons.""")
Sylvain Corlay
jupyter -> IPython
r18533 @register('IPython.RadioButtons')
Jonathan Frederic
Renamed *Widget to *,...
r17598 class RadioButtons(_Selection):
Jonathan Frederic
Added some doc strings on the widgets....
r17602 """Group of radio buttons that represent an enumeration. Only one radio
button can be toggled at any point in time."""
Jonathan Frederic
s/view_name/_view_name
r14701 _view_name = Unicode('RadioButtonsView', sync=True)
Jonathan Frederic
1-to-1 widget / view mapping
r14592
Sylvain Corlay
registering core widgets
r18531
Sylvain Corlay
jupyter -> IPython
r18533 @register('IPython.Select')
Jonathan Frederic
Renamed *Widget to *,...
r17598 class Select(_Selection):
Jonathan Frederic
Added some doc strings on the widgets....
r17602 """Listbox that only allows one item to be selected at any given time."""
Jonathan Frederic
Renamed widgets......
r14834 _view_name = Unicode('SelectView', sync=True)
Jonathan Frederic
Renamed *Widget to *,...
r17598
Jonathan Frederic
Added some doc strings on the widgets....
r17602
# Remove in IPython 4.0
Jonathan Frederic
Renamed *Widget to *,...
r17598 ToggleButtonsWidget = DeprecatedClass(ToggleButtons, 'ToggleButtonsWidget')
DropdownWidget = DeprecatedClass(Dropdown, 'DropdownWidget')
RadioButtonsWidget = DeprecatedClass(RadioButtons, 'RadioButtonsWidget')
SelectWidget = DeprecatedClass(Select, 'SelectWidget')