Open In App

Python | How to make a terminal progress bar using tqdm

Whether you’re installing software, loading a page, or doing a transaction, it always eases your mind whenever you see that small progress bar giving you an estimation of how long the process would take to complete or render. If you have a simple progress bar in your script or code, it looks very pleasing to the eye and gives proper feedback to the user whenever they execute the code. You can use the Python external library tqdm, to create simple & hassle-free progress bars which you can add to your code and make it look lively!
 

Installation

Open your command prompt or terminal and type: 
 



pip install tqdm

If you are using Python3 then type: 
 

pip3 install tqdm

This command would successfully install the library on your computer and is now ready to use.
 



Usage

Using tqdm is very simple, you just need to add your code between tqdm() after importing the library in your code. You need to make sure that the code you put in between the tqdm() function must be iterable or it would not work at all.
Let us see the following example that would help you understand better:
Example:
 




from tqdm import tqdm
 
 
for i in tqdm(range(int(9e6))):
    pass

Output:
 

Now that we know how to implement tqdm, let’s take a look at some of the important parameters it offers and how it can be used to tweak the progress bar. 
 

tqdm (self, iterable, desc= “Text You want”)

Example: 




from tqdm import tqdm
from time import sleep
 
 
for i in tqdm(range(0, 100), desc ="Text You Want"):
    sleep(.1)

Output: 
 

tqdm (self, iterable, total= 500)

Example:




from tqdm import tqdm
from time import sleep
 
 
for i in tqdm(range(0, 100), total = 500,
              desc ="Text You Want"):
    sleep(.1)

Output:
 

tqdm (self, iterable, disable=True)

Example: 




from tqdm import tqdm
from time import sleep
 
 
for i in tqdm(range(0, 100), disable = True,
               desc ="Text You Want"):
    sleep(.1)
 
print("Iteration Successful")

Output:
 

tqdm (self, iterable, ncols= 100)

Example:




from tqdm import tqdm
from time import sleep
 
 
for i in tqdm(range(0, 100), ncols = 100,
               desc ="Text You Want"):
    sleep(.1)

Output:

tqdm (self, iterable, mininterval=3)

Example:




from tqdm import tqdm
from time import sleep
 
 
for i in tqdm(range(0, 100), mininterval = 3,
              desc ="Text You Want"):
    sleep(.1)

Output:
 

tqdm (self, iterable, ascii= “123456789$”, desc=”Text You Want”)

Example:




from tqdm import tqdm
from time import sleep
 
 
for i in tqdm(range(0, 100),
              ascii ="123456789$"):
    sleep(.1)

Output:

tqdm (self, iterable, unit= “ ticks”)

Example: 




from tqdm import tqdm
from time import sleep
 
 
for i in tqdm(range(0, 100), unit =" ticks",
              desc ="Text You Want"):
    sleep(.1)

Output: 
 

tqdm (self, iterable, initial=50)

Example:




from tqdm import tqdm
from time import sleep
 
 
for i in tqdm(range(0, 100), initial = 50,
              desc ="Text You Want"):
    sleep(.1)

Output:
 

The counter would start from 50 and the progress bar would disappear after the final counter is reached. The loop would still run until the iteration is complete. 


Article Tags :