pythonjupyter-notebooktraceback

How to abbreviate traceback in Jupyter Notebook?


I documented an XML-API with Jupyter Notebook, so documentation and specification cannot drift apart.

This works great.

As the API also has to handle invalid input, Jupyter Notebook shows - correctly - the traceback.

The traceback is very verbose - I'd like to abbreviate / shorten it - ideally, only the last line should be shown.

request

server.get_licenses("not-existing-id")

current print out in Jupyter Notebook

---------------------------------------------------------------------------
Fault                                     Traceback (most recent call last)
<ipython-input-5-366cceb6869e> in <module>
----> 1 server.get_licenses("not-existing-id")

/usr/lib/python3.9/xmlrpc/client.py in __call__(self, *args)
   1114         return _Method(self.__send, "%s.%s" % (self.__name, name))
   1115     def __call__(self, *args):
-> 1116         return self.__send(self.__name, args)
   1117 
   1118 ##

/usr/lib/python3.9/xmlrpc/client.py in __request(self, methodname, params)
   1456                         allow_none=self.__allow_none).encode(self.__encoding, 'xmlcharrefreplace')
   1457 
-> 1458         response = self.__transport.request(
   1459             self.__host,
   1460             self.__handler,

/usr/lib/python3.9/xmlrpc/client.py in request(self, host, handler, request_body, verbose)
   1158         for i in (0, 1):
   1159             try:
-> 1160                 return self.single_request(host, handler, request_body, verbose)
   1161             except http.client.RemoteDisconnected:
   1162                 if i:

/usr/lib/python3.9/xmlrpc/client.py in single_request(self, host, handler, request_body, verbose)
   1174             if resp.status == 200:
   1175                 self.verbose = verbose
-> 1176                 return self.parse_response(resp)
   1177 
   1178         except Fault:

/usr/lib/python3.9/xmlrpc/client.py in parse_response(self, response)
   1346         p.close()
   1347 
-> 1348         return u.close()
   1349 
   1350 ##

/usr/lib/python3.9/xmlrpc/client.py in close(self)
    660             raise ResponseError()
    661         if self._type == "fault":
--> 662             raise Fault(**self._stack[0])
    663         return tuple(self._stack)
    664 

Fault: <Fault 1: 'company id is not valid'>

my wish output

Fault: <Fault 1: 'company id is not valid'>

Solution

  • As it turns out, that's built into iPython, so you don't need to install or update anything.

    Just put a single cell at the top of your notebook and run %xmode Minimal as the only input. You can also see the documentation with %xmode? or a lot of other "magic method" documentation with %quickref.