forked from ipython/ipython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget_selection.py
More file actions
95 lines (75 loc) · 3.72 KB
/
Copy pathwidget_selection.py
File metadata and controls
95 lines (75 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""SelectionWidget class.
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
#-----------------------------------------------------------------------------
from threading import Lock
from .widget import DOMWidget
from IPython.utils.traitlets import Unicode, List, Bool, Any, Dict
#-----------------------------------------------------------------------------
# SelectionWidget
#-----------------------------------------------------------------------------
class _SelectionWidget(DOMWidget):
value = Any(help="Selected value")
values = List(help="List of values the user can select")
labels = List(help="""List of string representations for each value.
These string representations are used to display the values in the
front-end.""", sync=True) # Only synced to the back-end.
disabled = Bool(False, help="Enable or disable user changes", sync=True)
description = Unicode(help="Description of the value this widget represents", sync=True)
_value = Unicode(sync=True) # Bi-directionally synced.
def __init__(self, *pargs, **kwargs):
"""Constructor"""
self.value_lock = Lock()
self.on_trait_change(self._string_value_set, ['_value'])
DOMWidget.__init__(self, *pargs, **kwargs)
def _labels_changed(self, name=None, old=None, new=None):
"""Handles when the value_names Dict has been changed.
This method sets the _reverse_value_names Dict to the inverse of the new
value for the value_names Dict."""
if len(new) != len(self.values):
raise TypeError('Labels list must be the same size as the values list.')
def _values_changed(self, name=None, old=None, new=None):
"""Handles when the value_names Dict has been changed.
This method sets the _reverse_value_names Dict to the inverse of the new
value for the value_names Dict."""
if len(new) != len(self.labels):
self.labels = [(self.labels[i] if i < len(self.labels) else str(v)) for i, v in enumerate(new)]
def _value_changed(self, name, old, new):
"""Called when value has been changed"""
if self.value_lock.acquire(False):
try:
# Make sure the value is in the list of values.
if new in self.values:
# Set the string version of the value.
self._value = self.labels[self.values.index(new)]
else:
raise TypeError('Value must be a value in the values list.')
finally:
self.value_lock.release()
def _string_value_set(self, name, old, new):
"""Called when _value has been changed."""
if self.value_lock.acquire(False):
try:
if new in self.labels:
self.value = self.values[self.labels.index(new)]
else:
self.value = None
finally:
self.value_lock.release()
class ToggleButtonsWidget(_SelectionWidget):
_view_name = Unicode('ToggleButtonsView', sync=True)
class DropdownWidget(_SelectionWidget):
_view_name = Unicode('DropdownView', sync=True)
class RadioButtonsWidget(_SelectionWidget):
_view_name = Unicode('RadioButtonsView', sync=True)
class SelectWidget(_SelectionWidget):
_view_name = Unicode('SelectView', sync=True)