Improved example code for trackback

- Show how to create a `console` object
- Demonstrate the `show_locals=True` parameter

Refs #1380
This commit is contained in:
Simon Willison 2021-08-03 08:51:39 -07:00 committed by GitHub
parent 6169848d89
commit 121d72721f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 3 deletions

View File

@ -9,11 +9,14 @@ Printing tracebacks
The :meth:`~rich.console.Console.print_exception` method will print a traceback for the current exception being handled. Here's an example::
import rich
try:
do_something()
except:
console.print_exception()
console = rich.console.Console()
console.print_exception(show_locals=True)
The ``show_locals=True`` parameter causes Rich to display the value of local variables for each section of the traceback.
Traceback handler
-----------------
@ -21,6 +24,6 @@ Traceback handler
Rich can be installed as the default traceback handler so that all uncaught exceptions will be rendered with highlighting. Here's how::
from rich.traceback import install
install()
install(show_locals=True)
There are a few options to configure the traceback handler, see :func:`~rich.traceback.install` for details.
There are a few options to configure the traceback handler, see :func:`~rich.traceback.install` for details.