How to Make a Terminal Progress Bar using tqdm in Python

Whenever we install a Python library, module, or software, we see a progress bar appear on the screen, which denotes a small progress bar that estimates how long the process will take to complete or render. It gives the impression of activity and can help to relax us. We’ve all seen the various progress bars.

The percentage of progress made in completing a task is represented by progress bars. The progress can be calculated by dividing the number of items processed by the total number of input items. Several factors influence the progress bar, including network speed, latency, and whether or not data is persisting into local storage to provide a more accurate ETA (Estimated Time of Arrival).

Using the Python external library tqdm, we can create simple and easy progress bars.

Python tqdm library:

The tqdm is a Python library that provides functions that wrap around the specified iterable and output a smart progress bar.
Python is a popular programming language for performing computationally intensive tasks that take a long time to complete.
The progress of these tasks is indicated by a tqdm progress bar.

The name “tqdm” is derived from the Arabic word “taqadum,” which means “progress.”
The library does support customizable progress bars, but at its core, the code tqdm(iterable) is sufficient to get you started with a smart progress metre displaying the iterable’s progress.

Installation

pip install tqdm

Use of tqdm:

To use tqdm, simply add your code between tqdm() after importing the library into your code. You must ensure that the code you insert between the tqdm() function is iterable or it will not work.

Approach:

  • Import tqdm from tqdm module using the import keyword.
  • Loop till the 7e^5 to make the progress bar using the for loop.
  • The Exit of the Program.

Below is the implementation:

# Import tqdm from tqdm module using the import keyword
from tqdm import tqdm

# Loop till the 7e^5 to make the progress bar using the for loop
for k in tqdm(range(int(7e5))):
    pass

Output:

100%|██████████| 700000/700000 [00:00<00:00, 1540207.48it/s]

Parameters of tqdm

1) desc: This is used to specify the description of your progress bar

Syntax:

tqdm (self, iterable, desc= "text")

Example

Approach:

  • Import tqdm from tqdm module using the import keyword.
  • Import sleep from time module using the import keyword.
  • Loop till the 80 by specifying any sample description using the desc argument.
  • Sleep for 1 second using the sleep() function.
  • The Exit of the Program.

Below is the implementation:

# Import tqdm from tqdm module using the import keyword
from tqdm import tqdm
# Import sleep from time module using the import keyword
from time import sleep

# Loop till the 80 by specifying any sample description using the desc argument
for i in tqdm(range(0, 80), desc ="PythonProgarms"):
  # Sleep for 1 second using the sleep() function
    sleep(.1)

Output:

PythonProgarms: 100%|██████████| 80/80 [00:08<00:00, 9.75it/s]

2) total: It specifies the total number of expected iterations if not specified already or requires any modification.

Syntax:

tqdm (self, iterable, total= 500)

Example

# Import tqdm from tqdm module using the import keyword
from tqdm import tqdm
# Import sleep from time module using the import keyword
from time import sleep

# Loop till 80 by specifying the total number of expected iterations using the total argument
for i in tqdm(range(0, 80), total = 400, desc ="PythonProgarms"):
    # Sleep for 2 seconds using the sleep() function
    sleep(.2)

Output:

PythonProgarms: 20%|██ | 80/400 [00:16<01:04, 4.93it/s]

3) disable: If you wish to totally disable the progress bar, use this parameter.

Syntax:

tqdm (self, iterable, disable=True)

Example

# Import tqdm from tqdm module using the import keyword
from tqdm import tqdm
# Import sleep from time module using the import keyword
from time import sleep

# Loop till the 80 and if we set disable as true then it will not display progress bar
for i in tqdm(range(0, 80), disable = True, desc ="PythonProgarms"):
    # Sleep for 2 seconds using the sleep() function
    sleep(.2)
 
print("The iteration is Success")

Output:

The iteration is Success

4) ncols: It represents the total width of the output message. If left unspecified, it defaults to the size of the window. The ncols parameter can be used to fix this.

Syntax:

tqdm (self, iterable, ncols= 100)

Example

# Import tqdm from tqdm module using the import keyword
from tqdm import tqdm
# Import sleep from time module using the import keyword
from time import sleep

# Loop till the 80 and here specify the width of output message using the ncols argument
for i in tqdm(range(0, 80), ncols = 70, desc ="PythonProgarms"):
    # Sleep for 2 seconds using the sleep() function
    sleep(.2)

Output:

PythonProgarms: 100%|█████████████████| 80/80 [00:16<00:00, 4.94it/s]

5) mininterval: Using this parameter, we can easily modify the minimum progress display update. The default value is 0.1 seconds.

Syntax:

tqdm (self, iterable, mininterval=3)

Example

# Import tqdm from tqdm module using the import keyword
from tqdm import tqdm
# Import sleep from time module using the import keyword
from time import sleep

# Loop till the 80 and min interval shows the minimum progress display update
for i in tqdm(range(0, 80), mininterval = 4, desc ="PythonProgarms"):
    # Sleep for 2 seconds using the sleep() function
    sleep(.2)

Output:

PythonProgarms: 100%|██████████| 80/80 [00:16<00:00, 4.99it/s]

6) ascii: ASCII characters can be used to fill the progress bar of your choice.

Syntax:

tqdm (self, iterable, ascii= "123456789$", desc="text")

Example

# Import tqdm from tqdm module using the import keyword
from tqdm import tqdm
# Import sleep from time module using the import keyword
from time import sleep

# Loop till the 80 and we filled the progress bar with the ascii characters
for i in tqdm(range(0, 80), ascii ="123456789$"):
    # Sleep for 2 seconds using the sleep() function
    sleep(.2)
Output:
100%|$$$$$$$$$$| 80/80 [00:16<00:00, 4.95it/s]

7) unit: The default unit of time is “it,” which can be changed to your preferred unit by using this argument.

Syntax:

tqdm (self, iterable, unit= "ticks")

Example

# Import tqdm from tqdm module using the import keyword
from tqdm import tqdm
# Import sleep from time module using the import keyword
from time import sleep

# Loop till the 80 and here if we specify unit as ticks we get number of ticks per second
for i in tqdm(range(0, 80), unit =" ticks", desc ="PythonProgarms"):
    # Sleep for 2 seconds using the sleep() function
    sleep(.2)

Output:

PythonProgarms: 100%|██████████| 80/80 [00:16<00:00, 4.94 ticks/s]

8) initial: The progress bar’s initial value is set to 0. If you want to change this, you may use this argument to set the progress bar to any value you want.

Syntax:

tqdm (self, iterable, initial=50)

Example

# Import tqdm from tqdm module using the import keyword
from tqdm import tqdm
# Import sleep from time module using the import keyword
from time import sleep

# Loop till the 80 and if we specify initial argument then it will be the starting value of the progress bar
for i in tqdm(range(0, 80), initial = 20, desc ="PythonProgarms"):
    # Sleep for 2 seconds using the sleep() function
    sleep(.2)

Output:

PythonProgarms: 100it [00:16, 4.95it/s]