Calmcode - rich: save

How to save Rich output in files.

1 2 3 4 5 6 7 8 9 10 11 12

Tracebacks in rich are great, but they can be a lot to scroll through. Instead of scrolling in the terminal, it may be a much better idea to save the traceback into an html file and to read from a browser instead.

The script below shows how you can record the console and save the output into an html file.

import time
from rich.console import Console
from rich.traceback import install
install()

def add_two(n1, n2):
    console.log("About to add two numbers.", log_locals=True)
    return n1 + n2

try:
    console = Console(record=True)
    for i in range(10):
        time.sleep(0.2)
        add_two(1, i)
    add_two(1, 'a')
except:
    console.print_exception()

console.save_html("demo.html")