Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I suppress tracebacks in Jupyter?

I want to hide tracebacks in my Python code in Jupyter notebooks, so only the error type and message are displayed.

This answer suggests sys.tracebacklimit = 0 but trying that gave the following:

ERROR:root:Internal Python error in the inspect module.
Below is the traceback from this internal error.

ERROR:root:Internal Python error in the inspect module.
Below is the traceback from this internal error.

Traceback (most recent call last): 
AssertionError 
Traceback (most recent call last): 
AssertionError

That answer also suggests replacing sys.excepthook with a custom function, but the traceback was still displayed.

How can I hide the traceback?

like image 603
Serp C Avatar asked Sep 14 '17 15:09

Serp C


People also ask

How do you not show traceback in python?

The easiest way to configure these is using the jupyter_nbextensions_configurator serverextension, but you can also configure them directly with a few lines of python. The available options are: skip-traceback. animation_duration - duration (in milliseconds) of the show/hide traceback animations.

What does %% capture do in Jupyter?

IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.

How do I stop printing warnings in Jupyter notebook?

Use warnings. filterwarnings() to ignore deprecation warnings filterwarnings(action, category=DeprecationWarning) with action as "ignore" and category set to DeprecationWarning to ignore any deprecation warnings that may rise.

How do you stop a code block in Jupyter notebook?

Just import 'exit' from the code beneath into your jupyter notebook (IPython notebook) and calling 'exit()' should work.


1 Answers

I have found a couple ways to do this, both involving monkeypatching IPython.

#1. This will output just the exception type and message but highlighted in red in the output area:

from __future__ import print_function  # for python 2 compatibility
import sys
ipython = get_ipython()

def exception_handler(exception_type, exception, traceback):
    print("%s: %s" % (exception_type.__name__, exception), file=sys.stderr)

ipython._showtraceback = exception_handler

#2. This will output the exception and color code the exception type (just like Jupyter normally does, but without the traceback):

import sys
ipython = get_ipython()

def hide_traceback(exc_tuple=None, filename=None, tb_offset=None,
                   exception_only=False, running_compiled_code=False):
    etype, value, tb = sys.exc_info()
    value.__cause__ = None  # suppress chained exceptions
    return ipython._showtraceback(etype, value, ipython.InteractiveTB.get_exception_only(etype, value))

ipython.showtraceback = hide_traceback
like image 56
Serp C Avatar answered Sep 28 '22 16:09

Serp C