Show More
@@ -1,402 +1,408 b'' | |||||
1 | """Test interact and interactive.""" |
|
1 | """Test interact and interactive.""" | |
2 |
|
2 | |||
3 | #----------------------------------------------------------------------------- |
|
3 | #----------------------------------------------------------------------------- | |
4 | # Copyright (C) 2014 The IPython Development Team |
|
4 | # Copyright (C) 2014 The IPython Development Team | |
5 | # |
|
5 | # | |
6 | # Distributed under the terms of the BSD License. The full license is in |
|
6 | # Distributed under the terms of the BSD License. The full license is in | |
7 | # the file COPYING, distributed as part of this software. |
|
7 | # the file COPYING, distributed as part of this software. | |
8 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
9 |
|
9 | |||
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | # Imports |
|
11 | # Imports | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | from __future__ import print_function |
|
14 | from __future__ import print_function | |
15 |
|
15 | |||
16 | from collections import OrderedDict |
|
16 | from collections import OrderedDict | |
17 |
|
17 | |||
18 | import nose.tools as nt |
|
18 | import nose.tools as nt | |
19 | import IPython.testing.tools as tt |
|
19 | import IPython.testing.tools as tt | |
20 |
|
20 | |||
21 | # from IPython.core.getipython import get_ipython |
|
21 | # from IPython.core.getipython import get_ipython | |
22 | from IPython.html import widgets |
|
22 | from IPython.html import widgets | |
23 | from IPython.html.widgets import interact, interactive, Widget, interaction |
|
23 | from IPython.html.widgets import interact, interactive, Widget, interaction | |
24 | from IPython.utils.py3compat import annotate |
|
24 | from IPython.utils.py3compat import annotate | |
25 | # from IPython.utils.capture import capture_output |
|
25 | # from IPython.utils.capture import capture_output | |
26 |
|
26 | |||
27 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
28 | # Utility stuff |
|
28 | # Utility stuff | |
29 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
30 |
|
30 | |||
31 | class DummyComm(object): |
|
31 | class DummyComm(object): | |
32 | comm_id = 'a-b-c-d' |
|
32 | comm_id = 'a-b-c-d' | |
33 | def send(self, *args, **kwargs): |
|
33 | def send(self, *args, **kwargs): | |
34 | pass |
|
34 | pass | |
35 |
|
35 | |||
36 | def close(self, *args, **kwargs): |
|
36 | def close(self, *args, **kwargs): | |
37 | pass |
|
37 | pass | |
38 |
|
38 | |||
39 | _widget_attrs = {} |
|
39 | _widget_attrs = {} | |
40 | displayed = [] |
|
40 | displayed = [] | |
41 |
|
41 | |||
42 | def setup(): |
|
42 | def setup(): | |
43 | _widget_attrs['comm'] = Widget.comm |
|
43 | _widget_attrs['comm'] = Widget.comm | |
44 | Widget.comm = DummyComm() |
|
44 | Widget.comm = DummyComm() | |
45 | _widget_attrs['_ipython_display_'] = Widget._ipython_display_ |
|
45 | _widget_attrs['_ipython_display_'] = Widget._ipython_display_ | |
46 | def raise_not_implemented(*args, **kwargs): |
|
46 | def raise_not_implemented(*args, **kwargs): | |
47 | raise NotImplementedError() |
|
47 | raise NotImplementedError() | |
48 | Widget._ipython_display_ = raise_not_implemented |
|
48 | Widget._ipython_display_ = raise_not_implemented | |
49 |
|
49 | |||
50 | def teardown(): |
|
50 | def teardown(): | |
51 | for attr, value in _widget_attrs.items(): |
|
51 | for attr, value in _widget_attrs.items(): | |
52 | setattr(Widget, attr, value) |
|
52 | setattr(Widget, attr, value) | |
53 |
|
53 | |||
54 | def f(**kwargs): |
|
54 | def f(**kwargs): | |
55 | pass |
|
55 | pass | |
56 |
|
56 | |||
57 | def clear_display(): |
|
57 | def clear_display(): | |
58 | global displayed |
|
58 | global displayed | |
59 | displayed = [] |
|
59 | displayed = [] | |
60 |
|
60 | |||
61 | def record_display(*args): |
|
61 | def record_display(*args): | |
62 | displayed.extend(args) |
|
62 | displayed.extend(args) | |
63 |
|
63 | |||
64 | #----------------------------------------------------------------------------- |
|
64 | #----------------------------------------------------------------------------- | |
65 | # Actual tests |
|
65 | # Actual tests | |
66 | #----------------------------------------------------------------------------- |
|
66 | #----------------------------------------------------------------------------- | |
67 |
|
67 | |||
68 | def check_widget(w, **d): |
|
68 | def check_widget(w, **d): | |
69 | """Check a single widget against a dict""" |
|
69 | """Check a single widget against a dict""" | |
70 | for attr, expected in d.items(): |
|
70 | for attr, expected in d.items(): | |
71 | if attr == 'cls': |
|
71 | if attr == 'cls': | |
72 | nt.assert_is(w.__class__, expected) |
|
72 | nt.assert_is(w.__class__, expected) | |
73 | else: |
|
73 | else: | |
74 | value = getattr(w, attr) |
|
74 | value = getattr(w, attr) | |
75 | nt.assert_equal(value, expected, |
|
75 | nt.assert_equal(value, expected, | |
76 | "%s.%s = %r != %r" % (w.__class__.__name__, attr, value, expected) |
|
76 | "%s.%s = %r != %r" % (w.__class__.__name__, attr, value, expected) | |
77 | ) |
|
77 | ) | |
78 |
|
78 | |||
79 | def check_widgets(container, **to_check): |
|
79 | def check_widgets(container, **to_check): | |
80 | """Check that widgets are created as expected""" |
|
80 | """Check that widgets are created as expected""" | |
81 | # build a widget dictionary, so it matches |
|
81 | # build a widget dictionary, so it matches | |
82 | widgets = {} |
|
82 | widgets = {} | |
83 | for w in container.children: |
|
83 | for w in container.children: | |
84 | widgets[w.description] = w |
|
84 | widgets[w.description] = w | |
85 |
|
85 | |||
86 | for key, d in to_check.items(): |
|
86 | for key, d in to_check.items(): | |
87 | nt.assert_in(key, widgets) |
|
87 | nt.assert_in(key, widgets) | |
88 | check_widget(widgets[key], **d) |
|
88 | check_widget(widgets[key], **d) | |
89 |
|
89 | |||
90 |
|
90 | |||
91 | def test_single_value_string(): |
|
91 | def test_single_value_string(): | |
92 | a = u'hello' |
|
92 | a = u'hello' | |
93 | c = interactive(f, a=a) |
|
93 | c = interactive(f, a=a) | |
94 | w = c.children[0] |
|
94 | w = c.children[0] | |
95 | check_widget(w, |
|
95 | check_widget(w, | |
96 | cls=widgets.TextWidget, |
|
96 | cls=widgets.TextWidget, | |
97 | description='a', |
|
97 | description='a', | |
98 | value=a, |
|
98 | value=a, | |
99 | ) |
|
99 | ) | |
100 |
|
100 | |||
101 | def test_single_value_bool(): |
|
101 | def test_single_value_bool(): | |
102 | for a in (True, False): |
|
102 | for a in (True, False): | |
103 | c = interactive(f, a=a) |
|
103 | c = interactive(f, a=a) | |
104 | w = c.children[0] |
|
104 | w = c.children[0] | |
105 | check_widget(w, |
|
105 | check_widget(w, | |
106 | cls=widgets.CheckboxWidget, |
|
106 | cls=widgets.CheckboxWidget, | |
107 | description='a', |
|
107 | description='a', | |
108 | value=a, |
|
108 | value=a, | |
109 | ) |
|
109 | ) | |
110 |
|
110 | |||
111 | def test_single_value_dict(): |
|
111 | def test_single_value_dict(): | |
112 | for d in [ |
|
112 | for d in [ | |
113 | dict(a=5), |
|
113 | dict(a=5), | |
114 | dict(a=5, b='b', c=dict), |
|
114 | dict(a=5, b='b', c=dict), | |
115 | ]: |
|
115 | ]: | |
116 | c = interactive(f, d=d) |
|
116 | c = interactive(f, d=d) | |
117 | w = c.children[0] |
|
117 | w = c.children[0] | |
118 | check_widget(w, |
|
118 | check_widget(w, | |
119 | cls=widgets.DropdownWidget, |
|
119 | cls=widgets.DropdownWidget, | |
120 | description='d', |
|
120 | description='d', | |
121 | values=d, |
|
121 | values=d, | |
122 | value=next(iter(d.values())), |
|
122 | value=next(iter(d.values())), | |
123 | ) |
|
123 | ) | |
124 |
|
124 | |||
125 | def test_single_value_float(): |
|
125 | def test_single_value_float(): | |
126 | for a in (2.25, 1.0, -3.5): |
|
126 | for a in (2.25, 1.0, -3.5): | |
127 | c = interactive(f, a=a) |
|
127 | c = interactive(f, a=a) | |
128 | w = c.children[0] |
|
128 | w = c.children[0] | |
129 | check_widget(w, |
|
129 | check_widget(w, | |
130 | cls=widgets.FloatSliderWidget, |
|
130 | cls=widgets.FloatSliderWidget, | |
131 | description='a', |
|
131 | description='a', | |
132 | value=a, |
|
132 | value=a, | |
133 | min= -a if a > 0 else 3*a, |
|
133 | min= -a if a > 0 else 3*a, | |
134 | max= 3*a if a > 0 else -a, |
|
134 | max= 3*a if a > 0 else -a, | |
135 | step=0.1, |
|
135 | step=0.1, | |
|
136 | readout=True, | |||
136 | ) |
|
137 | ) | |
137 |
|
138 | |||
138 | def test_single_value_int(): |
|
139 | def test_single_value_int(): | |
139 | for a in (1, 5, -3): |
|
140 | for a in (1, 5, -3): | |
140 | c = interactive(f, a=a) |
|
141 | c = interactive(f, a=a) | |
141 | nt.assert_equal(len(c.children), 1) |
|
142 | nt.assert_equal(len(c.children), 1) | |
142 | w = c.children[0] |
|
143 | w = c.children[0] | |
143 | check_widget(w, |
|
144 | check_widget(w, | |
144 | cls=widgets.IntSliderWidget, |
|
145 | cls=widgets.IntSliderWidget, | |
145 | description='a', |
|
146 | description='a', | |
146 | value=a, |
|
147 | value=a, | |
147 | min= -a if a > 0 else 3*a, |
|
148 | min= -a if a > 0 else 3*a, | |
148 | max= 3*a if a > 0 else -a, |
|
149 | max= 3*a if a > 0 else -a, | |
149 | step=1, |
|
150 | step=1, | |
|
151 | readout=True, | |||
150 | ) |
|
152 | ) | |
151 |
|
153 | |||
152 | def test_list_tuple_2_int(): |
|
154 | def test_list_tuple_2_int(): | |
153 | with nt.assert_raises(ValueError): |
|
155 | with nt.assert_raises(ValueError): | |
154 | c = interactive(f, tup=(1,1)) |
|
156 | c = interactive(f, tup=(1,1)) | |
155 | with nt.assert_raises(ValueError): |
|
157 | with nt.assert_raises(ValueError): | |
156 | c = interactive(f, tup=(1,-1)) |
|
158 | c = interactive(f, tup=(1,-1)) | |
157 | for min, max in [ (0,1), (1,10), (1,2), (-5,5), (-20,-19) ]: |
|
159 | for min, max in [ (0,1), (1,10), (1,2), (-5,5), (-20,-19) ]: | |
158 | c = interactive(f, tup=(min, max), lis=[min, max]) |
|
160 | c = interactive(f, tup=(min, max), lis=[min, max]) | |
159 | nt.assert_equal(len(c.children), 2) |
|
161 | nt.assert_equal(len(c.children), 2) | |
160 | d = dict( |
|
162 | d = dict( | |
161 | cls=widgets.IntSliderWidget, |
|
163 | cls=widgets.IntSliderWidget, | |
162 | min=min, |
|
164 | min=min, | |
163 | max=max, |
|
165 | max=max, | |
164 | step=1, |
|
166 | step=1, | |
|
167 | readout=True, | |||
165 | ) |
|
168 | ) | |
166 | check_widgets(c, tup=d, lis=d) |
|
169 | check_widgets(c, tup=d, lis=d) | |
167 |
|
170 | |||
168 | def test_list_tuple_3_int(): |
|
171 | def test_list_tuple_3_int(): | |
169 | with nt.assert_raises(ValueError): |
|
172 | with nt.assert_raises(ValueError): | |
170 | c = interactive(f, tup=(1,2,0)) |
|
173 | c = interactive(f, tup=(1,2,0)) | |
171 | with nt.assert_raises(ValueError): |
|
174 | with nt.assert_raises(ValueError): | |
172 | c = interactive(f, tup=(1,2,-1)) |
|
175 | c = interactive(f, tup=(1,2,-1)) | |
173 | for min, max, step in [ (0,2,1), (1,10,2), (1,100,2), (-5,5,4), (-100,-20,4) ]: |
|
176 | for min, max, step in [ (0,2,1), (1,10,2), (1,100,2), (-5,5,4), (-100,-20,4) ]: | |
174 | c = interactive(f, tup=(min, max, step), lis=[min, max, step]) |
|
177 | c = interactive(f, tup=(min, max, step), lis=[min, max, step]) | |
175 | nt.assert_equal(len(c.children), 2) |
|
178 | nt.assert_equal(len(c.children), 2) | |
176 | d = dict( |
|
179 | d = dict( | |
177 | cls=widgets.IntSliderWidget, |
|
180 | cls=widgets.IntSliderWidget, | |
178 | min=min, |
|
181 | min=min, | |
179 | max=max, |
|
182 | max=max, | |
180 | step=step, |
|
183 | step=step, | |
|
184 | readout=True, | |||
181 | ) |
|
185 | ) | |
182 | check_widgets(c, tup=d, lis=d) |
|
186 | check_widgets(c, tup=d, lis=d) | |
183 |
|
187 | |||
184 | def test_list_tuple_2_float(): |
|
188 | def test_list_tuple_2_float(): | |
185 | with nt.assert_raises(ValueError): |
|
189 | with nt.assert_raises(ValueError): | |
186 | c = interactive(f, tup=(1.0,1.0)) |
|
190 | c = interactive(f, tup=(1.0,1.0)) | |
187 | with nt.assert_raises(ValueError): |
|
191 | with nt.assert_raises(ValueError): | |
188 | c = interactive(f, tup=(0.5,-0.5)) |
|
192 | c = interactive(f, tup=(0.5,-0.5)) | |
189 | for min, max in [ (0.5, 1.5), (1.1,10.2), (1,2.2), (-5.,5), (-20,-19.) ]: |
|
193 | for min, max in [ (0.5, 1.5), (1.1,10.2), (1,2.2), (-5.,5), (-20,-19.) ]: | |
190 | c = interactive(f, tup=(min, max), lis=[min, max]) |
|
194 | c = interactive(f, tup=(min, max), lis=[min, max]) | |
191 | nt.assert_equal(len(c.children), 2) |
|
195 | nt.assert_equal(len(c.children), 2) | |
192 | d = dict( |
|
196 | d = dict( | |
193 | cls=widgets.FloatSliderWidget, |
|
197 | cls=widgets.FloatSliderWidget, | |
194 | min=min, |
|
198 | min=min, | |
195 | max=max, |
|
199 | max=max, | |
196 | step=.1, |
|
200 | step=.1, | |
|
201 | readout=True, | |||
197 | ) |
|
202 | ) | |
198 | check_widgets(c, tup=d, lis=d) |
|
203 | check_widgets(c, tup=d, lis=d) | |
199 |
|
204 | |||
200 | def test_list_tuple_3_float(): |
|
205 | def test_list_tuple_3_float(): | |
201 | with nt.assert_raises(ValueError): |
|
206 | with nt.assert_raises(ValueError): | |
202 | c = interactive(f, tup=(1,2,0.0)) |
|
207 | c = interactive(f, tup=(1,2,0.0)) | |
203 | with nt.assert_raises(ValueError): |
|
208 | with nt.assert_raises(ValueError): | |
204 | c = interactive(f, tup=(-1,-2,1.)) |
|
209 | c = interactive(f, tup=(-1,-2,1.)) | |
205 | with nt.assert_raises(ValueError): |
|
210 | with nt.assert_raises(ValueError): | |
206 | c = interactive(f, tup=(1,2.,-1.)) |
|
211 | c = interactive(f, tup=(1,2.,-1.)) | |
207 | for min, max, step in [ (0.,2,1), (1,10.,2), (1,100,2.), (-5.,5.,4), (-100,-20.,4.) ]: |
|
212 | for min, max, step in [ (0.,2,1), (1,10.,2), (1,100,2.), (-5.,5.,4), (-100,-20.,4.) ]: | |
208 | c = interactive(f, tup=(min, max, step), lis=[min, max, step]) |
|
213 | c = interactive(f, tup=(min, max, step), lis=[min, max, step]) | |
209 | nt.assert_equal(len(c.children), 2) |
|
214 | nt.assert_equal(len(c.children), 2) | |
210 | d = dict( |
|
215 | d = dict( | |
211 | cls=widgets.FloatSliderWidget, |
|
216 | cls=widgets.FloatSliderWidget, | |
212 | min=min, |
|
217 | min=min, | |
213 | max=max, |
|
218 | max=max, | |
214 | step=step, |
|
219 | step=step, | |
|
220 | readout=True, | |||
215 | ) |
|
221 | ) | |
216 | check_widgets(c, tup=d, lis=d) |
|
222 | check_widgets(c, tup=d, lis=d) | |
217 |
|
223 | |||
218 | def test_list_tuple_str(): |
|
224 | def test_list_tuple_str(): | |
219 | values = ['hello', 'there', 'guy'] |
|
225 | values = ['hello', 'there', 'guy'] | |
220 | first = values[0] |
|
226 | first = values[0] | |
221 | dvalues = OrderedDict((v,v) for v in values) |
|
227 | dvalues = OrderedDict((v,v) for v in values) | |
222 | c = interactive(f, tup=tuple(values), lis=list(values)) |
|
228 | c = interactive(f, tup=tuple(values), lis=list(values)) | |
223 | nt.assert_equal(len(c.children), 2) |
|
229 | nt.assert_equal(len(c.children), 2) | |
224 | d = dict( |
|
230 | d = dict( | |
225 | cls=widgets.DropdownWidget, |
|
231 | cls=widgets.DropdownWidget, | |
226 | value=first, |
|
232 | value=first, | |
227 | values=dvalues |
|
233 | values=dvalues | |
228 | ) |
|
234 | ) | |
229 | check_widgets(c, tup=d, lis=d) |
|
235 | check_widgets(c, tup=d, lis=d) | |
230 |
|
236 | |||
231 | def test_list_tuple_invalid(): |
|
237 | def test_list_tuple_invalid(): | |
232 | for bad in [ |
|
238 | for bad in [ | |
233 | (), |
|
239 | (), | |
234 | (5, 'hi'), |
|
240 | (5, 'hi'), | |
235 | ('hi', 5), |
|
241 | ('hi', 5), | |
236 | ({},), |
|
242 | ({},), | |
237 | (None,), |
|
243 | (None,), | |
238 | ]: |
|
244 | ]: | |
239 | with nt.assert_raises(ValueError): |
|
245 | with nt.assert_raises(ValueError): | |
240 | print(bad) # because there is no custom message in assert_raises |
|
246 | print(bad) # because there is no custom message in assert_raises | |
241 | c = interactive(f, tup=bad) |
|
247 | c = interactive(f, tup=bad) | |
242 |
|
248 | |||
243 | def test_defaults(): |
|
249 | def test_defaults(): | |
244 | @annotate(n=10) |
|
250 | @annotate(n=10) | |
245 | def f(n, f=4.5): |
|
251 | def f(n, f=4.5): | |
246 | pass |
|
252 | pass | |
247 |
|
253 | |||
248 | c = interactive(f) |
|
254 | c = interactive(f) | |
249 | check_widgets(c, |
|
255 | check_widgets(c, | |
250 | n=dict( |
|
256 | n=dict( | |
251 | cls=widgets.IntSliderWidget, |
|
257 | cls=widgets.IntSliderWidget, | |
252 | value=10, |
|
258 | value=10, | |
253 | ), |
|
259 | ), | |
254 | f=dict( |
|
260 | f=dict( | |
255 | cls=widgets.FloatSliderWidget, |
|
261 | cls=widgets.FloatSliderWidget, | |
256 | value=4.5, |
|
262 | value=4.5, | |
257 | ), |
|
263 | ), | |
258 | ) |
|
264 | ) | |
259 |
|
265 | |||
260 | def test_annotations(): |
|
266 | def test_annotations(): | |
261 | @annotate(n=10, f=widgets.FloatTextWidget()) |
|
267 | @annotate(n=10, f=widgets.FloatTextWidget()) | |
262 | def f(n, f): |
|
268 | def f(n, f): | |
263 | pass |
|
269 | pass | |
264 |
|
270 | |||
265 | c = interactive(f) |
|
271 | c = interactive(f) | |
266 | check_widgets(c, |
|
272 | check_widgets(c, | |
267 | n=dict( |
|
273 | n=dict( | |
268 | cls=widgets.IntSliderWidget, |
|
274 | cls=widgets.IntSliderWidget, | |
269 | value=10, |
|
275 | value=10, | |
270 | ), |
|
276 | ), | |
271 | f=dict( |
|
277 | f=dict( | |
272 | cls=widgets.FloatTextWidget, |
|
278 | cls=widgets.FloatTextWidget, | |
273 | ), |
|
279 | ), | |
274 | ) |
|
280 | ) | |
275 |
|
281 | |||
276 | def test_priority(): |
|
282 | def test_priority(): | |
277 | @annotate(annotate='annotate', kwarg='annotate') |
|
283 | @annotate(annotate='annotate', kwarg='annotate') | |
278 | def f(kwarg='default', annotate='default', default='default'): |
|
284 | def f(kwarg='default', annotate='default', default='default'): | |
279 | pass |
|
285 | pass | |
280 |
|
286 | |||
281 | c = interactive(f, kwarg='kwarg') |
|
287 | c = interactive(f, kwarg='kwarg') | |
282 | check_widgets(c, |
|
288 | check_widgets(c, | |
283 | kwarg=dict( |
|
289 | kwarg=dict( | |
284 | cls=widgets.TextWidget, |
|
290 | cls=widgets.TextWidget, | |
285 | value='kwarg', |
|
291 | value='kwarg', | |
286 | ), |
|
292 | ), | |
287 | annotate=dict( |
|
293 | annotate=dict( | |
288 | cls=widgets.TextWidget, |
|
294 | cls=widgets.TextWidget, | |
289 | value='annotate', |
|
295 | value='annotate', | |
290 | ), |
|
296 | ), | |
291 | ) |
|
297 | ) | |
292 |
|
298 | |||
293 | @nt.with_setup(clear_display) |
|
299 | @nt.with_setup(clear_display) | |
294 | def test_decorator_kwarg(): |
|
300 | def test_decorator_kwarg(): | |
295 | with tt.monkeypatch(interaction, 'display', record_display): |
|
301 | with tt.monkeypatch(interaction, 'display', record_display): | |
296 | @interact(a=5) |
|
302 | @interact(a=5) | |
297 | def foo(a): |
|
303 | def foo(a): | |
298 | pass |
|
304 | pass | |
299 | nt.assert_equal(len(displayed), 1) |
|
305 | nt.assert_equal(len(displayed), 1) | |
300 | w = displayed[0].children[0] |
|
306 | w = displayed[0].children[0] | |
301 | check_widget(w, |
|
307 | check_widget(w, | |
302 | cls=widgets.IntSliderWidget, |
|
308 | cls=widgets.IntSliderWidget, | |
303 | value=5, |
|
309 | value=5, | |
304 | ) |
|
310 | ) | |
305 |
|
311 | |||
306 | @nt.with_setup(clear_display) |
|
312 | @nt.with_setup(clear_display) | |
307 | def test_decorator_no_call(): |
|
313 | def test_decorator_no_call(): | |
308 | with tt.monkeypatch(interaction, 'display', record_display): |
|
314 | with tt.monkeypatch(interaction, 'display', record_display): | |
309 | @interact |
|
315 | @interact | |
310 | def foo(a='default'): |
|
316 | def foo(a='default'): | |
311 | pass |
|
317 | pass | |
312 | nt.assert_equal(len(displayed), 1) |
|
318 | nt.assert_equal(len(displayed), 1) | |
313 | w = displayed[0].children[0] |
|
319 | w = displayed[0].children[0] | |
314 | check_widget(w, |
|
320 | check_widget(w, | |
315 | cls=widgets.TextWidget, |
|
321 | cls=widgets.TextWidget, | |
316 | value='default', |
|
322 | value='default', | |
317 | ) |
|
323 | ) | |
318 |
|
324 | |||
319 | @nt.with_setup(clear_display) |
|
325 | @nt.with_setup(clear_display) | |
320 | def test_call_interact(): |
|
326 | def test_call_interact(): | |
321 | def foo(a='default'): |
|
327 | def foo(a='default'): | |
322 | pass |
|
328 | pass | |
323 | with tt.monkeypatch(interaction, 'display', record_display): |
|
329 | with tt.monkeypatch(interaction, 'display', record_display): | |
324 | ifoo = interact(foo) |
|
330 | ifoo = interact(foo) | |
325 | nt.assert_equal(len(displayed), 1) |
|
331 | nt.assert_equal(len(displayed), 1) | |
326 | w = displayed[0].children[0] |
|
332 | w = displayed[0].children[0] | |
327 | check_widget(w, |
|
333 | check_widget(w, | |
328 | cls=widgets.TextWidget, |
|
334 | cls=widgets.TextWidget, | |
329 | value='default', |
|
335 | value='default', | |
330 | ) |
|
336 | ) | |
331 |
|
337 | |||
332 | @nt.with_setup(clear_display) |
|
338 | @nt.with_setup(clear_display) | |
333 | def test_call_interact_kwargs(): |
|
339 | def test_call_interact_kwargs(): | |
334 | def foo(a='default'): |
|
340 | def foo(a='default'): | |
335 | pass |
|
341 | pass | |
336 | with tt.monkeypatch(interaction, 'display', record_display): |
|
342 | with tt.monkeypatch(interaction, 'display', record_display): | |
337 | ifoo = interact(foo, a=10) |
|
343 | ifoo = interact(foo, a=10) | |
338 | nt.assert_equal(len(displayed), 1) |
|
344 | nt.assert_equal(len(displayed), 1) | |
339 | w = displayed[0].children[0] |
|
345 | w = displayed[0].children[0] | |
340 | check_widget(w, |
|
346 | check_widget(w, | |
341 | cls=widgets.IntSliderWidget, |
|
347 | cls=widgets.IntSliderWidget, | |
342 | value=10, |
|
348 | value=10, | |
343 | ) |
|
349 | ) | |
344 |
|
350 | |||
345 | @nt.with_setup(clear_display) |
|
351 | @nt.with_setup(clear_display) | |
346 | def test_call_decorated_on_trait_change(): |
|
352 | def test_call_decorated_on_trait_change(): | |
347 | """test calling @interact decorated functions""" |
|
353 | """test calling @interact decorated functions""" | |
348 | d = {} |
|
354 | d = {} | |
349 | with tt.monkeypatch(interaction, 'display', record_display): |
|
355 | with tt.monkeypatch(interaction, 'display', record_display): | |
350 | @interact |
|
356 | @interact | |
351 | def foo(a='default'): |
|
357 | def foo(a='default'): | |
352 | d['a'] = a |
|
358 | d['a'] = a | |
353 | return a |
|
359 | return a | |
354 | nt.assert_equal(len(displayed), 1) |
|
360 | nt.assert_equal(len(displayed), 1) | |
355 | w = displayed[0].children[0] |
|
361 | w = displayed[0].children[0] | |
356 | check_widget(w, |
|
362 | check_widget(w, | |
357 | cls=widgets.TextWidget, |
|
363 | cls=widgets.TextWidget, | |
358 | value='default', |
|
364 | value='default', | |
359 | ) |
|
365 | ) | |
360 | # test calling the function directly |
|
366 | # test calling the function directly | |
361 | a = foo('hello') |
|
367 | a = foo('hello') | |
362 | nt.assert_equal(a, 'hello') |
|
368 | nt.assert_equal(a, 'hello') | |
363 | nt.assert_equal(d['a'], 'hello') |
|
369 | nt.assert_equal(d['a'], 'hello') | |
364 |
|
370 | |||
365 | # test that setting trait values calls the function |
|
371 | # test that setting trait values calls the function | |
366 | w.value = 'called' |
|
372 | w.value = 'called' | |
367 | nt.assert_equal(d['a'], 'called') |
|
373 | nt.assert_equal(d['a'], 'called') | |
368 |
|
374 | |||
369 | @nt.with_setup(clear_display) |
|
375 | @nt.with_setup(clear_display) | |
370 | def test_call_decorated_kwargs_on_trait_change(): |
|
376 | def test_call_decorated_kwargs_on_trait_change(): | |
371 | """test calling @interact(foo=bar) decorated functions""" |
|
377 | """test calling @interact(foo=bar) decorated functions""" | |
372 | d = {} |
|
378 | d = {} | |
373 | with tt.monkeypatch(interaction, 'display', record_display): |
|
379 | with tt.monkeypatch(interaction, 'display', record_display): | |
374 | @interact(a='kwarg') |
|
380 | @interact(a='kwarg') | |
375 | def foo(a='default'): |
|
381 | def foo(a='default'): | |
376 | d['a'] = a |
|
382 | d['a'] = a | |
377 | return a |
|
383 | return a | |
378 | nt.assert_equal(len(displayed), 1) |
|
384 | nt.assert_equal(len(displayed), 1) | |
379 | w = displayed[0].children[0] |
|
385 | w = displayed[0].children[0] | |
380 | check_widget(w, |
|
386 | check_widget(w, | |
381 | cls=widgets.TextWidget, |
|
387 | cls=widgets.TextWidget, | |
382 | value='kwarg', |
|
388 | value='kwarg', | |
383 | ) |
|
389 | ) | |
384 | # test calling the function directly |
|
390 | # test calling the function directly | |
385 | a = foo('hello') |
|
391 | a = foo('hello') | |
386 | nt.assert_equal(a, 'hello') |
|
392 | nt.assert_equal(a, 'hello') | |
387 | nt.assert_equal(d['a'], 'hello') |
|
393 | nt.assert_equal(d['a'], 'hello') | |
388 |
|
394 | |||
389 | # test that setting trait values calls the function |
|
395 | # test that setting trait values calls the function | |
390 | w.value = 'called' |
|
396 | w.value = 'called' | |
391 | nt.assert_equal(d['a'], 'called') |
|
397 | nt.assert_equal(d['a'], 'called') | |
392 |
|
398 | |||
393 | def test_fixed(): |
|
399 | def test_fixed(): | |
394 | c = interactive(f, a=widgets.fixed(5), b='text') |
|
400 | c = interactive(f, a=widgets.fixed(5), b='text') | |
395 | nt.assert_equal(len(c.children), 1) |
|
401 | nt.assert_equal(len(c.children), 1) | |
396 | w = c.children[0] |
|
402 | w = c.children[0] | |
397 | check_widget(w, |
|
403 | check_widget(w, | |
398 | cls=widgets.TextWidget, |
|
404 | cls=widgets.TextWidget, | |
399 | value='text', |
|
405 | value='text', | |
400 | description='b', |
|
406 | description='b', | |
401 | ) |
|
407 | ) | |
402 |
|
408 |
@@ -1,60 +1,60 b'' | |||||
1 | """FloatWidget class. |
|
1 | """FloatWidget class. | |
2 |
|
2 | |||
3 | Represents an unbounded float using a widget. |
|
3 | Represents an unbounded float using a widget. | |
4 | """ |
|
4 | """ | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
7 | # |
|
7 | # | |
8 | # Distributed under the terms of the Modified BSD License. |
|
8 | # Distributed under the terms of the Modified BSD License. | |
9 | # |
|
9 | # | |
10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | from .widget import DOMWidget |
|
16 | from .widget import DOMWidget | |
17 | from IPython.utils.traitlets import Unicode, CFloat, Bool, List, Enum |
|
17 | from IPython.utils.traitlets import Unicode, CFloat, Bool, List, Enum | |
18 |
|
18 | |||
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 | # Classes |
|
20 | # Classes | |
21 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
22 | class _FloatWidget(DOMWidget): |
|
22 | class _FloatWidget(DOMWidget): | |
23 | value = CFloat(0.0, help="Float value", sync=True) |
|
23 | value = CFloat(0.0, help="Float value", sync=True) | |
24 | disabled = Bool(False, help="Enable or disable user changes", sync=True) |
|
24 | disabled = Bool(False, help="Enable or disable user changes", sync=True) | |
25 | description = Unicode(help="Description of the value this widget represents", sync=True) |
|
25 | description = Unicode(help="Description of the value this widget represents", sync=True) | |
26 |
|
26 | |||
27 |
|
27 | |||
28 | class _BoundedFloatWidget(_FloatWidget): |
|
28 | class _BoundedFloatWidget(_FloatWidget): | |
29 | max = CFloat(100.0, help="Max value", sync=True) |
|
29 | max = CFloat(100.0, help="Max value", sync=True) | |
30 | min = CFloat(0.0, help="Min value", sync=True) |
|
30 | min = CFloat(0.0, help="Min value", sync=True) | |
31 | step = CFloat(0.1, help="Minimum step that the value can take (ignored by some views)", sync=True) |
|
31 | step = CFloat(0.1, help="Minimum step that the value can take (ignored by some views)", sync=True) | |
32 |
|
32 | |||
33 | def __init__(self, *pargs, **kwargs): |
|
33 | def __init__(self, *pargs, **kwargs): | |
34 | """Constructor""" |
|
34 | """Constructor""" | |
35 | DOMWidget.__init__(self, *pargs, **kwargs) |
|
35 | DOMWidget.__init__(self, *pargs, **kwargs) | |
36 | self.on_trait_change(self._validate, ['value', 'min', 'max']) |
|
36 | self.on_trait_change(self._validate, ['value', 'min', 'max']) | |
37 |
|
37 | |||
38 | def _validate(self, name, old, new): |
|
38 | def _validate(self, name, old, new): | |
39 | """Validate value, max, min.""" |
|
39 | """Validate value, max, min.""" | |
40 | if self.min > new or new > self.max: |
|
40 | if self.min > new or new > self.max: | |
41 | self.value = min(max(new, self.min), self.max) |
|
41 | self.value = min(max(new, self.min), self.max) | |
42 |
|
42 | |||
43 |
|
43 | |||
44 | class FloatTextWidget(_FloatWidget): |
|
44 | class FloatTextWidget(_FloatWidget): | |
45 | _view_name = Unicode('FloatTextView', sync=True) |
|
45 | _view_name = Unicode('FloatTextView', sync=True) | |
46 |
|
46 | |||
47 |
|
47 | |||
48 | class BoundedFloatTextWidget(_BoundedFloatWidget): |
|
48 | class BoundedFloatTextWidget(_BoundedFloatWidget): | |
49 | _view_name = Unicode('FloatTextView', sync=True) |
|
49 | _view_name = Unicode('FloatTextView', sync=True) | |
50 |
|
50 | |||
51 |
|
51 | |||
52 | class FloatSliderWidget(_BoundedFloatWidget): |
|
52 | class FloatSliderWidget(_BoundedFloatWidget): | |
53 | _view_name = Unicode('FloatSliderView', sync=True) |
|
53 | _view_name = Unicode('FloatSliderView', sync=True) | |
54 | orientation = Enum([u'horizontal', u'vertical'], u'horizontal', |
|
54 | orientation = Enum([u'horizontal', u'vertical'], u'horizontal', | |
55 | help="Vertical or horizontal.", sync=True) |
|
55 | help="Vertical or horizontal.", sync=True) | |
56 |
readout = Bool( |
|
56 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) | |
57 |
|
57 | |||
58 |
|
58 | |||
59 | class FloatProgressWidget(_BoundedFloatWidget): |
|
59 | class FloatProgressWidget(_BoundedFloatWidget): | |
60 | _view_name = Unicode('ProgressView', sync=True) |
|
60 | _view_name = Unicode('ProgressView', sync=True) |
@@ -1,60 +1,60 b'' | |||||
1 | """IntWidget class. |
|
1 | """IntWidget class. | |
2 |
|
2 | |||
3 | Represents an unbounded int using a widget. |
|
3 | Represents an unbounded int using a widget. | |
4 | """ |
|
4 | """ | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
7 | # |
|
7 | # | |
8 | # Distributed under the terms of the Modified BSD License. |
|
8 | # Distributed under the terms of the Modified BSD License. | |
9 | # |
|
9 | # | |
10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | from .widget import DOMWidget |
|
16 | from .widget import DOMWidget | |
17 | from IPython.utils.traitlets import Unicode, CInt, Bool, List, Enum |
|
17 | from IPython.utils.traitlets import Unicode, CInt, Bool, List, Enum | |
18 |
|
18 | |||
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 | # Classes |
|
20 | # Classes | |
21 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
22 | class _IntWidget(DOMWidget): |
|
22 | class _IntWidget(DOMWidget): | |
23 | value = CInt(0, help="Int value", sync=True) |
|
23 | value = CInt(0, help="Int value", sync=True) | |
24 | disabled = Bool(False, help="Enable or disable user changes", sync=True) |
|
24 | disabled = Bool(False, help="Enable or disable user changes", sync=True) | |
25 | description = Unicode(help="Description of the value this widget represents", sync=True) |
|
25 | description = Unicode(help="Description of the value this widget represents", sync=True) | |
26 |
|
26 | |||
27 |
|
27 | |||
28 | class _BoundedIntWidget(_IntWidget): |
|
28 | class _BoundedIntWidget(_IntWidget): | |
29 | step = CInt(1, help="Minimum step that the value can take (ignored by some views)", sync=True) |
|
29 | step = CInt(1, help="Minimum step that the value can take (ignored by some views)", sync=True) | |
30 | max = CInt(100, help="Max value", sync=True) |
|
30 | max = CInt(100, help="Max value", sync=True) | |
31 | min = CInt(0, help="Min value", sync=True) |
|
31 | min = CInt(0, help="Min value", sync=True) | |
32 |
|
32 | |||
33 | def __init__(self, *pargs, **kwargs): |
|
33 | def __init__(self, *pargs, **kwargs): | |
34 | """Constructor""" |
|
34 | """Constructor""" | |
35 | DOMWidget.__init__(self, *pargs, **kwargs) |
|
35 | DOMWidget.__init__(self, *pargs, **kwargs) | |
36 | self.on_trait_change(self._validate, ['value', 'min', 'max']) |
|
36 | self.on_trait_change(self._validate, ['value', 'min', 'max']) | |
37 |
|
37 | |||
38 | def _validate(self, name, old, new): |
|
38 | def _validate(self, name, old, new): | |
39 | """Validate value, max, min.""" |
|
39 | """Validate value, max, min.""" | |
40 | if self.min > new or new > self.max: |
|
40 | if self.min > new or new > self.max: | |
41 | self.value = min(max(new, self.min), self.max) |
|
41 | self.value = min(max(new, self.min), self.max) | |
42 |
|
42 | |||
43 |
|
43 | |||
44 | class IntTextWidget(_IntWidget): |
|
44 | class IntTextWidget(_IntWidget): | |
45 | _view_name = Unicode('IntTextView', sync=True) |
|
45 | _view_name = Unicode('IntTextView', sync=True) | |
46 |
|
46 | |||
47 |
|
47 | |||
48 | class BoundedIntTextWidget(_BoundedIntWidget): |
|
48 | class BoundedIntTextWidget(_BoundedIntWidget): | |
49 | _view_name = Unicode('IntTextView', sync=True) |
|
49 | _view_name = Unicode('IntTextView', sync=True) | |
50 |
|
50 | |||
51 |
|
51 | |||
52 | class IntSliderWidget(_BoundedIntWidget): |
|
52 | class IntSliderWidget(_BoundedIntWidget): | |
53 | _view_name = Unicode('IntSliderView', sync=True) |
|
53 | _view_name = Unicode('IntSliderView', sync=True) | |
54 | orientation = Enum([u'horizontal', u'vertical'], u'horizontal', |
|
54 | orientation = Enum([u'horizontal', u'vertical'], u'horizontal', | |
55 | help="Vertical or horizontal.", sync=True) |
|
55 | help="Vertical or horizontal.", sync=True) | |
56 |
readout = Bool( |
|
56 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) | |
57 |
|
57 | |||
58 |
|
58 | |||
59 | class IntProgressWidget(_BoundedIntWidget): |
|
59 | class IntProgressWidget(_BoundedIntWidget): | |
60 | _view_name = Unicode('ProgressView', sync=True) |
|
60 | _view_name = Unicode('ProgressView', sync=True) |
General Comments 0
You need to be logged in to leave comments.
Login now