TQDM is a Python library that stands for “progress” in Arabic. As the name implies, it is an excellent tool for tracking the progress of long-running loops and code execution, giving you insights into how far along your code is in its execution.
In this Python tutorial, we will explore the use of TQDM, how to use it, and the extra configuration options that are available.
What is TQDM?
TQDM is a console-based progress bar library that allows you to track the progress of your code execution. It has a simple and easy-to-use interface that can be integrated into your Python code with minimal effort. TQDM provides progress bars and counters to loops that can be customized to meet your needs.
TQDM can be used in many scenarios, including:
- Tracking the progress of data processing
- Tracking the progress of machine learning models training
- Tracking the progress of web scraping bots
- Tracking the progress of any long-running Python code
How to use TQDM for your Python Program
Getting started with TQDM is quite easy. First, you need to install the package using pip:
pip install tqdm
After installation, you can import the tqdm
module and use it in your Python code:
from tqdm import tqdm
from time import sleep
for i in tqdm(range(1000)):
sleep(0.1)
In the code above, we import the tqdm module and use it in a for loop to iterate 1000 times. We also added the sleep()
method from the time module to simulate some actual processing, and slow down the loop enough for us to see the progress bar in action.
tqdm adds the following progress bar to the loop, giving us an indication of how far along we are in the execution. It also provides you with the “time elapsed” and “time remaining” (estimate) details, included on the right-side of the progress bar.
Something like the above progressbar should show up in your terminal/console window.
Other Methods
Here are some other ways to use TQDM in different scenarios:
Using tnrange
TQDM provides a tnrange function that is a port of the range function, allowing you to create a progress bar for a range of values. This has the same effect and purpose as the tqdm()
function from earlier.
from tqdm import tnrange
from time import sleep
for i in tnrange(10):
sleep(0.1)
Using nested loops
TQDM can be used with nested loops by wrapping the inner loop with the tqdm function.
from tqdm import tqdm
from time import sleep
for i in tqdm(range(10)):
for j in tqdm(range(100)):
sleep(0.1)
In the code above, we use tqdm to wrap the inner loop to provide a progress bar for the inner loop.
Using TQDM Progress Bars for Parallel Code in Python
TQDM can be used with parallel code using the multiprocessing library. You can create a pool of processes and use tqdm to track the progress of each process.
from tqdm import tqdm
from time import sleep
from multiprocessing import Pool
def process_data(i):
sleep(0.1)
if __name__ == '__main__':
with Pool(processes=4) as pool:
max_iter = 100
with tqdm(total=max_iter) as pbar:
for _ in pool.imap(process_data, range(max_iter)):
pbar.update()
In the code above, we create a pool of 4 processes and use tqdm to track the progress of each process. We use the imap()
function to create an iterator that returns results from the processes as they complete.
Using the map()
function here is not a good idea, as it will cause the tqdm progress to not update until the processing is complete. This is why we are using imap()
instead.
Another slight change here, is that we had to provide tqdm
with the number of iterations our code was going to perform, in the total
parameter. This is necessary in certain programs because of the way they are written. We are not creating an for-loop in the above code where we can use tqdm()
. This is why we
Customization Options
TQDM provides a range of configuration options that allow you to customize the progress bar’s appearance and behavior. Some of the configuration options include:
Changing Color
One of the most useful and desired customizations, is the ability to change the color of a progress bar. Here’s how you can do this Python TQDM using the color
parameter.
from time import sleep
from tqdm import tqdm
for i in tqdm(range(10000), color='green'):
sleep(0.01)
As you can see, the progress bar is now green.
Changing ProgressBar Characters
TQDM uses characters to display the “progress” in the progress bar. We can change these default characters, with a set of our own. The ascii parameter in tqdm()
takes a string of atleast two characters. The first character is used to represent the initial state of the progressbar, whereas the second character defines the completed state of the progressbar.
In the below code, we are using a blank space to represent the ‘lack of progress’ and an equals to sign to represent the progress.
from time import sleep
from tqdm import tqdm
for i in tqdm(range(1000), ascii=" ="):
sleep(0.01)
Animating ProgressBars in TQDM
By adding extra characters to our ascii string, we can achieve an animated effect in our progress bars. The first character below is an empty space, and last character is a completed bar. All the characters in between will be used for animation by continuously looping them as the progressbar “progresses”.
from time import sleep
from tqdm import tqdm
for i in tqdm(range(1000), ascii=" ▖▘▝▗▚▞█"):
sleep(0.01)
This marks the end of the Python TQDM Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.