Python tqdm Module | How to create a terminal progress bar

Whenever we install any Python library, module, or installing software, we see the progress bar to screen, which denotes the small progress bar that estimates how long the process would take to complete or render. It gives us an impression of activity and can calm the nerves. We all are familiar with the various progress bar. Some of them are attractive and some of them boring.

In this article, we will create the progress bar without involving Python's code core logging library.

Progress bars are filled up according to the percentage of progress made in accomplishing a task. The progress can be calculated by dividing number_of_item_processed by total_input_item. Various factors affect the progress bar, such as network speed, latency, and if persisting data into local storage to derive a more accurate ETA (Estimated Time of Arrival).

We can create simple and hassle-free progress bars using the Python external library named tqdm. We can add it to the code and make it look lovely.

The tqdm stands for taqadum in Arabic, which means progress. Python tqdm module works on various platform such Linux, Window, Mac, etc. and it is also compatible with the IPython/Jupyter notebooks.

Need for Progress Bar

If we are working with the smaller data set, the progress will not bother in our workflow. However, the progress bar can be used for iterating over a dataset, training a model, or encoding a large information set.

  • The progress bar provides us an estimation of the process that has been given the approximation of the time it might take more.
  • It gives us information that the progress is still running and has not been terminated rudely.

Prerequisites

Python 3 must be installed in the system and we can also create a virtual environment to install the tqdm library.

Installation

Open the command-line terminal and type the following.

Or

The above command will successfully install in the system. We can verify it using the following statement.

If there is no error which means this library is successfully installed.

Adding Progress Bars to for Loops

Program

Output:

outer loop:   0%|          | 0/10 [00:00
    

Let's understand another example.

Example - 1

Output:

loop : 100%|██████████| 10/10 [00:01<00:00, 9.08it/s]

Example -

Output:

outer loop:   0%|          | 0/10 [00:00
    

Example -3

Output:

Outer loop:   0%|          | 0/3 [00:00
    

Predictive Manual Updates of Progress Bar

The tqdm module provides a facility to update the progress bar at certain intervals manually. When we download a multi-part file in chunks or streaming data, we can manually update the process bar function. Let's understand the following example.

Example -

Output:

100%|██████████| 100/100 [00:10<00:00, 9.93it/s]

Explanation -

In the above code, we have set attribute as 100. The called function incremented by ten in each iteration until 100% is achieved. We can pass any value to the update() method.

Threaded Progress Bar

We can also trap the Python tqdm package into Python threads. Multiprocessing is the best way to use the total number of cores. The tqdm position argument allows us to specify the line offset to print this bar. It is set on automatic by default in case of unscripted. Let's understand the following example. The value must be specified to manage the multiple bars at once. If we ignore this argument, our bar will be overridden by different threads.

Example -

Output:

progressbar #5:   0%|          | 0/100 [00:00
    

Add Color in Tqdm Progress Bar

Color can make the progress bar very attractive. However, it doesn't add any new functionality to the working of the bar. The tqdm can work with Colorama, a simple cross-platform color terminal text in Python. Let's understand the following example.

Example -

Output:

Python tqdm Module

Conclusion

We have discussed all basic concepts related to the progress bar. Python comes with the tqdm module that helps us to design manually. We have defined suitable examples for important operation that we can perform in tqdm module. The tqdm module can collaborate with the subprocess and threads where we can run the multiple process bars at the same time.






Contact US

Email:[email protected]

Python tqdm Module
10/30