Show More
@@ -0,0 +1,20 b'' | |||||
|
1 | """Test trait types of the widget packages.""" | |||
|
2 | ||||
|
3 | # Copyright (c) IPython Development Team. | |||
|
4 | # Distributed under the terms of the Modified BSD License. | |||
|
5 | ||||
|
6 | from unittest import TestCase | |||
|
7 | from IPython.utils.traitlets import HasTraits | |||
|
8 | from IPython.utils.tests.test_traitlets import TraitTestBase | |||
|
9 | from IPython.html.widgets import Color | |||
|
10 | ||||
|
11 | ||||
|
12 | class ColorTrait(HasTraits): | |||
|
13 | value = Color("black") | |||
|
14 | ||||
|
15 | ||||
|
16 | class TestColor(TraitTestBase): | |||
|
17 | obj = ColorTrait() | |||
|
18 | ||||
|
19 | _good_values = ["blue", "#AA0", "#FFFFFF"] | |||
|
20 | _bad_values = ["vanilla", "blues"] |
@@ -0,0 +1,28 b'' | |||||
|
1 | # encoding: utf-8 | |||
|
2 | """ | |||
|
3 | Trait types for html widgets. | |||
|
4 | """ | |||
|
5 | ||||
|
6 | # Copyright (c) IPython Development Team. | |||
|
7 | # Distributed under the terms of the Modified BSD License. | |||
|
8 | ||||
|
9 | import re | |||
|
10 | from IPython.utils import traitlets | |||
|
11 | ||||
|
12 | #----------------------------------------------------------------------------- | |||
|
13 | # Utilities | |||
|
14 | #----------------------------------------------------------------------------- | |||
|
15 | ||||
|
16 | _color_names = ['aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure', 'beige', 'bisque', 'black', 'blanchedalmond', 'blue', 'blueviolet', 'brown', 'burlywood', 'cadetblue', 'chartreuse', 'chocolate', 'coral', 'cornflowerblue', 'cornsilk', 'crimson', 'cyan', 'darkblue', 'darkcyan', 'darkgoldenrod', 'darkgray', 'darkgreen', 'darkkhaki', 'darkmagenta', 'darkolivegreen', 'darkorange', 'darkorchid', 'darkred', 'darksalmon', 'darkseagreen', 'darkslateblue', 'darkslategray', 'darkturquoise', 'darkviolet', 'deeppink', 'deepskyblue', 'dimgray', 'dodgerblue', 'firebrick', 'floralwhite', 'forestgreen', 'fuchsia', 'gainsboro', 'ghostwhite', 'gold', 'goldenrod', 'gray', 'green', 'greenyellow', 'honeydew', 'hotpink', 'indianred ', 'indigo ', 'ivory', 'khaki', 'lavender', 'lavenderblush', 'lawngreen', 'lemonchiffon', 'lightblue', 'lightcoral', 'lightcyan', 'lightgoldenrodyellow', 'lightgray', 'lightgreen', 'lightpink', 'lightsalmon', 'lightseagreen', 'lightskyblue', 'lightslategray', 'lightsteelblue', 'lightyellow', 'lime', 'limegreen', 'linen', 'magenta', 'maroon', 'mediumaquamarine', 'mediumblue', 'mediumorchid', 'mediumpurple', 'mediumseagreen', 'mediumslateblue', 'mediumspringgreen', 'mediumturquoise', 'mediumvioletred', 'midnightblue', 'mintcream', 'mistyrose', 'moccasin', 'navajowhite', 'navy', 'oldlace', 'olive', 'olivedrab', 'orange', 'orangered', 'orchid', 'palegoldenrod', 'palegreen', 'paleturquoise', 'palevioletred', 'papayawhip', 'peachpuff', 'peru', 'pink', 'plum', 'powderblue', 'purple', 'rebeccapurple', 'red', 'rosybrown', 'royalblue', 'saddlebrown', 'salmon', 'sandybrown', 'seagreen', 'seashell', 'sienna', 'silver', 'skyblue', 'slateblue', 'slategray', 'snow', 'springgreen', 'steelblue', 'tan', 'teal', 'thistle', 'tomato', 'turquoise', 'violet', 'wheat', 'white', 'whitesmoke', 'yellow', 'yellowgreen'] | |||
|
17 | _color_re = re.compile(r'#[a-fA-F0-9]{3}(?:[a-fA-F0-9]{3})?$') | |||
|
18 | ||||
|
19 | ||||
|
20 | class Color(traitlets.Unicode): | |||
|
21 | """A string holding a valid HTML color such as 'blue', '#060482', '#A80'""" | |||
|
22 | ||||
|
23 | info_text = 'a valid HTML color' | |||
|
24 | ||||
|
25 | def validate(self, obj, value): | |||
|
26 | if value.lower() in _color_names or _color_re.match(value): | |||
|
27 | return value | |||
|
28 | self.error(obj, value) |
@@ -1,5 +1,7 b'' | |||||
1 | from .widget import Widget, DOMWidget, CallbackDispatcher, register |
|
1 | from .widget import Widget, DOMWidget, CallbackDispatcher, register | |
2 |
|
2 | |||
|
3 | from .trait_types import Color | |||
|
4 | ||||
3 | from .widget_bool import Checkbox, ToggleButton |
|
5 | from .widget_bool import Checkbox, ToggleButton | |
4 | from .widget_button import Button |
|
6 | from .widget_button import Button | |
5 | from .widget_box import Box, FlexBox, HBox, VBox |
|
7 | from .widget_box import Box, FlexBox, HBox, VBox |
@@ -22,6 +22,7 b' from IPython.utils.importstring import import_item' | |||||
22 | from IPython.utils.traitlets import Unicode, Dict, Instance, Bool, List, \ |
|
22 | from IPython.utils.traitlets import Unicode, Dict, Instance, Bool, List, \ | |
23 | CaselessStrEnum, Tuple, CUnicode, Int, Set |
|
23 | CaselessStrEnum, Tuple, CUnicode, Int, Set | |
24 | from IPython.utils.py3compat import string_types |
|
24 | from IPython.utils.py3compat import string_types | |
|
25 | from .trait_types import Color | |||
25 |
|
26 | |||
26 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
27 | # Classes |
|
28 | # Classes | |
@@ -438,9 +439,9 b' class DOMWidget(Widget):' | |||||
438 | padding = CUnicode(sync=True) |
|
439 | padding = CUnicode(sync=True) | |
439 | margin = CUnicode(sync=True) |
|
440 | margin = CUnicode(sync=True) | |
440 |
|
441 | |||
441 |
color = |
|
442 | color = Color(None, allow_none=True, sync=True) | |
442 |
background_color = |
|
443 | background_color = Color(None, allow_none=True, sync=True) | |
443 |
border_color = |
|
444 | border_color = Color(None, allow_none=True, sync=True) | |
444 |
|
445 | |||
445 | border_width = CUnicode(sync=True) |
|
446 | border_width = CUnicode(sync=True) | |
446 | border_radius = CUnicode(sync=True) |
|
447 | border_radius = CUnicode(sync=True) |
@@ -14,6 +14,7 b' Represents an unbounded float using a widget.' | |||||
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | from .widget import DOMWidget, register |
|
16 | from .widget import DOMWidget, register | |
|
17 | from .trait_types import Color | |||
17 | from IPython.utils.traitlets import Unicode, CFloat, Bool, CaselessStrEnum, Tuple |
|
18 | from IPython.utils.traitlets import Unicode, CFloat, Bool, CaselessStrEnum, Tuple | |
18 | from IPython.utils.warn import DeprecatedClass |
|
19 | from IPython.utils.warn import DeprecatedClass | |
19 |
|
20 | |||
@@ -133,7 +134,7 b' class FloatSlider(_BoundedFloat):' | |||||
133 | default_value='horizontal', help="Vertical or horizontal.", sync=True) |
|
134 | default_value='horizontal', help="Vertical or horizontal.", sync=True) | |
134 | _range = Bool(False, help="Display a range selector", sync=True) |
|
135 | _range = Bool(False, help="Display a range selector", sync=True) | |
135 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) |
|
136 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) | |
136 |
slider_color = |
|
137 | slider_color = Color(None, allow_none=True, sync=True) | |
137 |
|
138 | |||
138 |
|
139 | |||
139 | @register('IPython.FloatProgress') |
|
140 | @register('IPython.FloatProgress') | |
@@ -287,7 +288,7 b' class FloatRangeSlider(_BoundedFloatRange):' | |||||
287 | default_value='horizontal', help="Vertical or horizontal.", sync=True) |
|
288 | default_value='horizontal', help="Vertical or horizontal.", sync=True) | |
288 | _range = Bool(True, help="Display a range selector", sync=True) |
|
289 | _range = Bool(True, help="Display a range selector", sync=True) | |
289 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) |
|
290 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) | |
290 |
slider_color = |
|
291 | slider_color = Color(None, allow_none=True, sync=True) | |
291 |
|
292 | |||
292 | # Remove in IPython 4.0 |
|
293 | # Remove in IPython 4.0 | |
293 | FloatTextWidget = DeprecatedClass(FloatText, 'FloatTextWidget') |
|
294 | FloatTextWidget = DeprecatedClass(FloatText, 'FloatTextWidget') |
@@ -14,6 +14,7 b' Represents an unbounded int using a widget.' | |||||
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | from .widget import DOMWidget, register |
|
16 | from .widget import DOMWidget, register | |
|
17 | from .trait_types import Color | |||
17 | from IPython.utils.traitlets import Unicode, CInt, Bool, CaselessStrEnum, Tuple |
|
18 | from IPython.utils.traitlets import Unicode, CInt, Bool, CaselessStrEnum, Tuple | |
18 | from IPython.utils.warn import DeprecatedClass |
|
19 | from IPython.utils.warn import DeprecatedClass | |
19 |
|
20 | |||
@@ -87,7 +88,7 b' class IntSlider(_BoundedInt):' | |||||
87 | default_value='horizontal', help="Vertical or horizontal.", sync=True) |
|
88 | default_value='horizontal', help="Vertical or horizontal.", sync=True) | |
88 | _range = Bool(False, help="Display a range selector", sync=True) |
|
89 | _range = Bool(False, help="Display a range selector", sync=True) | |
89 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) |
|
90 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) | |
90 |
slider_color = |
|
91 | slider_color = Color(None, allow_none=True, sync=True) | |
91 |
|
92 | |||
92 |
|
93 | |||
93 | @register('IPython.IntProgress') |
|
94 | @register('IPython.IntProgress') | |
@@ -198,7 +199,7 b' class IntRangeSlider(_BoundedIntRange):' | |||||
198 | default_value='horizontal', help="Vertical or horizontal.", sync=True) |
|
199 | default_value='horizontal', help="Vertical or horizontal.", sync=True) | |
199 | _range = Bool(True, help="Display a range selector", sync=True) |
|
200 | _range = Bool(True, help="Display a range selector", sync=True) | |
200 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) |
|
201 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) | |
201 |
slider_color = |
|
202 | slider_color = Color(None, allow_none=True, sync=True) | |
202 |
|
203 | |||
203 | # Remove in IPython 4.0 |
|
204 | # Remove in IPython 4.0 | |
204 | IntTextWidget = DeprecatedClass(IntText, 'IntTextWidget') |
|
205 | IntTextWidget = DeprecatedClass(IntText, 'IntTextWidget') |
@@ -919,7 +919,6 b' class TestDottedObjectName(TraitTestBase):' | |||||
919 |
|
919 | |||
920 |
|
920 | |||
921 | class TCPAddressTrait(HasTraits): |
|
921 | class TCPAddressTrait(HasTraits): | |
922 |
|
||||
923 | value = TCPAddress() |
|
922 | value = TCPAddress() | |
924 |
|
923 | |||
925 | class TestTCPAddress(TraitTestBase): |
|
924 | class TestTCPAddress(TraitTestBase): | |
@@ -979,6 +978,19 b' class TestInstanceList(TraitTestBase):' | |||||
979 | _good_values = [[Foo(), Foo(), None], []] |
|
978 | _good_values = [[Foo(), Foo(), None], []] | |
980 | _bad_values = [['1', 2,], '1', [Foo], None] |
|
979 | _bad_values = [['1', 2,], '1', [Foo], None] | |
981 |
|
980 | |||
|
981 | class UnionListTrait(HasTraits): | |||
|
982 | ||||
|
983 | value = List(Int() | Bool()) | |||
|
984 | ||||
|
985 | class TestUnionListTrait(HasTraits): | |||
|
986 | ||||
|
987 | obj = UnionListTrait() | |||
|
988 | ||||
|
989 | _default_value = [] | |||
|
990 | _good_values = [[True, 1], [False, True]] | |||
|
991 | _bad_values = [[1, 'True'], False] | |||
|
992 | ||||
|
993 | ||||
982 | class LenListTrait(HasTraits): |
|
994 | class LenListTrait(HasTraits): | |
983 |
|
995 | |||
984 | value = List(Int, [0], minlen=1, maxlen=2) |
|
996 | value = List(Int, [0], minlen=1, maxlen=2) |
General Comments 0
You need to be logged in to leave comments.
Login now