Search code examples
pythonjupyter-notebookjupyter-lab

How to hide the error traceback in Jupyter Lab/Notebook?


My goal is to have only a minimal output from pythons exception handler (without the traceback). In a plain python script I can use :

sys.tracebacklimit=0

However in a notebook, this does not seem to work the way I want and the output is actually worse (longer). Is there any quick rescue to this issue?


Solution

  • 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()
        return ipython._showtraceback(etype, value, ipython.InteractiveTB.get_exception_only(etype, value))
    
    ipython.showtraceback = hide_traceback
    

    Above code will print only exception and color code the exception type without trace back. Is this what you are looking for. Paste it in the 1st cell of your notebook and try to run it