pip install alive-progress
1. Simple Progress Bar
from alive_progress import alive_bar
import time
items = range(100)
with alive_bar(len(items)) as bar:
for item in items:
time.sleep(0.05)
bar()
|████████████████████████████████████████| 100/100 [100%] in 5.0s (19.83/s)
2. Progress Bar with Custom Text
from alive_progress import alive_bar
import time
tasks = ['task1', 'task2', 'task3', 'task4']
with alive_bar(len(tasks), title='Processing Tasks') as bar:
for task in tasks:
time.sleep(0.5)
bar.text = f'Working on {task}'
bar()
Processing Tasks |████████████████████████████████████████| 4/4 [100%] in 2.0s (2.00/s)
3. Nested Progress Bars
from alive_progress import alive_bar
import time
outer_items = range(3)
inner_items = range(5)
with alive_bar(len(outer_items) * len(inner_items), title='Processing') as bar:
for _ in outer_items:
for _ in inner_items:
time.sleep(0.1) # Simulate work
bar()
Processing |████████████████████████████████████████| 15/15 [100%] in 1.5s (9.94/s)
4. Download Simulation with Progress Bar
from alive_progress import alive_bar
import time
file_size = 1024
chunk_size = 64
with alive_bar(file_size // chunk_size, title='Downloading') as bar:
for _ in range(0, file_size, chunk_size):
time.sleep(0.1)
bar()
Downloading |████████████████████████████████████████| 16/16 [100%] in 1.6s (9.83/s)
5. Progress Bar with Percentage and ETA
from alive_progress import alive_bar
import time
total_items = 50
with alive_bar(total_items, title='Processing',
spinner='dots_waves', bar='smooth') as bar:
for _ in range(total_items):
time.sleep(0.1)
bar()
Processing |████████████████████████████████████████| 50/50 [100%] in 5.0s (9.95/s)
0 Comments:
Post a Comment