Skip to content

Commit f5e3648

Browse files
committed
Default values for widget model attributes
1 parent 50918d3 commit f5e3648

File tree

3 files changed

+95
-38
lines changed

3 files changed

+95
-38
lines changed

ipywidgets/static/widgets/js/widget.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ define(["./utils",
3939
};
4040

4141
var WidgetModel = Backbone.Model.extend({
42+
43+
defaults: {
44+
_model_module: null,
45+
_model_name: "WidgetModel",
46+
_view_module: "",
47+
_view_name: null,
48+
msg_throttle: 3
49+
},
50+
4251
constructor: function (widget_manager, model_id, comm) {
4352
/**
4453
* Constructor
@@ -548,7 +557,11 @@ define(["./utils",
548557
}
549558
};
550559

551-
var DOMWidgetModel = WidgetModel.extend({}, {
560+
var DOMWidgetModel = WidgetModel.extend({
561+
defaults: _.extend(WidgetModel.prototype.defaults, {
562+
layout: undefined,
563+
}),
564+
}, {
552565
serializers: _.extend({
553566
layout: {deserialize: unpack_models},
554567
}, WidgetModel.serializers),
@@ -843,16 +856,16 @@ define(["./utils",
843856
var DOMWidgetView = WidgetView.extend(DOMWidgetViewMixin);
844857

845858
var widget = {
846-
'unpack_models': unpack_models,
847-
'WidgetModel': WidgetModel,
848-
'WidgetViewMixin': WidgetViewMixin,
849-
'DOMWidgetViewMixin': DOMWidgetViewMixin,
850-
'ViewList': ViewList,
851-
'DOMWidgetModel': DOMWidgetModel,
859+
unpack_models: unpack_models,
860+
WidgetModel: WidgetModel,
861+
WidgetViewMixin: WidgetViewMixin,
862+
DOMWidgetViewMixin: DOMWidgetViewMixin,
863+
ViewList: ViewList,
864+
DOMWidgetModel: DOMWidgetModel,
852865

853866
// For backwards compatibility.
854-
'WidgetView': WidgetView,
855-
'DOMWidgetView': DOMWidgetView,
867+
WidgetView: WidgetView,
868+
DOMWidgetView: DOMWidgetView,
856869
};
857870

858871
return widget;

ipywidgets/static/widgets/js/widget_bool.js

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,28 @@ if (typeof define !== 'function') { var define = require('./requirejs-shim')(mod
77
define([
88
"./widget",
99
"jquery",
10+
"underscore",
1011
"bootstrap",
11-
], function(widget, $){
12+
], function(widget, $, _){
13+
14+
var BoolModel = widget.DOMWidgetModel.extend({
15+
defaults: _.extend(widget.DOMWidgetModel.prototype.defaults, {
16+
value: false,
17+
description: "",
18+
disabled: false,
19+
_model_name: "BoolModel"
20+
}),
21+
});
22+
23+
var CheckboxModel = BoolModel.extend({
24+
defaults: _.extend(BoolModel.prototype.defaults, {
25+
_view_name: "CheckboxView",
26+
_model_name: "CheckboxModel"
27+
}),
28+
});
1229

1330
var CheckboxView = widget.DOMWidgetView.extend({
14-
render : function(){
31+
render: function(){
1532
/**
1633
* Called when view is rendered.
1734
*/
@@ -52,7 +69,7 @@ define([
5269
this.touch();
5370
},
5471

55-
update : function(options){
72+
update: function(options){
5673
/**
5774
* Update the contents of this view
5875
*
@@ -76,9 +93,18 @@ define([
7693
},
7794
});
7895

96+
var ToggleButtonModel = BoolModel.extend({
97+
defaults: _.extend(BoolModel.prototype.defaults, {
98+
_view_name: "ToggleButtonView",
99+
_model_name: "ToggleButtonModel",
100+
tooltip: "",
101+
icon: "",
102+
button_style: "primary",
103+
}),
104+
});
79105

80106
var ToggleButtonView = widget.DOMWidgetView.extend({
81-
render : function() {
107+
render: function() {
82108
/**
83109
* Called when view is rendered.
84110
*/
@@ -110,7 +136,7 @@ define([
110136
this.update_mapped_classes(class_map, 'button_style', previous_trait_value);
111137
},
112138

113-
update : function(options){
139+
update: function(options){
114140
/**
115141
* Update the contents of this view
116142
*
@@ -152,6 +178,13 @@ define([
152178
},
153179
});
154180

181+
var ValidModel = BoolModel.extend({
182+
defaults: _.extend(BoolModel.prototype.defaults, {
183+
readout: "Invalid",
184+
_view_name: "ValidView",
185+
_model_name: "ValidModel"
186+
}),
187+
});
155188

156189
var ValidView = widget.DOMWidgetView.extend({
157190
render: function() {
@@ -162,6 +195,7 @@ define([
162195
this.listenTo(this.model, "change", this.update, this);
163196
this.update();
164197
},
198+
165199
update: function() {
166200
/**
167201
* Update the contents of this view
@@ -190,8 +224,12 @@ define([
190224

191225

192226
return {
193-
'CheckboxView': CheckboxView,
194-
'ToggleButtonView': ToggleButtonView,
195-
'ValidView': ValidView,
227+
BoolModel: BoolModel,
228+
CheckboxModel: CheckboxModel,
229+
CheckboxView: CheckboxView,
230+
ToggleButtonModel: ToggleButtonModel,
231+
ToggleButtonView: ToggleButtonView,
232+
ValidModel: ValidModel,
233+
ValidView: ValidView,
196234
};
197235
});

ipywidgets/widgets/widget_bool.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,42 @@ def __init__(self, value=None, **kwargs):
2222
kwargs['value'] = value
2323
super(_Bool, self).__init__(**kwargs)
2424

25+
_model_name = Unicode('BoolModel', sync=True)
26+
2527

2628
@register('Jupyter.Checkbox')
2729
class Checkbox(_Bool):
2830
"""Displays a boolean `value` in the form of a checkbox.
2931
30-
Parameters
31-
----------
32-
value : {True,False}
33-
value of the checkbox: True-checked, False-unchecked
34-
description : str
35-
description displayed next to the checkbox
36-
"""
32+
Parameters
33+
----------
34+
value : {True,False}
35+
value of the checkbox: True-checked, False-unchecked
36+
description : str
37+
description displayed next to the checkbox
38+
"""
3739
_view_name = Unicode('CheckboxView', sync=True)
40+
_model_name = Unicode('CheckboxModel', sync=True)
3841

3942

4043
@register('Jupyter.ToggleButton')
4144
class ToggleButton(_Bool):
4245
"""Displays a boolean `value` in the form of a toggle button.
4346
44-
Parameters
45-
----------
46-
value : {True,False}
47-
value of the toggle button: True-pressed, False-unpressed
48-
description : str
49-
description displayed next to the button
50-
tooltip: str
51-
tooltip caption of the toggle button
52-
icon: str
53-
font-awesome icon name
54-
"""
47+
Parameters
48+
----------
49+
value : {True,False}
50+
value of the toggle button: True-pressed, False-unpressed
51+
description : str
52+
description displayed next to the button
53+
tooltip: str
54+
tooltip caption of the toggle button
55+
icon: str
56+
font-awesome icon name
57+
"""
5558
_view_name = Unicode('ToggleButtonView', sync=True)
59+
_model_name = Unicode('ToggleButtonModel', sync=True)
60+
5661
tooltip = Unicode(help="Tooltip caption of the toggle button.", sync=True)
5762
icon = Unicode('', help= "Font-awesome icon.", sync=True)
5863

@@ -64,14 +69,15 @@ class ToggleButton(_Bool):
6469

6570
@register('Jupyter.Valid')
6671
class Valid(_Bool):
67-
6872
"""Displays a boolean `value` in the form of a green check (True / valid)
6973
or a red cross (False / invalid).
7074
7175
Parameters
7276
----------
7377
value: {True,False}
7478
value of the Valid widget
75-
"""
76-
readout = Unicode(help="Message displayed when the value is False", sync=True)
79+
"""
80+
readout = Unicode('Invalid', help="Message displayed when the value is False", sync=True)
7781
_view_name = Unicode('ValidView', sync=True)
82+
_model_name = Unicode('ValidModel', sync=True)
83+

0 commit comments

Comments
 (0)