Skip to content

Commit

Permalink
Merge pull request #1290 from jasongrout/models
Browse files Browse the repository at this point in the history
Disambiguate models and views
  • Loading branch information
jasongrout authored Apr 18, 2017
2 parents 50da4b8 + da30a54 commit 55ab0a7
Show file tree
Hide file tree
Showing 4 changed files with 313 additions and 202 deletions.
13 changes: 6 additions & 7 deletions ipywidgets/widgets/widget_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def __init__(self, value=None, **kwargs):
class _BoundedFloat(_Float):
max = CFloat(100.0, help="Max value").tag(sync=True)
min = CFloat(0.0, help="Min value").tag(sync=True)
step = CFloat(0.1, help="Minimum step to increment the value (ignored by some views)").tag(sync=True)

@validate('value')
def _validate_value(self, proposal):
Expand Down Expand Up @@ -98,7 +97,7 @@ class BoundedFloatText(_BoundedFloat):
description displayed next to the textbox
"""
_view_name = Unicode('FloatTextView').tag(sync=True)
_model_name = Unicode('FloatTextModel').tag(sync=True)
_model_name = Unicode('BoundedFloatTextModel').tag(sync=True)


@register
Expand Down Expand Up @@ -128,9 +127,9 @@ class FloatSlider(_BoundedFloat):
"""
_view_name = Unicode('FloatSliderView').tag(sync=True)
_model_name = Unicode('FloatSliderModel').tag(sync=True)
step = CFloat(0.1, help="Minimum step to increment the value").tag(sync=True)
orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
default_value='horizontal', help="Vertical or horizontal.").tag(sync=True)
_range = Bool(False, help="Display a range selector").tag(sync=True)
readout = Bool(True, help="Display the current value of the slider next to it.").tag(sync=True)
readout_format = Unicode('.2f', help="Format for the readout").tag(sync=True)
continuous_update = Bool(True, help="Update the value of the widget as the user is holding the slider.").tag(sync=True)
Expand Down Expand Up @@ -161,7 +160,7 @@ class FloatProgress(_BoundedFloat):
colors are: 'success'-green, 'info'-light blue, 'warning'-orange, 'danger'-red
"""
_view_name = Unicode('ProgressView').tag(sync=True)
_model_name = Unicode('ProgressModel').tag(sync=True)
_model_name = Unicode('FloatProgressModel').tag(sync=True)
orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
default_value='horizontal', help="Vertical or horizontal.").tag(sync=True)

Expand Down Expand Up @@ -260,11 +259,11 @@ class FloatRangeSlider(_BoundedFloatRange):
slider value for human consumption, modeled after Python 3's format
specification mini-language (PEP 3101).
"""
_view_name = Unicode('FloatSliderView').tag(sync=True)
_model_name = Unicode('FloatSliderModel').tag(sync=True)
_view_name = Unicode('FloatRangeSliderView').tag(sync=True)
_model_name = Unicode('FloatRangeSliderModel').tag(sync=True)
step = CFloat(0.1, help="Minimum step to increment the value").tag(sync=True)
orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
default_value='horizontal', help="Vertical or horizontal.").tag(sync=True)
_range = Bool(True, help="Display a range selector").tag(sync=True)
readout = Bool(True, help="Display the current value of the slider next to it.").tag(sync=True)
readout_format = Unicode('.2f', help="Format for the readout").tag(sync=True)
continuous_update = Bool(True, help="Update the value of the widget as the user is sliding the slider.").tag(sync=True)
Expand Down
37 changes: 17 additions & 20 deletions ipywidgets/widgets/widget_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ def __init__(self, value=None, **kwargs):
class _BoundedInt(_Int):
"""Base class for widgets that represent an integer bounded from above and below.
"""
step = CInt(1, help="Minimum step to increment the value (ignored by some views)").tag(sync=True)
max = CInt(100, help="Max value").tag(sync=True)
min = CInt(0, help="Min value").tag(sync=True)

Expand Down Expand Up @@ -140,7 +139,7 @@ class BoundedIntText(_BoundedInt):
"""Textbox widget that represents an integer bounded from above and below.
"""
_view_name = Unicode('IntTextView').tag(sync=True)
_model_name = Unicode('IntTextModel').tag(sync=True)
_model_name = Unicode('BoundedIntTextModel').tag(sync=True)


