Python, a versatile programming language, often involves iterating over large datasets or executing tasks that take time to complete. This is where tqdm
steps in—offering a straightforward way to add progress bars in your Python scripts. The name tqdm
is derived from the Arabic word "taqaddum," meaning "progress," which reflects its primary function perfectly.
Installing tqdm
First, let's install the module. This can be done easily via pip:
pip install tqdm
Once installed, you can import tqdm
and use it in your script to track the progress of operations like loops.
Basic Usage
Here’s a simple example of how to incorporate a progress bar into a loop using tqdm
. Consider the following loop that processes a list of numbers:
from tqdm import tqdm
import time
# Sample list
numbers = list(range(1, 101))
# Using tqdm to add a progress bar
for number in tqdm(numbers):
time.sleep(0.1) # Simulating a time-consuming task
The tqdm()
wrapper turns any iterable into a progress bar in the terminal, automatically displaying the percentage completion and other details.
Enhancing the Progress Bar
tqdm
can show extra information like descriptions, elapsed time, and remaining time. These features are customizable:
from tqdm import tqdm
import time
# Sample list
numbers = list(range(1, 101))
# A more informative progress bar with description
for number in tqdm(numbers, desc="Processing", ncols=100, ascii=True, unit=" iterations"):
time.sleep(0.1)
desc
: Adds a prefix string to the progress bar.ncols
: Specifies the width of the progress bar.ascii
: Uses ASCII characters for the progress bar.unit
: Defines the unit of iteration.
Progress Bars with Pandas
tqdm
also supports integration with pandas, making it easy to track the progress of operations like data frame looping.
import pandas as pd
from tqdm import tqdm
import time
tqdm.pandas()
# Sample DataFrame
df = pd.DataFrame({"numbers": range(1, 101)})
# Simulate a time-consuming operation with progress_apply
df['squared'] = df['numbers'].progress_apply(lambda x: time.sleep(0.1) or x ** 2)
print(df.head())
By using progress_apply
, you can visualize the progress of operations across the DataFrame rows. Note that time.sleep(0.1)
is included to simulate a time-consuming operation, which allows the progress bar to animate properly.
Customizing the Bar with Color
To add color to your progress bars using terminal color codes, you can use the colorama
library. Colorama
allows you to format text output in the terminal with colors, which enhances the user interface of your progress bars.
First, ensure that colorama
is installed:
pip install colorama
Then, integrate it with tqdm
to create colored progress bars:
from tqdm import tqdm
import time
from colorama import init, Fore, Style
# Initialize colorama
init()
# Custom format: red bar, green text for description and percentage
bar_format = (
f"{Fore.GREEN}{{l_bar}}"
f"{Fore.YELLOW}{{bar}}"
f"{Fore.GREEN}{{r_bar}}{Style.RESET_ALL}"
)
# Sample list
numbers = list(range(1, 101))
for number in tqdm(numbers, desc="Green Text", bar_format=bar_format, ncols=75):
time.sleep(0.05)
print(Style.RESET_ALL)
In this example, colorama
is utilized to apply colors via terminal escape codes. The Fore.GREEN
sets the text in green, and Style.RESET_ALL
reverts the text back to normal after the loop completes.
Nested Progress Bars for Jupyter Notebooks
tqdm
supports nested progress bars, which are particularly useful in Jupyter Notebooks where visual tracking of progress for inner loops is desired:
from tqdm.notebook import trange, tqdm
from time import sleep
for i in trange(2, desc='1st loop'):
for j in tqdm(range(100), desc='2nd loop'):
for x in tqdm(range(100), desc='3rd loop'):
sleep(0.01)
This example demonstrates the use of nested tqdm
progress bars within a Jupyter Notebook. Each loop is equipped with a progress bar, showing how each level of the loop progresses independently. The nested visual representation is especially handy in Notebook environments where visual feedback enhances understanding.
Conclusion
Incorporating tqdm into your Python scripts elevates your ability to track and visualize the progress of loops and tasks, making your code more interactive and informative. Whether you're processing data frames or running lengthy scripts, tqdm
ensures you stay informed on progress without much effort. Start experimenting with tqdm
in your projects to bring elegance and functionality to your Python code.