Skip to content

Commit ec80035

Browse files
committed
Add js default values for widget-int and widget-float
1 parent 5bfa8f4 commit ec80035

File tree

4 files changed

+113
-55
lines changed

4 files changed

+113
-55
lines changed

ipywidgets/static/widgets/js/widget_float.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ define([
3131
});
3232

3333
return {
34-
'FloatSliderView': FloatSliderView,
35-
'FloatTextView': FloatTextView,
34+
FloatSliderView: FloatSliderView,
35+
FloatTextView: FloatTextView,
3636
};
3737
});

ipywidgets/static/widgets/js/widget_int.js

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,36 @@ define([
1212
"jquery-ui"
1313
], function(widget, _, $) {
1414

15+
var IntModel = widget.DOMWidgetModel.extend({
16+
defaults: _.extend({}, widget.DOMWidgetModel.prototype.defaults, {
17+
_model_name: "IntModel",
18+
value: 0,
19+
disabled: false,
20+
description: ""
21+
}),
22+
});
23+
24+
var BoundedIntModel = IntModel.extend({
25+
defaults: _.extend({}, IntModel.prototype.defaults, {
26+
_model_name: "BoundedIntModel",
27+
step: 1,
28+
max: 100,
29+
min: 0
30+
}),
31+
});
32+
33+
var IntSliderModel = BoundedIntModel.extend({
34+
defaults: _.extend({}, BoundedIntModel.prototype.defaults, {
35+
_model_name: "IntSliderModel",
36+
_view_name: "IntSliderView",
37+
orientation: "horizontal",
38+
_range: false,
39+
readout: true,
40+
slider_color: null,
41+
continuous_update: true
42+
}),
43+
});
44+
1545
var IntSliderView = widget.DOMWidgetView.extend({
1646
render : function() {
1747
/**
@@ -27,7 +57,7 @@ define([
2757
this.$slider = $('<div />')
2858
.slider({})
2959
.addClass('slider');
30-
// Put the slider in a container
60+
// Put the slider in a container
3161
this.$slider_container = $('<div />')
3262
.addClass('slider-container')
3363
.append(this.$slider);
@@ -41,7 +71,7 @@ define([
4171

4272
this.listenTo(this.model, 'change:slider_handleTextChangecolor', function(sender, value) {
4373
this.$slider.find('a').css('background', value);
44-
}, this);
74+
}, this);
4575
this.listenTo(this.model, 'change:description', function(sender, value) {
4676
this.updateDescription();
4777
}, this);
@@ -336,8 +366,14 @@ define([
336366
},
337367
});
338368

369+
var IntTextModel = IntModel.extend({
370+
defaults: _.extend({}, IntModel.prototype.defaults, {
371+
_model_name: "IntTextModel",
372+
_view_name: "IntTextView"
373+
}),
374+
});
339375

340-
var IntTextView = widget.DOMWidgetView.extend({
376+
var IntTextView = widget.DOMWidgetView.extend({
341377
render : function() {
342378
/**
343379
* Called when view is rendered.
@@ -352,7 +388,7 @@ define([
352388
.addClass('form-control')
353389
.addClass('widget-numeric-text')
354390
.appendTo(this.$el);
355-
391+
356392
this.listenTo(this.model, 'change:description', function(sender, value) {
357393
this.updateDescription();
358394
}, this);
@@ -412,9 +448,9 @@ define([
412448

413449
// Fires only when control is validated or looses focus.
414450
"change input" : "handleChanged"
415-
},
451+
},
416452

417-
handleChanging: function(e) {
453+
handleChanging: function(e) {
418454
/**
419455
* Handles and validates user input.
420456
*
@@ -443,15 +479,15 @@ define([
443479

444480
// Apply the value if it has changed.
445481
if (numericalValue != this.model.get('value')) {
446-
447-
// Calling model.set will trigger all of the other views of the
482+
483+
// Calling model.set will trigger all of the other views of the
448484
// model to update.
449485
this.model.set('value', numericalValue, {updated_view: this});
450486
this.touch();
451487
}
452488
}
453489
},
454-
490+
455491
handleChanged: function(e) {
456492
/**
457493
* Applies validated input.
@@ -464,6 +500,14 @@ define([
464500
_parse_value: parseInt
465501
});
466502

503+
var ProgressModel = BoundedIntModel.extend({
504+
defaults: _.extend({}, BoundedIntModel.prototype.defaults, {
505+
_model_name: "ProgressModel",
506+
_view_name: "ProgressView",
507+
orientation: "horisontal",
508+
bar_style: ""
509+
}),
510+
});
467511

468512
var ProgressView = widget.DOMWidgetView.extend({
469513
render : function() {
@@ -487,9 +531,9 @@ define([
487531
'bottom': 0, 'left': 0,
488532
})
489533
.appendTo(this.$progress);
490-
534+
491535
// Set defaults.
492-
this.update();
536+
this.update();
493537
this.updateDescription();
494538

495539
this.listenTo(this.model, "change:bar_style", this.update_bar_style, this);
@@ -507,14 +551,14 @@ define([
507551
} else {
508552
this.typeset(this.$label, description);
509553
this.$label.show();
510-
}
554+
}
511555
},
512556

513557
update : function() {
514558
/**
515559
* Update the contents of this view
516560
*
517-
* Called when the model is changed. The model may have been
561+
* Called when the model is changed. The model may have been
518562
* changed by another view or by a state update from the back-end.
519563
*/
520564
var value = this.model.get('value');
@@ -544,7 +588,7 @@ define([
544588
});
545589
}
546590
return ProgressView.__super__.update.apply(this);
547-
},
591+
},
548592

549593
update_bar_style: function() {
550594
var class_map = {
@@ -560,7 +604,7 @@ define([
560604
/**
561605
* Set a css attr of the widget view.
562606
*/
563-
if (name == 'color') {
607+
if (name == "color") {
564608
this.$bar.css('background', value);
565609
} else if (name.substring(0, 6) == 'border' || name == 'background') {
566610
this.$progress.css(name, value);
@@ -571,8 +615,13 @@ define([
571615
});
572616

573617
return {
574-
'IntSliderView': IntSliderView,
575-
'IntTextView': IntTextView,
576-
'ProgressView': ProgressView,
618+
IntModel: IntModel,
619+
BoundedIntModel: BoundedIntModel,
620+
IntSliderModel: IntSliderModel,
621+
IntSliderView: IntSliderView,
622+
IntTextModel: IntTextModel,
623+
IntTextView: IntTextView,
624+
ProgressModel: ProgressModel,
625+
ProgressView: ProgressView,
577626
};
578627
});

ipywidgets/widgets/widget_float.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class FloatText(_Float):
6767
color of the value displayed
6868
"""
6969
_view_name = Unicode('FloatTextView', sync=True)
70+
_model_name = Unicode('IntTextModel', sync=True)
7071

7172

7273
@register('Jupyter.BoundedFloatText')
@@ -88,6 +89,7 @@ class BoundedFloatText(_BoundedFloat):
8889
color of the value displayed
8990
"""
9091
_view_name = Unicode('FloatTextView', sync=True)
92+
_model_name = Unicode('IntTextModel', sync=True)
9193

9294

9395
@register('Jupyter.FloatSlider')
@@ -107,7 +109,7 @@ class FloatSlider(_BoundedFloat):
107109
description : str
108110
name of the slider
109111
orientation : {'vertical', 'horizontal}, optional
110-
default is horizontal
112+
default is horizontal
111113
readout : {True, False}, optional
112114
default is True, display the current value of the slider next to it
113115
slider_color : str Unicode color code (eg. '#C13535'), optional
@@ -116,6 +118,7 @@ class FloatSlider(_BoundedFloat):
116118
color of the value displayed (if readout == True)
117119
"""
118120
_view_name = Unicode('FloatSliderView', sync=True)
121+
_model_name = Unicode('IntSliderModel', sync=True)
119122
orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
120123
default_value='horizontal', help="Vertical or horizontal.", sync=True)
121124
_range = Bool(False, help="Display a range selector", sync=True)
@@ -145,20 +148,22 @@ class FloatProgress(_BoundedFloat):
145148
colors are: 'success'-green, 'info'-light blue, 'warning'-orange, 'danger'-red
146149
"""
147150
_view_name = Unicode('ProgressView', sync=True)
148-
orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
151+
_model_name = Unicode('ProgressModel', sync=True)
152+
orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
149153
default_value='horizontal', help="Vertical or horizontal.", sync=True)
150154

151155
bar_style = CaselessStrEnum(
152-
values=['success', 'info', 'warning', 'danger', ''],
156+
values=['success', 'info', 'warning', 'danger', ''],
153157
default_value='', allow_none=True, sync=True, help="""Use a
154158
predefined styling for the progess bar.""")
155159

156160

157161
class _FloatRange(_Float):
158-
value = Tuple(CFloat(), CFloat(), default_value=(0.0, 1.0), help="Tuple of (lower, upper) bounds", sync=True)
162+
value = Tuple(CFloat(), CFloat(), default_value=(0.0, 1.0),
163+
help="Tuple of (lower, upper) bounds", sync=True)
159164
lower = CFloat(0.0, help="Lower bound", sync=False)
160165
upper = CFloat(1.0, help="Upper bound", sync=False)
161-
166+
162167
def __init__(self, *pargs, **kwargs):
163168
value_given = 'value' in kwargs
164169
lower_given = 'lower' in kwargs
@@ -167,17 +172,17 @@ def __init__(self, *pargs, **kwargs):
167172
raise ValueError("Cannot specify both 'value' and 'lower'/'upper' for range widget")
168173
if lower_given != upper_given:
169174
raise ValueError("Must specify both 'lower' and 'upper' for range widget")
170-
175+
171176
DOMWidget.__init__(self, *pargs, **kwargs)
172-
177+
173178
# ensure the traits match, preferring whichever (if any) was given in kwargs
174179
if value_given:
175180
self.lower, self.upper = self.value
176181
else:
177182
self.value = (self.lower, self.upper)
178-
183+
179184
self.on_trait_change(self._validate, ['value', 'upper', 'lower'])
180-
185+
181186
def _validate(self, name, old, new):
182187
if name == 'value':
183188
self.lower, self.upper = min(new), max(new)
@@ -273,7 +278,8 @@ class FloatRangeSlider(_BoundedFloatRange):
273278
color of the value displayed (if readout == True)
274279
"""
275280
_view_name = Unicode('FloatSliderView', sync=True)
276-
orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
281+
_model_name = Unicode('IntSliderModel', sync=True)
282+
orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
277283
default_value='horizontal', help="Vertical or horizontal.", sync=True)
278284
_range = Bool(True, help="Display a range selector", sync=True)
279285
readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)

0 commit comments

Comments
 (0)