##// END OF EJS Templates
Use output widget with interact
Jonathan Frederic -
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, Output)
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,29 +204,34 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)
207 container.children = c
211 container.children = c
208
212
209 # Build the callback
213 # Build the callback
210 def call_f(name=None, old=None, new=None):
214 def call_f(name=None, old=None, new=None):
211 container.kwargs = {}
215 with output:
212 for widget in kwargs_widgets:
216 container.kwargs = {}
213 value = widget.value
217 for widget in kwargs_widgets:
214 container.kwargs[widget._kwarg] = value
218 value = widget.value
215 if co:
219 container.kwargs[widget._kwarg] = value
216 clear_output(wait=True)
220 if co:
217 if manual:
221 clear_output(wait=True)
218 manual_button.disabled = True
219 try:
220 container.result = f(**container.kwargs)
221 except Exception as e:
222 ip = get_ipython()
223 if ip is None:
224 container.log.warn("Exception in interact callback: %s", e, exc_info=True)
225 else:
226 ip.showtraceback()
227 finally:
228 if manual:
222 if manual:
229 manual_button.disabled = False
223 manual_button.disabled = True
224 try:
225 container.result = f(**container.kwargs)
226 except Exception as e:
227 ip = get_ipython()
228 if ip is None:
229 container.log.warn("Exception in interact callback: %s", e, exc_info=True)
230 else:
231 ip.showtraceback()
232 finally:
233 if manual:
234 manual_button.disabled = False
230
235
231 # Wire up the widgets
236 # Wire up the widgets
232 # If we are doing manual running, the callback is only triggered by the button
237 # If we are doing manual running, the callback is only triggered by the button
General Comments 0
You need to be logged in to leave comments. Login now