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.
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.
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.
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).
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.
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:
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.