Adding a progress bar in Python is a great way to enhance user experience for long-running tasks. Whether you’re iterating over a loop, downloading files, or waiting for a time-consuming process, progress indicators can be easily implemented using libraries or manual approaches.
The tqdm library is the most popular and simple way to display a progress bar.
Example:
from tqdm import tqdm import time # Simulating a long-running loop for i in tqdm(range(100)): time.sleep(0.05) # Simulated workload
A real-time progress bar with estimated time and percentage completes in the terminal.
For more control or if you don’t want external dependencies:
import sys
import time
def print_progress_bar(iteration, total, length=50):
percent = ("{0:.1f}").format(100 * (iteration / float(total)))
filled_length = int(length * iteration // total)
bar = '█' * filled_length + '-' * (length - filled_length)
sys.stdout.write(f'\rProgress: |{bar}| {percent}% Complete')
sys.stdout.flush()
# Simulate a task
total = 100
for i in range(total):
time.sleep(0.05)
print_progress_bar(i + 1, total)
Work with our skilled Python developers to accelerate your project and boost its performance.
Hire Python Developers