Skip to content

Commit 5d5adf4

Browse files
authored
Merge pull request #1253 from jasongrout/styles
Abstract out the notion of a traitlet instance type that can be initialized with a dict
2 parents 128bb36 + fea23fd commit 5d5adf4

File tree

6 files changed

+38
-50
lines changed

6 files changed

+38
-50
lines changed

ipywidgets/widgets/domwidget.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,16 @@
55

66
from traitlets import Unicode, Bool, Tuple, default
77
from .widget import Widget, widget_serialization
8-
from .trait_types import Color
9-
from .widget_layout import Layout, LayoutTraitType
8+
from .trait_types import Color, InstanceDict
9+
from .widget_layout import Layout
1010

1111

1212
class DOMWidget(Widget):
1313
"""Widget that can be inserted into the DOM"""
1414

1515
_model_name = Unicode('DOMWidgetModel').tag(sync=True)
1616
_dom_classes = Tuple(help="CSS classes applied to widget DOM element").tag(sync=True)
17-
layout = LayoutTraitType().tag(sync=True, **widget_serialization)
18-
19-
@default('layout')
20-
def _default_layout(self):
21-
return Layout()
17+
layout = InstanceDict(Layout).tag(sync=True, **widget_serialization)
2218

2319
def add_class(self, className):
2420
"""

ipywidgets/widgets/trait_types.py

+17
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,20 @@ def datetime_from_json(js, manager):
7878
'from_json': datetime_from_json,
7979
'to_json': datetime_to_json
8080
}
81+
82+
class InstanceDict(traitlets.Instance):
83+
"""An instance trait which coerces a dict to an instance.
84+
85+
This lets the instance be specified as a dict, which is used to initialize the instance.
86+
87+
Also, we default to a trivial instance, even if args and kwargs is not specified."""
88+
89+
def validate(self, obj, value):
90+
if isinstance(value, dict):
91+
return super(InstanceDict, self).validate(obj, self.klass(**value))
92+
else:
93+
return super(InstanceDict, self).validate(obj, value)
94+
95+
def make_dynamic_default(self):
96+
return self.klass(*(self.default_args or ()),
97+
**(self.default_kwargs or {}))

ipywidgets/widgets/widget_button.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from .widget import CallbackDispatcher, register, widget_serialization
1212
from .widget_core import CoreWidget
1313
from .widget_style import Style
14-
from .trait_types import Color
14+
from .trait_types import Color, InstanceDict
1515

1616
from traitlets import Unicode, Bool, CaselessStrEnum, Instance, validate, default
1717
import warnings
@@ -55,11 +55,7 @@ class Button(DOMWidget, CoreWidget):
5555
values=['primary', 'success', 'info', 'warning', 'danger', ''], default_value='',
5656
help="""Use a predefined styling for the button.""").tag(sync=True)
5757

58-
style = Instance(ButtonStyle).tag(sync=True, **widget_serialization)
59-
60-
@default('style')
61-
def _default_style(self):
62-
return ButtonStyle()
58+
style = InstanceDict(ButtonStyle).tag(sync=True, **widget_serialization)
6359

6460
def __init__(self, **kwargs):
6561
super(Button, self).__init__(**kwargs)

ipywidgets/widgets/widget_float.py

+11-24
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
# Copyright (c) Jupyter Development Team.
77
# Distributed under the terms of the Modified BSD License.
88

9+
from traitlets import (
10+
Instance, Unicode, CFloat, Bool, CaselessStrEnum, Tuple, TraitError, validate, default
11+
)
912
from .domwidget import LabeledWidget
13+
from .trait_types import InstanceDict
1014
from .valuewidget import ValueWidget
1115
from .widget import register, widget_serialization
1216
from .widget_core import CoreWidget
13-
from .trait_types import Color
14-
from .widget_int import ProgressStyle
15-
from traitlets import (
16-
Instance, Unicode, CFloat, Bool, Int, CaselessStrEnum, Tuple, TraitError, validate, default
17-
)
17+
from .widget_int import ProgressStyle, SliderStyle
1818

1919

2020
class _Float(LabeledWidget, ValueWidget, CoreWidget):
@@ -75,8 +75,6 @@ class FloatText(_Float):
7575
value displayed
7676
description : str
7777
description displayed next to the text box
78-
color : str Unicode color code (eg. '#C13535')
79-
color of the value displayed
8078
"""
8179
_view_name = Unicode('FloatTextView').tag(sync=True)
8280
_model_name = Unicode('FloatTextModel').tag(sync=True)
@@ -98,8 +96,6 @@ class BoundedFloatText(_BoundedFloat):
9896
maximal value of the range of possible values displayed
9997
description : str
10098
description displayed next to the textbox
101-
color : str Unicode color code (eg. '#C13535')
102-
color of the value displayed
10399
"""
104100
_view_name = Unicode('FloatTextView').tag(sync=True)
105101
_model_name = Unicode('FloatTextModel').tag(sync=True)
@@ -129,10 +125,6 @@ class FloatSlider(_BoundedFloat):
129125
default is '.2f', specifier for the format function used to represent
130126
slider value for human consumption, modeled after Python 3's format
131127
specification mini-language (PEP 3101).
132-
slider_color : str Unicode color code (eg. '#C13535')
133-
color of the slider
134-
color : str Unicode color code (eg. '#C13535')
135-
color of the value displayed (if readout == True)
136128
"""
137129
_view_name = Unicode('FloatSliderView').tag(sync=True)
138130
_model_name = Unicode('FloatSliderModel').tag(sync=True)
@@ -141,9 +133,10 @@ class FloatSlider(_BoundedFloat):
141133
_range = Bool(False, help="Display a range selector").tag(sync=True)
142134
readout = Bool(True, help="Display the current value of the slider next to it.").tag(sync=True)
143135
readout_format = Unicode('.2f', help="Format for the readout").tag(sync=True)
144-
slider_color = Color(None, allow_none=True).tag(sync=True)
145136
continuous_update = Bool(True, help="Update the value of the widget as the user is holding the slider.").tag(sync=True)
146137

