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.
1 | 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.
12 | 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.
1234 | 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).
12345 | 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.
12345 | 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.
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 Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus 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 its permission. The views expressed in this material are solely those of the author and/or TheAutomatic.net and Interactive Brokers 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 buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. 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.