This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.

Python’s Rich Library – a Tutorial

TheAutomatic.net

Contributor:
TheAutomatic.net
Visit: TheAutomatic.net

By:

Blogger, TheAutomatic.net, and Senior Data Scientist

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.

1pip 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.

12from rich import prettypretty.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 Pythonrich 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.

1234from 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).

12345import 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.

12345from 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.

Visit TheAutomatic.net for additional Python code such as how to create formatted tables and inspect Python objects with rich:
http://theautomatic.net/2021/01/05/pythons-rich-library-a-tutorial/
.

Disclosure: Interactive Brokers

Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from TheAutomatic.net and is being posted with permission from TheAutomatic.net. The views expressed in this material are solely those of the author and/or TheAutomatic.net and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.

trading top