Show More
@@ -80,7 +80,8 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 | widgets[w.description] = w |
|
83 | if hasattr(w, 'description'): | |
|
84 | widgets[w.description] = w | |||
84 |
|
85 | |||
85 | for key, d in to_check.items(): |
|
86 | for key, d in to_check.items(): | |
86 | nt.assert_in(key, widgets) |
|
87 | nt.assert_in(key, widgets) | |
@@ -138,7 +139,7 b' def test_single_value_float():' | |||||
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), |
|
142 | nt.assert_equal(len(c.children), 2) | |
142 | w = c.children[0] |
|
143 | w = c.children[0] | |
143 | check_widget(w, |
|
144 | check_widget(w, | |
144 | cls=widgets.IntSlider, |
|
145 | cls=widgets.IntSlider, | |
@@ -157,7 +158,7 b' def test_list_tuple_2_int():' | |||||
157 | c = interactive(f, tup=(1,-1)) |
|
158 | c = interactive(f, tup=(1,-1)) | |
158 | 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) ]: | |
159 | c = interactive(f, tup=(min, max), lis=[min, max]) |
|
160 | c = interactive(f, tup=(min, max), lis=[min, max]) | |
160 |
nt.assert_equal(len(c.children), |
|
161 | nt.assert_equal(len(c.children), 3) | |
161 | d = dict( |
|
162 | d = dict( | |
162 | cls=widgets.IntSlider, |
|
163 | cls=widgets.IntSlider, | |
163 | min=min, |
|
164 | min=min, | |
@@ -174,7 +175,7 b' def test_list_tuple_3_int():' | |||||
174 | c = interactive(f, tup=(1,2,-1)) |
|
175 | c = interactive(f, tup=(1,2,-1)) | |
175 | 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) ]: | |
176 | c = interactive(f, tup=(min, max, step), lis=[min, max, step]) |
|
177 | c = interactive(f, tup=(min, max, step), lis=[min, max, step]) | |
177 |
nt.assert_equal(len(c.children), |
|
178 | nt.assert_equal(len(c.children), 3) | |
178 | d = dict( |
|
179 | d = dict( | |
179 | cls=widgets.IntSlider, |
|
180 | cls=widgets.IntSlider, | |
180 | min=min, |
|
181 | min=min, | |
@@ -191,7 +192,7 b' def test_list_tuple_2_float():' | |||||
191 | c = interactive(f, tup=(0.5,-0.5)) |
|
192 | c = interactive(f, tup=(0.5,-0.5)) | |
192 | 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.) ]: | |
193 | c = interactive(f, tup=(min, max), lis=[min, max]) |
|
194 | c = interactive(f, tup=(min, max), lis=[min, max]) | |
194 |
nt.assert_equal(len(c.children), |
|
195 | nt.assert_equal(len(c.children), 3) | |
195 | d = dict( |
|
196 | d = dict( | |
196 | cls=widgets.FloatSlider, |
|
197 | cls=widgets.FloatSlider, | |
197 | min=min, |
|
198 | min=min, | |
@@ -210,7 +211,7 b' def test_list_tuple_3_float():' | |||||
210 | c = interactive(f, tup=(1,2.,-1.)) |
|
211 | c = interactive(f, tup=(1,2.,-1.)) | |
211 | 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.) ]: | |
212 | c = interactive(f, tup=(min, max, step), lis=[min, max, step]) |
|
213 | c = interactive(f, tup=(min, max, step), lis=[min, max, step]) | |
213 |
nt.assert_equal(len(c.children), |
|
214 | nt.assert_equal(len(c.children), 3) | |
214 | d = dict( |
|
215 | d = dict( | |
215 | cls=widgets.FloatSlider, |
|
216 | cls=widgets.FloatSlider, | |
216 | min=min, |
|
217 | min=min, | |
@@ -224,7 +225,7 b' def test_list_tuple_str():' | |||||
224 | values = ['hello', 'there', 'guy'] |
|
225 | values = ['hello', 'there', 'guy'] | |
225 | first = values[0] |
|
226 | first = values[0] | |
226 | c = interactive(f, tup=tuple(values), lis=list(values)) |
|
227 | c = interactive(f, tup=tuple(values), lis=list(values)) | |
227 |
nt.assert_equal(len(c.children), |
|
228 | nt.assert_equal(len(c.children), 3) | |
228 | d = dict( |
|
229 | d = dict( | |
229 | cls=widgets.Dropdown, |
|
230 | cls=widgets.Dropdown, | |
230 | value=first, |
|
231 | value=first, | |
@@ -471,7 +472,7 b' def test_call_decorated_kwargs_on_trait_change():' | |||||
471 |
|
472 | |||
472 | def test_fixed(): |
|
473 | def test_fixed(): | |
473 | c = interactive(f, a=widgets.fixed(5), b='text') |
|
474 | c = interactive(f, a=widgets.fixed(5), b='text') | |
474 |
nt.assert_equal(len(c.children), |
|
475 | nt.assert_equal(len(c.children), 2) | |
475 | w = c.children[0] |
|
476 | w = c.children[0] | |
476 | check_widget(w, |
|
477 | check_widget(w, | |
477 | cls=widgets.Text, |
|
478 | cls=widgets.Text, |
@@ -37,7 +37,11 b' class Output(DOMWidget):' | |||||
37 | def __init__(self, *args, **kwargs): |
|
37 | def __init__(self, *args, **kwargs): | |
38 | super(Output, self).__init__(*args, **kwargs) |
|
38 | super(Output, self).__init__(*args, **kwargs) | |
39 | from IPython import get_ipython |
|
39 | from IPython import get_ipython | |
40 |
|
|
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 | |||
41 |
|
45 | |||
42 | def clear_output(self, *pargs, **kwargs): |
|
46 | def clear_output(self, *pargs, **kwargs): | |
43 | with self: |
|
47 | with self: | |
@@ -45,37 +49,38 b' class Output(DOMWidget):' | |||||
45 |
|
49 | |||
46 | def __enter__(self): |
|
50 | def __enter__(self): | |
47 | """Called upon entering output widget context manager.""" |
|
51 | """Called upon entering output widget context manager.""" | |
48 | self._flush() |
|
52 | if self._kernel is not None: | |
49 | kernel = self._ip.kernel |
|
53 | self._flush() | |
50 | session = kernel.session |
|
54 | session = self._kernel.session | |
51 | send = session.send |
|
55 | send = session.send | |
52 | self._original_send = send |
|
56 | self._original_send = send | |
53 | self._session = session |
|
57 | self._session = session | |
54 |
|
58 | |||
55 | def send_hook(stream, msg_or_type, content=None, parent=None, ident=None, |
|
59 | def send_hook(stream, msg_or_type, content=None, parent=None, ident=None, | |
56 | buffers=None, track=False, header=None, metadata=None): |
|
60 | buffers=None, track=False, header=None, metadata=None): | |
57 |
|
61 | |||
58 | # Handle both prebuild messages and unbuilt messages. |
|
62 | # Handle both prebuild messages and unbuilt messages. | |
59 | if isinstance(msg_or_type, (Message, dict)): |
|
63 | if isinstance(msg_or_type, (Message, dict)): | |
60 | msg_type = msg_or_type['msg_type'] |
|
64 | msg_type = msg_or_type['msg_type'] | |
61 | msg = dict(msg_or_type) |
|
65 | msg = dict(msg_or_type) | |
62 | else: |
|
66 | else: | |
63 | msg_type = msg_or_type |
|
67 | msg_type = msg_or_type | |
64 | msg = session.msg(msg_type, content=content, parent=parent, |
|
68 | msg = session.msg(msg_type, content=content, parent=parent, | |
65 | header=header, metadata=metadata) |
|
69 | header=header, metadata=metadata) | |
66 |
|
70 | |||
67 | # If this is a message type that we want to forward, forward it. |
|
71 | # If this is a message type that we want to forward, forward it. | |
68 | if stream is kernel.iopub_socket and msg_type in ['clear_output', 'stream', 'display_data']: |
|
72 | if stream is self._kernel.iopub_socket and msg_type in ['clear_output', 'stream', 'display_data']: | |
69 | self.send(msg) |
|
73 | self.send(msg) | |
70 | else: |
|
74 | else: | |
71 | send(stream, msg, ident=ident, buffers=buffers, track=track) |
|
75 | send(stream, msg, ident=ident, buffers=buffers, track=track) | |
72 |
|
76 | |||
73 | session.send = send_hook |
|
77 | session.send = send_hook | |
74 |
|
78 | |||
75 | def __exit__(self, exception_type, exception_value, traceback): |
|
79 | def __exit__(self, exception_type, exception_value, traceback): | |
76 | """Called upon exiting output widget context manager.""" |
|
80 | """Called upon exiting output widget context manager.""" | |
77 | self._flush() |
|
81 | if self._kernel is not None: | |
78 | self._session.send = self._original_send |
|
82 | self._flush() | |
|
83 | self._session.send = self._original_send | |||
79 |
|
84 | |||
80 | def _flush(self): |
|
85 | def _flush(self): | |
81 | """Flush stdout and stderr buffers.""" |
|
86 | """Flush stdout and stderr buffers.""" |
General Comments 0
You need to be logged in to leave comments.
Login now