Rich: Generate Rich and Beautiful Text in the Terminal with Python | by Khuyen Tran | Towards Data Science

After developing a useful Python package or a useful function, you might wish for your teammates or other users to use your code. However, the output of your code in the terminal is a little bit boring and is confusing to understand.
Is there a way that you can make the output clearer and prettier like below?
That is when Rich comes in handy. Besides beautiful printing, Rich also allows you to:
Produce a beautiful report on any Python object
Debug the output of your function
Create a tree to display a file structure
Create progress bars and status
Create beautiful columns
In this tutorial, I will show you how to do everything mentioned above in a few lines of Python code.
To install Rich, type
pip install rich
To have a quick summary of Rich’s functionalities, type
python -m rich
The outputs are pretty! But how can we reproduce the output like above?
Rich makes it easy to debug with the pretty print function. While icecream allows you to view the code that produces the output, Rich highlights the output based on its datatype.
In the output above, different outputs were highlighted with different colors based on their datatypes. This output is not only pretty but is also useful for debugging when there are many outputs with different datatypes.
Scenerio: You are not sure about the functionalities of the Python object you are using. Instead of searching on Google, you want to have a quick overview of the Python object in your terminal. This could be done using help()
However, the output is quite long and difficult to read.
Luckily, Rich provides inspect , which makes it much easier to view documentation of a Python object within your terminal.
Now you know which datasets are provided withinsklearn.datasets and how to use them!
Scenerio: You want to know which elements in the function created a certain output. So you print every variable in the function.
Solution: You can simply use Console object to print both the output and all the variables in the function.
You can view relevant details that produce the output of your function by simply using console.log ! This will make it easier for you to detect any mistakes in your code and fix them accordingly.
If we want to know the structure of a directory, viewing the directory tree structure can be really helpful. Rich allows us to do so using Tree object.
For example, if our data directory contains 2 types of data: data1 and data2 , we can add these 2 files to the parent directory using tree.add method
Under data2 , we have 3 other files. We can add them to data2 using the same approach as above. But to make it more interesting, we will also add color to our string using ['color_name]your_text'
Now we have a neat tree structure with different colors for different leaves!
It is often useful to know the progress or status of our code. Rich allows us to create a progress bar using progress.track method.
If we want to record the time when a particular task is finished executing, we can use console.status instead
Isn’t it cool? If you don’t like the current spinner, you can search for other spinner options using:
python -m rich.spinner
Then change the default spinner by specifying a different value for spinner parameter inside the console.status method.
Instead of viewing many features in a dataset at once, you might just want to compare the relationship between 2 different features. Rich allows you to view many rows of data at once using neat columns with equal width.
We will download Open ML’s Speed Dating dataset to view the relationship between the age difference between 2 people and whether they matched or not.
Congratulations! You have just learned how to generate beautiful terminal outputs using a few lines of Python code. We all love the Python packages that are cool to look at in the terminal while we’re waiting for the package to do its work.
I hope this article will give you ideas on how to make your workflow more efficient and to develop a package with a cool user interface. I encourage you to explore Rich to learn other features that I didn’t mention in this article.
Feel free to fork and play with the source code of this article here.
I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.
Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these: