There are multiple ways to implement progress bars in Python. Below are several common methods:
Example
import time
for i in range(101): # Add progress bar graphics and percentage
bar = '[' + '=' * (i // 2) + ' ' * (50 - i // 2) + ']'
print(f"\r{bar} {i:3}%", end='', flush=True)
time.sleep(0.05)
print()
for i in range(101): # Add progress bar graphics and percentage
bar = '[' + '=' * (i // 2) + ' ' * (50 - i // 2) + ']'
print(f"\r{bar} {i:3}%", end='', flush=True)
time.sleep(0.05)
print()
Example
import sys
import time
def progress_bar(current, total, bar_length=50):
percent = float(current) / total
arrow = '=' * int(round(percent * bar_length) - 1) + '>'
spaces = ' ' * (bar_length - len(arrow))
sys.stdout.write(f"\r`Progress: [{arrow + spaces}] {int(round(percent * 100))}%"`)
sys.stdout.flush()
for i in range(101):
time.sleep(0.1)
progress_bar(i, 100)
print() # Line break
import time
def progress_bar(current, total, bar_length=50):
percent = float(current) / total
arrow = '=' * int(round(percent * bar_length) - 1) + '>'
spaces = ' ' * (bar_length - len(arrow))
sys.stdout.write(f"\r`Progress: [{arrow + spaces}] {int(round(percent * 100))}%"`)
sys.stdout.flush()
for i in range(101):
time.sleep(0.1)
progress_bar(i, 100)
print() # Line break
Progress with different color changes:
Example
import time
import sys
def progress_bar(current, total, bar_length=50):
"""
Display progress bar
:param current: current progress
:param total: total progress
:param bar_length: progress bar length
"""
percent = current / total
arrow = '=' * int(round(percent * bar_length) - 1) + '>'
spaces = ' ' * (bar_length - len(arrow))
# Add color (optional)
color_code = 32 # Green
if percent > 0.7:
color_code = 33 # Yellow
if percent > 0.9:
color_code = 31 # Red
# Format output
sys.stdout.write(f"\r\0`33[{color_code}mProgress: [{arrow + spaces}] {current:3d}/{total} ({percent:.0%})`\033[0m")
sys.stdout.flush()
# Usage example
total = 100
for i in range(total + 1):
progress_bar(i, total)
time.sleep(0.05)
print("\n`Done!"`) # Newline after completion
import sys
def progress_bar(current, total, bar_length=50):
"""
Display progress bar
:param current: current progress
:param total: total progress
:param bar_length: progress bar length
"""
percent = current / total
arrow = '=' * int(round(percent * bar_length) - 1) + '>'
spaces = ' ' * (bar_length - len(arrow))
# Add color (optional)
color_code = 32 # Green
if percent > 0.7:
color_code = 33 # Yellow
if percent > 0.9:
color_code = 31 # Red
# Format output
sys.stdout.write(f"\r\0`33[{color_code}mProgress: [{arrow + spaces}] {current:3d}/{total} ({percent:.0%})`\033[0m")
sys.stdout.flush()
# Usage example
total = 100
for i in range(total + 1):
progress_bar(i, total)
time.sleep(0.05)
print("\n`Done!"`) # Newline after completion
Using the standard library tqdm
tqdm is one of the most popular progress bar libraries. It is simple to install and easy to use.
Install the standard library with pip:
pip install tqdm
Example
from tqdm import tqdm
import time
# Basic usage
for i in tqdm(range(100)):
time.sleep(0.1) # Simulate task
# Progress bar with description
with tqdm(range(100), desc=`"Processing progress"`) as pbar:
for i in pbar:
time.sleep(0.1)
# Manual update
pbar = tqdm(total=100)
for i in range(10):
time.sleep(0.5)
pbar.update(10) # Update 10 each time
pbar.close()
import time
# Basic usage
for i in tqdm(range(100)):
time.sleep(0.1) # Simulate task
# Progress bar with description
with tqdm(range(100), desc=`"Processing progress"`) as pbar:
for i in pbar:
time.sleep(0.1)
# Manual update
pbar = tqdm(total=100)
for i in range(10):
time.sleep(0.5)
pbar.update(10) # Update 10 each time
pbar.close()