Trees in Rich
We can also create a tree-like structure in the terminal using Rich. There is a tree class in Rich for displaying hierarchical structures in the terminal, we can create a simple tree instance using the rich tree class.
Python3
from rich.tree import Tree from rich import print tree_example = Tree( "Tree's Root Node" ) tree_example.add( "Node 1" ) print (tree_example) |
Output:
The tree class is instantiated with the Tree method imported from the rich.tree module. This creates a simple root node in the tree, further, we can add the node into the tree by using the add function associated with the tree. Using this simple code, we can create more styled trees by using the console class in rich.
Python3
# rich-tree.py from rich.console import Console from rich.tree import Tree console = Console(width = 100 ) tree = Tree( "Programming Languages" ) python_tree = tree.add( "[b green]Python[/]" ) python_tree.add( "Numpy" ) python_tree.add( "Pandas" ) python_tree.add( "Django" ) python_tree.add( "Flask" ) java_tree = tree.add( "[b dark_orange3]Java[/]" ) java_tree.add( "Spring" ) java_tree.add( "Apache" ) frameworks = [ "Express" , "React" , "Next" , "Vue" , "Angular" ] js_tree = tree.add( "[b yellow]Javascript[/]" ) for framework in frameworks: js_tree.add(framework) console. print (tree) CONSOLE_HTML_FORMAT = """\ <pre style="font-family:Menlo">{code}</pre> """ |
Output:
So in this example, we have used the console class, by adding parameters like width which will set the width of the console and the output screen’s width. Next, we will instantiate a tree class that will create a tree object and stores it in the tree variable, we can name it anything we want as we have named it example_tree in the previous example. As demonstrated earlier we can add nodes to the tree, we will add the node as a new tree, so we will create nested trees. By assigning the returned value of the add function for the tree variable, we have added a new node, we can add nodes further.
Here, the tree variable is the root tree which has nodes like Python, Java, and Javascript with tree class instances as python_tree, java_tree, and js_tree. We have created the python_tree with nodes like Numpy, Pandas, and Django. The java_tree has nodes like Spring and Apache, and finally, we have js_tree nodes as a list of strings which we append dynamically using a for a loop. For each element in the list “frameworks”, we add the nodes in the js_tree.
That’s how we can use the tree class in rich.
Progress Bar in Rich
We can even add progress bars in the terminal using rich. The rich also give a class for displaying progress bars using the track class. The track class gives a view of progress bars until the loop is complete and the given task has reached 100%.
Python3
from rich.progress import track from time import sleep for step in track( range ( 10 )): sleep( 1 ) |
Output:
Here, we have demonstrated a basic example of a simple progress bar for a loop that iterates over a range of 10 numbers and it sleeps for a second hence we see the transition of the progress bar.
We can even perform the task asynchronously and display the progress bar by displaying the love progress.
Python3
import time from rich.progress import Progress with Progress() as progress: task1 = progress.add_task( "[red]Doing Task 1" , total = 100 ) task2 = progress.add_task( "[blue]Doing Task 2" , total = 40 ) task3 = progress.add_task( "[green]Doing Task 3" , total = 500 ) while not progress.finished: progress.update(task1, advance = 0.1 ) progress.update(task2, advance = 0.3 ) progress.update(task3, advance = 0.6 ) time.sleep( 0.01 ) |
Output:
To display a few progress bars on the terminal, we can add use the Progress function in the progress class. We add tasks which are individual progress bars and we increment the current progress, we have included the while loop so that we increment and perform any operation till the progress bar has been completed.
The progress bar is incremented after the task has been performed here the task is simply sleeping for a fraction of a second but that can be any operation like fetching resources from the internet or any other logical operation in the program.
Here, we have added the total parameter in the progress class. The progress class will be displayed, and the task is added by the function add_task which adds a progress bar in the console. The add tasks function takes in the parameter of the task name and the total indicates the number of steps that must be completed for the progress to reach 100%.
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.