The tqdm library in Python is a popular tool used for displaying progress bars in loops. It provides a quick and straightforward way to add progress indicators to your existing code, which can be very useful when running long-running processes. The name “tqdm” stands for “taqaddum” (تقدّم) in Arabic or “progress” in English.
Here are some key features of the tqdm library:
- Ease of Use: You can add a progress bar to a loop with a simple wrapper around any iterable.
- Flexibility:
tqdmworks in a variety of settings including standard Python scripts, IPython (Jupyter) notebooks, and even within console scripts. - Customization: It offers numerous options to customize the progress bar according to your needs (e.g., changing the bar style, adding custom messages).
- Performance: It’s lightweight and has a minimal performance overhead.
Basic Usage
Here is a simple example of how to use tqdm:
from tqdm import tqdm
import time
# Simulate a task with a loop
for i in tqdm(range(100)):
time.sleep(0.1) # simulate some work
This code will display a progress bar that updates each time the loop iterates.
Advanced Features
- Nested Loops:
tqdmautomatically handles nested loops and can display separate progress bars for each. - Manual Update: You can manually control when and how the progress bar updates, which is useful for loops where each iteration does not correspond to a single, uniform step towards completion.
- Integration with Pandas:
tqdmcan integrate with Pandas operations viatqdm.pandas()for showing progress bars duringapply(),groupby(), or other Pandas operations.
Installation
You can install tqdm using pip:
pip install tqdm
This library is a handy tool for tracking the progress of operations, making it easier to estimate the time remaining for a process to complete, especially in data processing or batch jobs.