Show More
@@ -14,7 +14,7 b' from inspect import getcallargs' | |||||
14 | from IPython.core.getipython import get_ipython |
|
14 | from IPython.core.getipython import get_ipython | |
15 | from IPython.html.widgets import (Widget, Text, |
|
15 | from IPython.html.widgets import (Widget, Text, | |
16 | FloatSlider, IntSlider, Checkbox, Dropdown, |
|
16 | FloatSlider, IntSlider, Checkbox, Dropdown, | |
17 |
Box, Button, DOMWidget |
|
17 | Box, Button, DOMWidget) | |
18 | from IPython.display import display, clear_output |
|
18 | from IPython.display import display, clear_output | |
19 | from IPython.utils.py3compat import string_types, unicode_type |
|
19 | from IPython.utils.py3compat import string_types, unicode_type | |
20 | from IPython.utils.traitlets import HasTraits, Any, Unicode |
|
20 | from IPython.utils.traitlets import HasTraits, Any, Unicode | |
@@ -204,15 +204,10 b' def interactive(__interact_f, **kwargs):' | |||||
204 | if manual: |
|
204 | if manual: | |
205 | manual_button = Button(description="Run %s" % f.__name__) |
|
205 | manual_button = Button(description="Run %s" % f.__name__) | |
206 | c.append(manual_button) |
|
206 | c.append(manual_button) | |
207 |
|
||||
208 | # Use an output widget to capture the output of interact. |
|
|||
209 | output = Output() |
|
|||
210 | c.append(output) |
|
|||
211 | container.children = c |
|
207 | container.children = c | |
212 |
|
208 | |||
213 | # Build the callback |
|
209 | # Build the callback | |
214 | def call_f(name=None, old=None, new=None): |
|
210 | def call_f(name=None, old=None, new=None): | |
215 | with output: |
|
|||
216 |
|
|
211 | container.kwargs = {} | |
217 |
|
|
212 | for widget in kwargs_widgets: | |
218 |
|
|
213 | value = widget.value |
@@ -80,7 +80,6 b' def check_widgets(container, **to_check):' | |||||
80 | # build a widget dictionary, so it matches |
|
80 | # build a widget dictionary, so it matches | |
81 | widgets = {} |
|
81 | widgets = {} | |
82 | for w in container.children: |
|
82 | for w in container.children: | |
83 | if hasattr(w, 'description'): |
|
|||
84 |
|
|
83 | widgets[w.description] = w | |
85 |
|
84 | |||
86 | for key, d in to_check.items(): |
|
85 | for key, d in to_check.items(): | |
@@ -139,7 +138,7 b' def test_single_value_float():' | |||||
139 | def test_single_value_int(): |
|
138 | def test_single_value_int(): | |
140 | for a in (1, 5, -3): |
|
139 | for a in (1, 5, -3): | |
141 | c = interactive(f, a=a) |
|
140 | c = interactive(f, a=a) | |
142 |
nt.assert_equal(len(c.children), |
|
141 | nt.assert_equal(len(c.children), 1) | |
143 | w = c.children[0] |
|
142 | w = c.children[0] | |
144 | check_widget(w, |
|
143 | check_widget(w, | |
145 | cls=widgets.IntSlider, |
|
144 | cls=widgets.IntSlider, | |
@@ -158,7 +157,7 b' def test_list_tuple_2_int():' | |||||
158 | c = interactive(f, tup=(1,-1)) |
|
157 | c = interactive(f, tup=(1,-1)) | |
159 | for min, max in [ (0,1), (1,10), (1,2), (-5,5), (-20,-19) ]: |
|
158 | for min, max in [ (0,1), (1,10), (1,2), (-5,5), (-20,-19) ]: | |
160 | c = interactive(f, tup=(min, max), lis=[min, max]) |
|
159 | c = interactive(f, tup=(min, max), lis=[min, max]) | |
161 |
nt.assert_equal(len(c.children), |
|
160 | nt.assert_equal(len(c.children), 2) | |
162 | d = dict( |
|
161 | d = dict( | |
163 | cls=widgets.IntSlider, |
|
162 | cls=widgets.IntSlider, | |
164 | min=min, |
|
163 | min=min, | |
@@ -175,7 +174,7 b' def test_list_tuple_3_int():' | |||||
175 | c = interactive(f, tup=(1,2,-1)) |
|
174 | c = interactive(f, tup=(1,2,-1)) | |
176 | for min, max, step in [ (0,2,1), (1,10,2), (1,100,2), (-5,5,4), (-100,-20,4) ]: |
|
175 | for min, max, step in [ (0,2,1), (1,10,2), (1,100,2), (-5,5,4), (-100,-20,4) ]: | |
177 | c = interactive(f, tup=(min, max, step), lis=[min, max, step]) |
|
176 | c = interactive(f, tup=(min, max, step), lis=[min, max, step]) | |
178 |
nt.assert_equal(len(c.children), |
|
177 | nt.assert_equal(len(c.children), 2) | |
179 | d = dict( |
|
178 | d = dict( | |
180 | cls=widgets.IntSlider, |
|
179 | cls=widgets.IntSlider, | |
181 | min=min, |
|
180 | min=min, | |
@@ -192,7 +191,7 b' def test_list_tuple_2_float():' | |||||
192 | c = interactive(f, tup=(0.5,-0.5)) |
|
191 | c = interactive(f, tup=(0.5,-0.5)) | |
193 | for min, max in [ (0.5, 1.5), (1.1,10.2), (1,2.2), (-5.,5), (-20,-19.) ]: |
|
192 | for min, max in [ (0.5, 1.5), (1.1,10.2), (1,2.2), (-5.,5), (-20,-19.) ]: | |
194 | c = interactive(f, tup=(min, max), lis=[min, max]) |
|
193 | c = interactive(f, tup=(min, max), lis=[min, max]) | |
195 |
nt.assert_equal(len(c.children), |
|
194 | nt.assert_equal(len(c.children), 2) | |
196 | d = dict( |
|
195 | d = dict( | |
197 | cls=widgets.FloatSlider, |
|
196 | cls=widgets.FloatSlider, | |
198 | min=min, |
|
197 | min=min, | |
@@ -211,7 +210,7 b' def test_list_tuple_3_float():' | |||||
211 | c = interactive(f, tup=(1,2.,-1.)) |
|
210 | c = interactive(f, tup=(1,2.,-1.)) | |
212 | for min, max, step in [ (0.,2,1), (1,10.,2), (1,100,2.), (-5.,5.,4), (-100,-20.,4.) ]: |
|
211 | for min, max, step in [ (0.,2,1), (1,10.,2), (1,100,2.), (-5.,5.,4), (-100,-20.,4.) ]: | |
213 | c = interactive(f, tup=(min, max, step), lis=[min, max, step]) |
|
212 | c = interactive(f, tup=(min, max, step), lis=[min, max, step]) | |
214 |
nt.assert_equal(len(c.children), |
|
213 | nt.assert_equal(len(c.children), 2) | |
215 | d = dict( |
|
214 | d = dict( | |
216 | cls=widgets.FloatSlider, |
|
215 | cls=widgets.FloatSlider, | |
217 | min=min, |
|
216 | min=min, | |
@@ -225,7 +224,7 b' def test_list_tuple_str():' | |||||
225 | values = ['hello', 'there', 'guy'] |
|
224 | values = ['hello', 'there', 'guy'] | |
226 | first = values[0] |
|
225 | first = values[0] | |
227 | c = interactive(f, tup=tuple(values), lis=list(values)) |
|
226 | c = interactive(f, tup=tuple(values), lis=list(values)) | |
228 |
nt.assert_equal(len(c.children), |
|
227 | nt.assert_equal(len(c.children), 2) | |
229 | d = dict( |
|
228 | d = dict( | |
230 | cls=widgets.Dropdown, |
|
229 | cls=widgets.Dropdown, | |
231 | value=first, |
|
230 | value=first, | |
@@ -472,7 +471,7 b' def test_call_decorated_kwargs_on_trait_change():' | |||||
472 |
|
471 | |||
473 | def test_fixed(): |
|
472 | def test_fixed(): | |
474 | c = interactive(f, a=widgets.fixed(5), b='text') |
|
473 | c = interactive(f, a=widgets.fixed(5), b='text') | |
475 |
nt.assert_equal(len(c.children), |
|
474 | nt.assert_equal(len(c.children), 1) | |
476 | w = c.children[0] |
|
475 | w = c.children[0] | |
477 | check_widget(w, |
|
476 | check_widget(w, | |
478 | cls=widgets.Text, |
|
477 | cls=widgets.Text, |
@@ -34,24 +34,15 b' class Output(DOMWidget):' | |||||
34 | print('prints to output widget')""" |
|
34 | print('prints to output widget')""" | |
35 | _view_name = Unicode('OutputView', sync=True) |
|
35 | _view_name = Unicode('OutputView', sync=True) | |
36 |
|
36 | |||
37 | def __init__(self, *args, **kwargs): |
|
|||
38 | super(Output, self).__init__(*args, **kwargs) |
|
|||
39 | from IPython import get_ipython |
|
|||
40 | ip = get_ipython() |
|
|||
41 | if ip is not None and hasattr(ip, 'kernel'): |
|
|||
42 | self._kernel = ip.kernel |
|
|||
43 | else: |
|
|||
44 | self._kernel = None |
|
|||
45 |
|
||||
46 | def clear_output(self, *pargs, **kwargs): |
|
37 | def clear_output(self, *pargs, **kwargs): | |
47 | with self: |
|
38 | with self: | |
48 | clear_output(*pargs, **kwargs) |
|
39 | clear_output(*pargs, **kwargs) | |
49 |
|
40 | |||
50 | def __enter__(self): |
|
41 | def __enter__(self): | |
51 | """Called upon entering output widget context manager.""" |
|
42 | """Called upon entering output widget context manager.""" | |
52 | if self._kernel is not None: |
|
|||
53 |
|
|
43 | self._flush() | |
54 | session = self._kernel.session |
|
44 | kernel = get_ipython().kernel | |
|
45 | session = kernel.session | |||
55 |
|
|
46 | send = session.send | |
56 |
|
|
47 | self._original_send = send | |
57 |
|
|
48 | self._session = session | |
@@ -69,7 +60,7 b' class Output(DOMWidget):' | |||||
69 |
|
|
60 | header=header, metadata=metadata) | |
70 |
|
61 | |||
71 |
|
|
62 | # If this is a message type that we want to forward, forward it. | |
72 |
|
|
63 | if stream is kernel.iopub_socket and msg_type in ['clear_output', 'stream', 'display_data']: | |
73 |
|
|
64 | self.send(msg) | |
74 |
|
|
65 | else: | |
75 |
|
|
66 | send(stream, msg, ident=ident, buffers=buffers, track=track) | |
@@ -78,7 +69,6 b' class Output(DOMWidget):' | |||||
78 |
|
69 | |||
79 | def __exit__(self, exception_type, exception_value, traceback): |
|
70 | def __exit__(self, exception_type, exception_value, traceback): | |
80 | """Called upon exiting output widget context manager.""" |
|
71 | """Called upon exiting output widget context manager.""" | |
81 | if self._kernel is not None: |
|
|||
82 |
|
|
72 | self._flush() | |
83 |
|
|
73 | self._session.send = self._original_send | |
84 |
|
74 |
General Comments 0
You need to be logged in to leave comments.
Login now