Progress bars are a great way to visually indicate progress in your Python programs, especially during tasks that take a significant amount of time. For example, they are commonly used during a for loop that processes large datasets, while downloading files from the internet or even when an application is starting up to indicate loading progress. Let’s now explore the different methods to create progress bars efficiently.
Using tqdm
tqdm is one of the most popular and easiest libraries for showing progress bars in Python. It works well with loops and gives a neat, real-time progress bar in your terminal.
from tqdm import tqdm
import time
for i in tqdm(range(100)):
time.sleep(0.05) # Simulating a task
Output

Explanation: This code runs a loop 100 times and tqdm adds a progress bar to show how far along it is. Each loop pauses briefly using time.sleep(0.05) to mimic a task taking time. As it runs, the progress bar updates in real time, giving you a clear visual of the task’s progress.
Using rich.progress
rich is a modern Python library for beautiful terminal output, including progress bars with colors and animations. It makes your console output visually appealing.
from rich.progress import Progress
import time
with Progress() as p:
t = p.add_task("Processing...", total=100)
while not p.finished:
p.update(t, advance=1)
time.sleep(0.05)
Output

Explanation: rich.progress create a colorful progress bar for a 100-step task. It updates the bar step-by-step with a short pause time.sleep(0.05) to simulate work, providing a smooth, real-time view of progress until completion.
Using alive_progress
alive_progress adds a fun twist to progress bars by showing animated bars and spinners. It's very user-friendly and adds a bit of life to long-running tasks.
from alive_progress import alive_bar
import time
with alive_bar(100) as bar:
for i in range(100):
time.sleep(0.05)
bar()
Output

Explanation: alive_progress show an animated progress bar. It runs a 100-step loop with a brief pause time.sleep(0.05) to simulate work, updating the progress bar after each iteration.
Using progressbar2
progressbar2 is a more traditional progress bar library that gives you a classic loading bar feel. It's a solid choice and has been around for a while.
import progressbar
import time
b = progressbar.ProgressBar(maxval=100)
b.start()
for i in range(100):
time.sleep(0.05)
b.update(i + 1)
b.finish()
Output

Explanation: progressbar2 library display a terminal progress bar for a 100-step task. It starts the bar, simulates work with a short pause in each loop iteration, updates the progress with b.update(i + 1) and finishes cleanly with b.finish().