Implementing Dynamic Progress Bars in Python: Using tqdm, Manual Methods, and Various Libraries
This article explains how to create dynamic command‑line progress bars in Python, covering the easy‑to‑use tqdm library, a manual implementation with sys and time, and several alternative libraries such as colorama, rich, alive‑progress, click, progressbar2, and pyfiglet, each with code examples and installation instructions.
Creating a dynamic progress bar in the command line is a common requirement for long‑running Python tasks. You can use the convenient tqdm library or build a simple one from scratch with the standard sys and time modules.
Using the tqdm library
Install tqdm with pip install tqdm and create a progress bar as follows:
from tqdm import tqdm
import time
total_iterations = 100
for i in tqdm(range(total_iterations), desc="Progress", unit="it"):
time.sleep(0.1)Manual implementation
Define an update_progress function that calculates the percentage, builds an arrow and spaces, and writes the bar to sys.stdout using a carriage return. Example code:
import sys
import time
def update_progress(progress, total, bar_length=40):
percent = float(progress) / total
arrow = '=' * int(round(percent * bar_length) - 1) + '>'
spaces = ' ' * (bar_length - len(arrow))
sys.stdout.write(f"\rProgress: [{arrow}{spaces}] {int(percent * 100)}%")
sys.stdout.flush()
total_iterations = 100
for i in range(total_iterations + 1):
update_progress(i, total_iterations)
time.sleep(0.1)
print("\nTask completed!")Custom colored progress bar with colorama
Install colorama ( pip install colorama ) and modify the update function to add ANSI colors:
import sys
import time
from colorama import Fore, Style, init
init(autoreset=True)
def update_progress(progress, total, bar_length=40):
percent = float(progress) / total
arrow = Fore.GREEN + '=' * int(round(percent * bar_length) - 1) + '>' + Style.RESET_ALL
spaces = ' ' * (bar_length - len(arrow) + 1)
sys.stdout.write(f"\r{Fore.CYAN}Progress: [{arrow}{spaces}] {int(percent * 100)}%{Style.RESET_ALL}")
sys.stdout.flush()
total_iterations = 100
for i in range(total_iterations + 1):
update_progress(i, total_iterations)
time.sleep(0.1)
print(f"\n{Fore.GREEN}Task completed!{Style.RESET_ALL}")tqdm with color
tqdm supports colored bars via the colour argument:
from tqdm import tqdm
import time
total_iterations = 100
for i in tqdm(range(total_iterations), desc="Progress", unit="it", ncols=100, colour="green"):
time.sleep(0.1)Using the rich library
Install rich ( pip install rich ) and display a progress bar with track :
from rich.progress import track
import time
total_iterations = 100
for i in track(range(total_iterations), description="Processing..."):
time.sleep(0.1)Using alive‑progress
Install alive‑progress ( pip install alive-progress ) and use alive_bar :
from alive_progress import alive_bar
import time
total_iterations = 100
with alive_bar(total_iterations, title='Processing', bar='blocks') as bar:
for i in range(total_iterations):
time.sleep(0.1)
bar()Using click
Install click ( pip install click ) and create a progress bar:
import click
import time
total_iterations = 100
with click.progressbar(range(total_iterations), label='Processing') as bar:
for i in bar:
time.sleep(0.1)Using progressbar2
Install progressbar2 ( pip install progressbar2 ) and configure widgets:
import progressbar
import time
total_iterations = 100
widgets = [progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()]
bar = progressbar.ProgressBar(maxval=total_iterations, widgets=widgets).start()
for i in range(total_iterations):
time.sleep(0.1)
bar.update(i + 1)
bar.finish()Combining colorama with custom ASCII art
Install colorama and create a colored ASCII‑art bar similar to the manual implementation, using Fore.GREEN and Style.RESET_ALL for colors.
import sys
import time
from colorama import Fore, Style, init
init(autoreset=True)
def update_progress(progress, total, bar_length=40):
percent = float(progress) / total
arrow = Fore.GREEN + '█' * int(round(percent * bar_length) - 1) + '>' + Style.RESET_ALL
spaces = ' ' * (bar_length - len(arrow) + 1)
sys.stdout.write(f"\r{Fore.CYAN}Progress: [{arrow}{spaces}] {int(percent * 100)}%{Style.RESET_ALL}")
sys.stdout.flush()
total_iterations = 100
for i in range(total_iterations + 1):
update_progress(i, total_iterations)
time.sleep(0.1)
print(f"\n{Fore.GREEN}Task completed!{Style.RESET_ALL}")Using pyfiglet with tqdm
Install pyfiglet ( pip install pyfiglet ) to print ASCII art, then run a colored tqdm bar:
from tqdm import tqdm
import time
import pyfiglet
print(pyfiglet.figlet_format("Processing"))
total_iterations = 100
for i in tqdm(range(total_iterations), desc="Progress", unit="it", ncols=100, colour="green"):
time.sleep(0.1)
print("Task completed!")These examples demonstrate multiple ways to add informative, colorful, and customizable progress indicators to Python command‑line applications.
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.