Using tqdm to Add Progress Bars in Python: Installation, Examples, and Customization
This article introduces the Python tqdm library, explains how to install it, provides multiple practical code examples for adding progress bars to loops, file processing, and data handling, and details various customization options to tailor the progress display to specific needs.
When working with large data sets, time‑consuming tasks, or iterative loops, adding a progress bar can greatly improve visibility of execution status; the Python tqdm library offers a simple and effective solution.
1. What is tqdm?
tqdm (pronounced “ta‑ka‑doom”) is a fast, extensible progress‑bar library that can display real‑time progress for loops, file I/O, data processing, and more, helping developers understand execution flow and speed up debugging.
2. How to use tqdm?
Install the library with:
pip install tqdmImport it in your script:
from tqdm import tqdmBelow are several practical examples.
Example 1 – Iterate over an API list with a progress bar
from tqdm import tqdm
import time
api_list = ["api1", "api2", "api3", "api4", "api5"]
for api in tqdm(api_list, desc="Testing APIs"):
time.sleep(1) # Simulate API call
# Add your API testing code hereExample 2 – Read a large file with a progress bar
from tqdm import tqdm
with open("large_file.txt", "r") as file:
total_lines = sum(1 for _ in file)
with open("large_file.txt", "r") as file:
for line in tqdm(file, total=total_lines, desc="Processing File"):
# Add your file‑processing code hereExample 3 – Process a large dataset with a progress bar
from tqdm import tqdm
data = range(1000000)
for item in tqdm(data, desc="Processing Data"):
# Add your data‑processing code here3. Additional customization options
You can tailor the progress bar using parameters such as desc , total , unit , ncols , bar_format , and disable . For example:
desc: set description text
total: total number of iterations
unit: unit label
ncols: width of the bar
bar_format: custom bar style
disable: turn off the bar and output plain textExamples of custom styles, speed control, unit changes, disabling the bar, setting width, dynamic description updates, showing remaining time, using tqdm in multithreaded/multiprocess environments, initializing values, and logging progress are demonstrated with code snippets throughout the article.
4. Summary
By integrating tqdm into your Python code, you can easily add informative progress bars that enhance monitoring of long‑running operations, improve debugging efficiency, and provide a better development experience for tasks ranging from API testing to large‑scale data processing.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.