##// END OF EJS Templates
Merge pull request #7826 from SylvainCorlay/color_trait...
Min RK -
r20802:d6cb4618 merge
parent child Browse files
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 1 from .widget import Widget, DOMWidget, CallbackDispatcher, register
2 2
3 from .trait_types import Color
4
3 5 from .widget_bool import Checkbox, ToggleButton
4 6 from .widget_button import Button
5 7 from .widget_box import Box, FlexBox, HBox, VBox
@@ -22,6 +22,7 b' from IPython.utils.importstring import import_item'
22 22 from IPython.utils.traitlets import Unicode, Dict, Instance, Bool, List, \
23 23 CaselessStrEnum, Tuple, CUnicode, Int, Set
24 24 from IPython.utils.py3compat import string_types
25 from .trait_types import Color
25 26
26 27 #-----------------------------------------------------------------------------
27 28 # Classes
@@ -438,9 +439,9 b' class DOMWidget(Widget):'
438 439 padding = CUnicode(sync=True)
439 440 margin = CUnicode(sync=True)
440 441
441 color = Unicode(sync=True)
442 background_color = Unicode(sync=True)
443 border_color = Unicode(sync=True)
442 color = Color(None, allow_none=True, sync=True)
443 background_color = Color(None, allow_none=True, sync=True)
444 border_color = Color(None, allow_none=True, sync=True)
444 445
445 446 border_width = CUnicode(sync=True)
446 447 border_radius = CUnicode(sync=True)
@@ -14,6 +14,7 b' Represents an unbounded float using a widget.'
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16 from .widget import DOMWidget, register
17 from .trait_types import Color
17 18 from IPython.utils.traitlets import Unicode, CFloat, Bool, CaselessStrEnum, Tuple
18 19 from IPython.utils.warn import DeprecatedClass
19 20
@@ -133,7 +134,7 b' class FloatSlider(_BoundedFloat):'
133 134 default_value='horizontal', help="Vertical or horizontal.", sync=True)
134 135 _range = Bool(False, help="Display a range selector", sync=True)
135 136 readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
136 slider_color = Unicode(sync=True)
137 slider_color = Color(None, allow_none=True, sync=True)
137 138
138 139
139 140 @register('IPython.FloatProgress')
@@ -287,7 +288,7 b' class FloatRangeSlider(_BoundedFloatRange):'
287 288 default_value='horizontal', help="Vertical or horizontal.", sync=True)
288 289 _range = Bool(True, help="Display a range selector", sync=True)
289 290 readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
290 slider_color = Unicode(sync=True)
291 slider_color = Color(None, allow_none=True, sync=True)
291 292
292 293 # Remove in IPython 4.0
293 294 FloatTextWidget = DeprecatedClass(FloatText, 'FloatTextWidget')
@@ -14,6 +14,7 b' Represents an unbounded int using a widget.'
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16 from .widget import DOMWidget, register
17 from .trait_types import Color
17 18 from IPython.utils.traitlets import Unicode, CInt, Bool, CaselessStrEnum, Tuple
18 19 from IPython.utils.warn import DeprecatedClass
19 20
@@ -87,7 +88,7 b' class IntSlider(_BoundedInt):'
87 88 default_value='horizontal', help="Vertical or horizontal.", sync=True)
88 89 _range = Bool(False, help="Display a range selector", sync=True)
89 90 readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
90 slider_color = Unicode(sync=True)
91 slider_color = Color(None, allow_none=True, sync=True)
91 92
92 93
93 94 @register('IPython.IntProgress')
@@ -198,7 +199,7 b' class IntRangeSlider(_BoundedIntRange):'
198 199 default_value='horizontal', help="Vertical or horizontal.", sync=True)
199 200 _range = Bool(True, help="Display a range selector", sync=True)
200 201 readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
201 slider_color = Unicode(sync=True)
202 slider_color = Color(None, allow_none=True, sync=True)
202 203
203 204 # Remove in IPython 4.0
204 205 IntTextWidget = DeprecatedClass(IntText, 'IntTextWidget')
@@ -919,7 +919,6 b' class TestDottedObjectName(TraitTestBase):'
919 919
920 920
921 921 class TCPAddressTrait(HasTraits):
922
923 922 value = TCPAddress()
924 923
925 924 class TestTCPAddress(TraitTestBase):
@@ -979,6 +978,19 b' class TestInstanceList(TraitTestBase):'
979 978 _good_values = [[Foo(), Foo(), None], []]
980 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 994 class LenListTrait(HasTraits):
983 995
984 996 value = List(Int, [0], minlen=1, maxlen=2)
General Comments 0
You need to be logged in to leave comments. Login now