Skip to content

Commit

Permalink
Merge pull request #5067 from minrk/widget-error
Browse files Browse the repository at this point in the history
show traceback in widget handlers
  • Loading branch information
ellisonbg committed Feb 8, 2014
2 parents e68e13c + 9853ad8 commit 2182b3f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
10 changes: 9 additions & 1 deletion IPython/html/widgets/interaction.py
Expand Up @@ -20,6 +20,7 @@
from IPython.utils.signatures import signature, Parameter
from inspect import getcallargs

from IPython.core.getipython import get_ipython
from IPython.html.widgets import (Widget, TextWidget,
FloatSliderWidget, IntSliderWidget, CheckboxWidget, DropdownWidget,
ContainerWidget, DOMWidget)
Expand Down Expand Up @@ -205,7 +206,14 @@ def call_f(name, old, new):
container.kwargs[widget.description] = value
if co:
clear_output(wait=True)
container.result = f(**container.kwargs)
try:
container.result = f(**container.kwargs)
except Exception as e:
ip = get_ipython()
if ip is None:
container.log.warn("Exception in interact callback: %s", e, exc_info=True)
else:
ip.showtraceback()

# Wire up the widgets
for widget in kwargs_widgets:
Expand Down
20 changes: 19 additions & 1 deletion IPython/html/widgets/widget.py
Expand Up @@ -14,6 +14,7 @@
#-----------------------------------------------------------------------------
from contextlib import contextmanager

from IPython.core.getipython import get_ipython
from IPython.kernel.comm import Comm
from IPython.config import LoggingConfigurable
from IPython.utils.traitlets import Unicode, Dict, Instance, Bool, List, Tuple
Expand All @@ -33,7 +34,11 @@ def __call__(self, *args, **kwargs):
try:
local_value = callback(*args, **kwargs)
except Exception as e:
self.log.warn("Exception in callback %s: %s", callback, e)
ip = get_ipython()
if ip is None:
self.log.warn("Exception in callback %s: %s", callback, e, exc_info=True)
else:
ip.showtraceback()
else:
value = local_value if local_value is not None else value
return value
Expand All @@ -54,6 +59,18 @@ def register_callback(self, callback, remove=False):
elif not remove and callback not in self.callbacks:
self.callbacks.append(callback)

def _show_traceback(method):
"""decorator for showing tracebacks in IPython"""
def m(self, *args, **kwargs):
try:
return(method(self, *args, **kwargs))
except Exception as e:
ip = get_ipython()
if ip is None:
self.log.warn("Exception in widget method %s: %s", method, e, exc_info=True)
else:
ip.showtraceback()
return m

class Widget(LoggingConfigurable):
#-------------------------------------------------------------------------
Expand Down Expand Up @@ -241,6 +258,7 @@ def _should_send_property(self, key, value):
value != self._property_lock[1]

# Event handlers
@_show_traceback
def _handle_msg(self, msg):
"""Called when a msg is received from the front-end"""
data = msg['content']['data']
Expand Down

0 comments on commit 2182b3f

Please sign in to comment.