Displaying Rich Text and Colourful output on Python Terminal

Priyam Mehta
4 min readMar 19, 2021

Working on terminal everyday? At some point, you’ll be tired and boared of all those black and white lines on screen.

So here’s a simple trick to beautify your Python code, like this |

Yup, that’s how cool your output could look.

Now, this can be done by an awesome python library called Rich

For this, open up a fresh terminal window and, install Rich using

$pip install rich

Now for the starters, all you need to do is

from rich import print

this overrides your conventional print method, to make it look sexy.

output on terminal with rich

and this is how it looked without rich

output on terminal without rich

and that’s not it, with rich not only you can change individual colour of printed text, but also make it underlined, Bold or italic or strikethrough, reverse and even make it blink

print("Inhibit, [bold red]Mars[/bold red]!!", ":rocket:")
print(locals())

You can also make your Bools and numbers look awesome, using

>>> from rich import pretty>>> pretty.install()

→ NOW HERE’S THE COOLEST PART

Inspect And Log.

from rich.console import Console

console = Console()
console.print("Where there is a [bold cyan]Will[/bold cyan] there [u]is[/u] a [i]way[/i].")

Now let’s Inspect!

>>> my_list = ["foo", "bar"]
>>> from rich import inspect
>>> inspect(my_list, methods=True)

let’s try something more complicated!!

from rich.console import Consoleconsole = Console()test_data = [           {“jsonrpc”: “2.0”, “method”: “sum”, “params”: [None, 1, 2, 4, False, True], “id”: “1”,},           {“jsonrpc”: “2.0”, “method”: “notify_hello”, “params”: [7]},           {“jsonrpc”: “2.0”, “method”: “subtract”, “params”: [42, 23], “id”: “2”},]def someFunc():    a = False    b = [1, 2, {“foo”: “bar”}]    c = {         “Aniken Skywalker” : “Darth Vader”,          “Rey” : “Last Jedi”,          “Iron man”: “Killed Thanos”    }    console.log(test_data, log_locals = True)someFunc()

and that’s how it looks.

the “log_locals = True” tag shows the values of all the local variables.

and the adding the tag “methods=True”, with inspect like

inspect(my_list, methods=True)

will show a list of methods.

And Guess what ?! They Have Progress Bars

the basic usage is as follows:

from rich.progress import track

for step in track(range(100)):
do_step(step)

# or
import time

from rich.progress import Progress

with Progress() as progress:

task1 = progress.add_task("[red]Downloading...", total=1000)


while not progress.finished:
progress.update(task1, advance=0.5)
time.sleep(0.02)

to add multiple progress bars,

import time

from rich.progress import Progress

with Progress() as progress:

task1 = progress.add_task("[red]Downloading...", total=1000)
task2 = progress.add_task("[green]Processing...", total=1000)
task3 = progress.add_task("[cyan]Cooking...", total=1000)

while not progress.finished:
progress.update(task1, advance=0.5)
progress.update(task2, advance=0.3)
progress.update(task3, advance=0.9)
time.sleep(0.02)

even modifying it is pretty simple, refer this example.

or read more here.

Sometimes, its hard to Calculate progress for multiple tasks, so you can use status as follows:

from time import sleep
from rich.console import Console

console = Console()
tasks = [f"task {n}" for n in range(1, 11)]

with console.status("[bold green]Working on tasks...") as status:
while tasks:
task = tasks.pop(0)
sleep(1)
console.log(f"{task} complete")

And This is the Most Coolest Part : Aweosme Tracebacks

Who loves errors ? A buggy code is what a developer hates the most… Even though I can’t help you with buggy codes, I can help you with making them look good… just to make you feel less shitty.

And to add chocolate to the cake (I don’t know if it’s real reference, but, it sounds tasty), it’ as easy as 2 lines. Nothing more. No bugs.

from rich.traceback import install
install()

just add these 2 lines in the beginning of your code and Voila ! The errors look great !!

So that was it people. And for this awesome project we can thank Will McGugan, and if you want to check out the whole library out, go here.

--

--