@register
Expand All @@ -157,9 +156,9 @@ class IntSlider(_BoundedInt):
"""
_view_name = Unicode('IntSliderView').tag(sync=True)
_model_name = Unicode('IntSliderModel').tag(sync=True)
step = CInt(1, help="Minimum step to increment the value").tag(sync=True)
orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
default_value='horizontal', help="Vertical or horizontal.").tag(sync=True)
_range = Bool(False, help="Display a range selector").tag(sync=True)
readout = Bool(True, help="Display the current value of the slider next to it.").tag(sync=True)
readout_format = Unicode('d', help="Format for the readout").tag(sync=True)
continuous_update = Bool(True, help="Update the value of the widget as the user is holding the slider.").tag(sync=True)
Expand All @@ -180,7 +179,7 @@ class IntProgress(_BoundedInt):
"""Progress bar that represents an integer bounded from above and below.
"""
_view_name = Unicode('ProgressView').tag(sync=True)
_model_name = Unicode('ProgressModel').tag(sync=True)
_model_name = Unicode('IntProgressModel').tag(sync=True)
orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
default_value='horizontal', help="Vertical or horizontal.").tag(sync=True)

Expand Down Expand Up @@ -218,9 +217,19 @@ def _validate_value(self, proposal):
raise TraitError('setting lower > upper')
return lower, upper

@register
class Play(_BoundedInt):
interval = CInt(100).tag(sync=True)
step = CInt(1, help="Increment step").tag(sync=True)

_view_name = Unicode('PlayView').tag(sync=True)
_model_name = Unicode('PlayModel').tag(sync=True)
_view_module = Unicode('jupyter-js-widgets').tag(sync=True)
_model_module = Unicode('jupyter-js-widgets').tag(sync=True)

_playing = Bool().tag(sync=True)

class _BoundedIntRange(_IntRange):
step = CInt(1, help="Minimum step that the value can take (ignored by some views)").tag(sync=True)
max = CInt(100, help="Max value").tag(sync=True)
min = CInt(0, help="Min value").tag(sync=True)

Expand Down Expand Up @@ -266,23 +275,11 @@ class IntRangeSlider(_BoundedIntRange):
max : int
The highest allowed value for `upper`
"""
_view_name = Unicode('IntSliderView').tag(sync=True)
_model_name = Unicode('IntSliderModel').tag(sync=True)
_view_name = Unicode('IntRangeSliderView').tag(sync=True)
_model_name = Unicode('IntRangeSliderModel').tag(sync=True)
step = CInt(1, help="Minimum step that the value can take").tag(sync=True)
orientation = CaselessStrEnum(values=['horizontal', 'vertical'],
default_value='horizontal', help="Vertical or horizontal.").tag(sync=True)
_range = Bool(True, help="Display a range selector").tag(sync=True)
readout = Bool(True, help="Display the current value of the slider next to it.").tag(sync=True)
continuous_update = Bool(True, help="Update the value of the widget as the user is sliding the slider.").tag(sync=True)
style = InstanceDict(SliderStyle).tag(sync=True, **widget_serialization)


@register
class Play(_BoundedInt):
interval = CInt(100).tag(sync=True)

_view_name = Unicode('PlayView').tag(sync=True)
_model_name = Unicode('PlayModel').tag(sync=True)
_view_module = Unicode('jupyter-js-widgets').tag(sync=True)
_model_module = Unicode('jupyter-js-widgets').tag(sync=True)

_playing = Bool().tag(sync=True)
45 changes: 41 additions & 4 deletions jupyter-js-widgets/src/widget_float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import * as _ from 'underscore';

import {
IntSliderView, IntTextView
IntSliderView, IntRangeSliderView, IntTextView
} from './widget_int';


Expand All @@ -29,13 +29,11 @@ class FloatModel extends CoreLabeledDOMWidgetModel {
}
}


export
class BoundedFloatModel extends FloatModel {
defaults() {
return _.extend(super.defaults(), {
_model_name: "BoundedFloatModel",
step: 1.0,
max: 100.0,
min: 0.0
});
Expand All @@ -48,6 +46,7 @@ class FloatSliderModel extends BoundedFloatModel {
return _.extend(super.defaults(), {
_model_name: "FloatSliderModel",
_view_name: "FloatSliderView",
step: 1.0,
orientation: "horizontal",
_range: false,
readout: true,
Expand All @@ -69,6 +68,9 @@ class FloatSliderModel extends BoundedFloatModel {
readout_formatter: any;
}

export
class FloatRangeSliderModel extends FloatSliderModel {}

export
class FloatSliderView extends IntSliderView {
/**
Expand All @@ -80,10 +82,22 @@ class FloatSliderView extends IntSliderView {
}

_parse_value = parseFloat
}

export
class FloatRangeSliderView extends IntRangeSliderView {
/**
* Validate the value of the slider before sending it to the back-end
* and applying it to the other views on the page.
*/
_validate_slide_value(x) {
return x;
}

_parse_value = parseFloat

// matches: whitespace?, float, whitespace?, (hyphen, colon, or en-dash), whitespace?, float
_range_regex = /^\s*([+-]?(?:\d*\.?\d+|\d+\.)(?:[eE][-:]?\d+)?)\s*[-:]\s*([+-]?(?:\d*\.?\d+|\d+\.)(?:[eE][+-]?\d+)?)/

}

export
Expand All @@ -96,7 +110,30 @@ class FloatTextModel extends FloatModel {
}
}

export
class BoundedFloatTextModel extends BoundedFloatModel {
defaults() {
return _.extend(super.defaults(), {
_model_name: "BoundedFloatTextModel",
_view_name: "FloatTextView"
});
}
}

export
class FloatTextView extends IntTextView {
_parse_value = parseFloat;
}

export
class FloatProgressModel extends BoundedFloatModel {
defaults() {
return _.extend(super.defaults(), {
_model_name: 'FloatProgressModel',
_view_name: 'ProgressView',
orientation: 'horizontal',
bar_style: '',
style: void 0
});
}
}
Loading

0 comments on commit 55ab0a7

Please sign in to comment.