Creating a Python Script to demonstrate Rich

Now, since we have the rich package installed in the virtual environment, we can use the rich package in a python script and leverage its formatting and logging styles in the terminal I/O. We’ll first create a hello word script in python and demonstrate a few other functions and operators provided by the rich package.

Python3




from rich import print
 
print("Hello, [bold green]Geeks[/bold green]!")


 

Here, we are importing a function called print from the rich package which we just installed. The print function is different from the regular print function provided by python. Though it’s technically serving the same purpose it can do a lot more than a normal print function. In the above example, the print function has a few things written which are interpreted by python as a formatting style like the [bold green] and [/bold green]. 

These two tags simply represent the start and end of the styling provided in the square brackets. The styles provided in this example are bold and green. You can choose a few options for the decoration of the text as provided below:

 The colors can be chosen from the list provided in the docs with this link, the colors are 256 Standard colors along with a CSS-styled color format. The decoration like bold, italics, underlined, strike, reverse, etc. can be provided in the list from the documentation. 

Installing and Using Rich Package in Python

In this article, We are going to learn how to install and use rich packages in Python.

RIch is a python package for creating some awesome terminal formatting and logging. It has several features and functions that can make your application look nicer and even add a new look to your CLI application. We will be understanding the process of installing and basic usage of the RICH package in Python in this article.

Similar Reads

Installing Rich

Rich is a python package, so we can install it with the pip command, you can directly use the pip install command to install rich but if you don’t want to mess up your global python environment, you can use a virtualenv to try out and play with a package in isolation. So, we will first set up a virtual environment with the virtualenv package...

Setting up a virtual environment

To set up a virtual environment follow the procedure explained in the article Create virtual environment using venv....

Using pip to install rich

We can now safely install any packages in our activated virtual environment, to install rich we need to run the following command:...

Creating a Python Script to demonstrate Rich

Now, since we have the rich package installed in the virtual environment, we can use the rich package in a python script and leverage its formatting and logging styles in the terminal I/O. We’ll first create a hello word script in python and demonstrate a few other functions and operators provided by the rich package....

Creating an informative tasks display

...

Tables in Terminal with Rich

Python3 from time import sleep from rich.console import Console   console = Console() tasks = [f"Task {n}" for n in range(1, 8)]   with console.status("[bold dark_orange]Finishing tasks...") as status:     while tasks:         task = tasks.pop(0)         sleep(1)         console.log(f"{task} complete")...

Rendering Markdown file

...

Trees in Rich

We can even display tables in a rich format with the rich package. The rich package has a way to represent tabular data in the terminal with the Tables class.  We can construct a few sample tables to understand the fundamentals of rendering the tabular data to the console....