Python’s rich library – a tutorial

The Python rich library is a package for having clearer, styled, and colored output in the terminal. rich works across multiple operating systems – including Windows, Linux, and macOS. In this post, we’ll give an introduction to what it can do for you.

You can get started with rich by installing it with pip.

pip install rich

Once you have it installed, open up the command line and type in python. In order to get the additional functionality from rich, you’ll need to do one more step, which you can see below. Running this snippet will allow you to have styled / formatted code interactively. You’ll only need to do this once.

from rich import pretty
pretty.install()

Here’s a couple examples of automatic coloring for a list.

Creating “rich” progress bars

In a previous post, we talked about how to create progress bars with Python. rich can also create progress bars. Below, we import track from rich.progress. Similarly to tqdm, you can wrap an iterable with track to print out a progress bar as the loop completes each iteration.

from rich.progress import track

for num in track(range(100)):
print(num * 2)

Also, like tqdm, progress bars can be created for list and dictionary comprehensions, too! In the snippet below, we use rich to track the download progress of a collection of stocks (see more on scraping stock prices here).

import yahoo_fin.stock_info as si

tickers = si.tickers_dow()

dow_prices = {ticker : si.get_data(ticker) for ticker in track(tickers)}

Word wrapping and font colors

rich can apply word wrapping to printed text so that it fits within the terminal width. To take advantage of this feature, you need to create a Console object.

from rich.console import Console

c = Console()

c.print(“testing word wrap”)

You can also add styling to the printed text, such as modifying the font color. To see the possible colors, check out this link.

Creating formatted tables

rich has functionality to create stylistic tables, as well. Creating tables is made easy by the importing Table from rich.table. Next, you create a Table object, which can take a title as input. Then, the add_column and add_row methods are used (as you can probably guess) to add columns and rows, respectively. Finally, you can print out the table using a Console object, similar to earlier in the post.

from rich.table import Table
from rich.console import Console

table = Table(“First table using rich!”)
table.add_column(“Language”, justify=”right”, style=”bright_yellow”, no_wrap=True)
table.add_column(“Year Initially Released”, style=”green”)
table.add_column(“Most recent version”, justify=”right”, style=”red”)

table.add_row(“”, “Python”, “1991”, “3.9.1”)
table.add_row(“”, “R”, “1993”, “4.0.3”)
table.add_row(“”, “Java”, “1995”, “Java 15”)

console = Console()
console.print(table)

How to inspect Python objects with rich

Another feature of rich is that it can inspect Python objects. The example below inspects a simple list of numbers. If you specify all = True, you can see all of the attributes for the object (the first part of which is shown in the second snapshot below).

import rich

nums = [1, 2, 3, 4, 5]

rich.inspect(nums)

rich.inspect(nums, all = True)

Formatting listing files

Like on Linux, where you can list files in a column structure, rich allows you to do this, as well, regardless of your operating system. This can make the result much easier to read.

import os
import sys
import rich
from rich.columns import Columns

directory = os.listdir()
rich.print(Columns(directory))

Conclusion

That’s it for now! Thanks for reading and while you’re here, check out my other Python posts. The documentation for the Python rich library is available here.

The post Python’s rich library – a tutorial appeared first on Open Source Automation.

Leave a Reply

Your email address will not be published.