How To Add A Progress Bar In Python

Subscribe to my newsletter and never miss my upcoming articles

Hello world!

When you happen to install a software, download an application or media or load a page, you see a progress bar give you an estimate of how long the process would take to complete or render. You can add one in your code too. In this Blog article, we will learn how to Create Progress Bar. We will see the implementation in Python.

Check out the Repository for Ultimate Resource in python. Drop a star if you find it useful! Got anything to add? Open a PR on the same!

You can refer to my YouTube video Tutorial to see a working tutorial for better Understanding and a step by step Guide of the same.

What will be covered in this Blog

1. tqdm Introduction
2. How to Create Progress Bar

tqdm Introduction:

A progress bar is a graphical control element used to visualize the progression of an extended computer operation, such as a download, file transfer, or installation. We will make use of the Python library tqdm, to create simple progress bars which can be added in the code and make it look lively!

tqdm means "progress" in Arabic (taqadum, تقدّم) and is an abbreviation for "I love you so much" in Spanish (te quiero demasiado). tqdm is Fast and Extensible Progress Meter.

If you wish to know more about it, you can refer to tqdmDocumentation. Use this link to navigate to the documentation.

Now that you are aware of tqdm basics, we can move forward to the coding section. Let's get started!

Time to Code!

You can find all the code at my GitHub Repository. Drop a star if you find it useful.

In order to access the Python library, you need to install it into your Python environment, use the following command to install tqdm

pip install tqdm

Now, let's import the package in our Python script.

from tqdm import tqdm, trange

We will require time package, so lets import that one as well

import time

Let's run a loop for an iterable

for i in range(10):
    pass

In order to see the progress, lets make use of time module

for i in range(10):
    time.sleep(0.4)

Once done, let's add a progress bar for the loop. In order to do so, let's wrap the iterable with tqdm method.

for i in tqdm(range(10)):
    time.sleep(0.4)

Let's have a look at the output. It will look something like this:

ezgif.com-gif-maker.gif

If you wish to get rid of the iterable, you can do so by wrapping the iterable in trange module.

for i in trange(5):
    sleep(0.4)

This is how you can add a progress bar in your python script. That's it!

Simple, isn't it? Hope this tutorial has helped. I would strongly recommend you to Check out the YouTube video of the same and don't forget to subscribe my Channel.

You can play around with the tqdm library and even explore more features. You can even make use of Python GUI using Tkinter.

You can find all the code at my GitHub Repository. Drop a star if you find it useful.

Thank you for reading, I would love to connect with you at Twitter | LinkedIn.

Do share your valuable suggestions, I appreciate your honest feedback!

You should definitely check out my other Blogs:

Resources:

See you in my next Blog article, Take care

Comments (2)

DesignerAnna1's photo

Hey @ayushi7rawat

I read your article on Progress bar in python using tqdm and I made a simple timer with it using the code:

from tqdm import tqdm, trange
import time
for i in tqdm(range(60)):
    time.sleep(1.0)

print("time's up")

Screenshot 2020-11-22 at 1.41.19 PM.jpg

DesignerAnna1's photo

By the way, it doesn't actually run the print command, but it works :)