138+
style = InstanceDict(SliderStyle).tag(sync=True, **widget_serialization)
139+
147140

148141
@register
149142
class FloatProgress(_BoundedFloat):
@@ -177,11 +170,7 @@ class FloatProgress(_BoundedFloat):
177170
default_value='', allow_none=True,
178171
help="Use a predefined styling for the progess bar.").tag(sync=True)
179172

180-
style = Instance(ProgressStyle).tag(sync=True, **widget_serialization)
181-
182-
@default('style')
183-
def _default_style(self):
184-
return ProgressStyle()
173+
style = InstanceDict(ProgressStyle).tag(sync=True, **widget_serialization)
185174

186175

187176
class _FloatRange(_Float):
@@ -270,16 +259,14 @@ class FloatRangeSlider(_BoundedFloatRange):
270259
default is '.2f', specifier for the format function used to represent
271260
slider value for human consumption, modeled after Python 3's format
272261
specification mini-language (PEP 3101).
273-
slider_color : str Unicode color code (eg. '#C13535')
274-
color of the slider
275-
color : str Unicode color code (eg. '#C13535')
276-
color of the value displayed (if readout == True)
277262
"""
278263
_view_name = Unicode('FloatSliderView').tag(sync=True)
279264
_model_name = Unicode('FloatSliderModel').tag(sync=True)
280265
orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
281266
default_value='horizontal', help="Vertical or horizontal.").tag(sync=True)
282267
_range = Bool(True, help="Display a range selector").tag(sync=True)
283268
readout = Bool(True, help="Display the current value of the slider next to it.").tag(sync=True)
284-
slider_color = Color(None, allow_none=True).tag(sync=True)
269+
readout_format = Unicode('.2f', help="Format for the readout").tag(sync=True)
285270
continuous_update = Bool(True, help="Update the value of the widget as the user is sliding the slider.").tag(sync=True)
271+
272+
style = InstanceDict(SliderStyle).tag(sync=True, **widget_serialization)

ipywidgets/widgets/widget_int.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .widget_core import CoreWidget
1313
from .widget_style import Style
1414
from traitlets import Instance
15-
from .trait_types import Color
15+
from .trait_types import Color, InstanceDict
1616
from traitlets import (
1717
Unicode, CInt, Bool, CaselessStrEnum, Tuple, TraitError, default, validate
1818
)
@@ -164,11 +164,7 @@ class IntSlider(_BoundedInt):
164164
readout_format = Unicode('d', help="Format for the readout").tag(sync=True)
165165
continuous_update = Bool(True, help="Update the value of the widget as the user is holding the slider.").tag(sync=True)
166166

167-
style = Instance(SliderStyle).tag(sync=True, **widget_serialization)
168-
169-
@default('style')
170-
def _default_style(self):
171-
return SliderStyle()
167+
style = InstanceDict(SliderStyle).tag(sync=True, **widget_serialization)
172168

173169

174170
@register
@@ -192,11 +188,7 @@ class IntProgress(_BoundedInt):
192188
values=['success', 'info', 'warning', 'danger', ''], default_value='',
193189
help="""Use a predefined styling for the progess bar.""").tag(sync=True)
194190

195-
style = Instance(ProgressStyle).tag(sync=True, **widget_serialization)
196-
197-
@default('style')
198-
def _default_style(self):
199-
return ProgressStyle()
191+
style = InstanceDict(ProgressStyle).tag(sync=True, **widget_serialization)
200192

201193

202194
class _IntRange(_Int):
@@ -280,8 +272,8 @@ class IntRangeSlider(_BoundedIntRange):
280272
default_value='horizontal', help="Vertical or horizontal.").tag(sync=True)
281273
_range = Bool(True, help="Display a range selector").tag(sync=True)
282274
readout = Bool(True, help="Display the current value of the slider next to it.").tag(sync=True)
283-
slider_color = Color(None, allow_none=True).tag(sync=True)
284275
continuous_update = Bool(True, help="Update the value of the widget as the user is sliding the slider.").tag(sync=True)
276+
style = InstanceDict(SliderStyle).tag(sync=True, **widget_serialization)
285277

286278

287279
@register

ipywidgets/widgets/widget_layout.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ class LayoutTraitType(Instance):
6666

6767
def validate(self, obj, value):
6868
if isinstance(value, dict):
69-
return super(LayoutTraitType, self).validate(obj, Layout(**value))
69+
return super(LayoutTraitType, self).validate(obj, self.klass(**value))
7070
else:
7171
return super(LayoutTraitType, self).validate(obj, value)

0 commit comments

Comments
 (0)