##// END OF EJS Templates
added eurosym package to define \euro symbol
added eurosym package to define \euro symbol

File last commit:

r19485:ee6267a3
r20084:3d7ba6af
Show More
widget_selection.py
172 lines | 6.5 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
Thomas Kluyver
Import Tuple traitlet for Selection widget
r19060 from IPython.utils.traitlets import (
Unicode, Bool, Any, Dict, TraitError, CaselessStrEnum, Tuple
)
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")
Jonathan Frederic
Something
r19058 value_name = Unicode(help="The name of the selected value", sync=True)
Jonathan Frederic
Don't magically convert values to a list of tuples.
r19134 values = Any(help="""List of (key, value) tuples or dict of values that the
user can select.
MinRK
Make `SelectionWidget.values` a dict...
r15024
Jonathan Frederic
Something
r19058 The keys of this list are the strings that will be displayed in the UI,
MinRK
Make `SelectionWidget.values` a dict...
r15024 representing the actual Python choices.
Jonathan Frederic
Hide variables that shouldn't be exposed to the user
r19485 The keys of this list are also available as _value_names.
MinRK
Make `SelectionWidget.values` a dict...
r15024 """)
Jonathan Frederic
Something
r19058
Jonathan Frederic
Hide variables that shouldn't be exposed to the user
r19485 _values_dict = Dict()
_value_names = Tuple(sync=True)
_value_values = Tuple()
Jonathan Frederic
Something
r19058
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)
Jonathan Frederic
Something
r19058
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()
Jonathan Frederic
Something
r19058 self.values_lock = Lock()
Jonathan Frederic
Hide variables that shouldn't be exposed to the user
r19485 self.on_trait_change(self._values_readonly_changed, ['_values_dict', '_value_names', '_value_values', '_values'])
MinRK
Make `SelectionWidget.values` a dict...
r15024 if 'values' in kwargs:
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
Jonathan Frederic
Something
r19058 def _make_values(self, x):
# If x is a dict, convert it to list format.
if isinstance(x, (OrderedDict, dict)):
return [(k, v) for k, v in x.items()]
# Make sure x is a list or tuple.
if not isinstance(x, (list, tuple)):
raise ValueError('x')
# If x is an ordinary list, use the values as names.
for y in x:
if not isinstance(y, (list, tuple)) or len(y) < 2:
return [(i, i) for i in x]
# Value is already in the correct format.
return x
MinRK
Make `SelectionWidget.values` a dict...
r15024 def _values_changed(self, name, old, new):
Jonathan Frederic
Something
r19058 """Handles when the values tuple 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.
Jonathan Frederic
Something
r19058 """
if self.values_lock.acquire(False):
try:
Jonathan Frederic
Don't magically convert values to a list of tuples.
r19134 self.values = new
Jonathan Frederic
Typo fix
r19157
Jonathan Frederic
Don't magically convert values to a list of tuples.
r19134 values = self._make_values(new)
Jonathan Frederic
Hide variables that shouldn't be exposed to the user
r19485 self._values_dict = {i[0]: i[1] for i in values}
self._value_names = [i[0] for i in values]
self._value_values = [i[1] for i in values]
Jonathan Frederic
Make selection widget respect order.
r19059 self._value_in_values()
finally:
self.values_lock.release()
Jonathan Frederic
Something
r19058
Sylvain Corlay
forcing value to be in values
r18125 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
Jonathan Frederic
Hide variables that shouldn't be exposed to the user
r19485 if self._value_values:
if self.value not in self._value_values:
self.value = next(iter(self._value_values))
MinRK
Make `SelectionWidget.values` a dict...
r15024
Jonathan Frederic
Something
r19058 def _values_readonly_changed(self, name, old, new):
Jonathan Frederic
Fix readonly lock logic.
r19131 if not self.values_lock.locked():
Jonathan Frederic
Something
r19058 raise TraitError("`.%s` is a read-only trait. Use the `.values` tuple instead." % name)
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
Jonathan Frederic
Hide variables that shouldn't be exposed to the user
r19485 for k,v in self._values_dict.items():
MinRK
Make `SelectionWidget.values` a dict...
r15024 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:
Jonathan Frederic
Hide variables that shouldn't be exposed to the user
r19485 self.value = self._values_dict[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')