Python Command Line Interface Tools for colored output, progress bars, text formatting, and argument handling
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Progress bars, dots, and mill/spinner indicators for long-running operations. Supports both iterator wrapping and manual progress updates with customizable appearance, automatic terminal width detection, and ETA calculations.
Wrap iterables with a progress bar that automatically updates as items are processed.
def bar(it, label='', width=32, hide=None, empty_char=' ', filled_char='#', expected_size=None, every=1):
"""
Progress iterator that wraps iterables with a progress bar.
Parameters:
- it: iterable, items to iterate over
- label: str, label to display before progress bar (default: '')
- width: int, width of progress bar in characters (default: 32)
- hide: bool, force hide progress bar (default: None, auto-detect TTY)
- empty_char: str, character for empty portion of bar (default: ' ')
- filled_char: str, character for filled portion of bar (default: '#')
- expected_size: int, override length detection (default: None)
- every: int, update frequency - update every N items (default: 1)
Yields:
items from the wrapped iterable
"""Progress bar class for manual control over progress updates and display.
class Bar:
def __init__(self, label='', width=32, hide=None, empty_char=' ', filled_char='#', expected_size=None, every=1):
"""
Manual progress bar with customizable appearance and timing.
Parameters:
- label: str, label to display before progress bar (default: '')
- width: int, width of progress bar in characters (default: 32)
- hide: bool, force hide progress bar (default: None, auto-detect TTY)
- empty_char: str, character for empty portion of bar (default: ' ')
- filled_char: str, character for filled portion of bar (default: '#')
- expected_size: int, total number of items expected (default: None)
- every: int, update frequency - update every N calls (default: 1)
"""
def show(self, progress, count=None):
"""
Display progress bar at given progress level.
Parameters:
- progress: int, current progress value
- count: int, optional override for expected_size
"""
def done(self):
"""
Complete the progress bar and show final state with elapsed time.
Automatically called when using Bar as context manager.
"""
def __enter__(self):
"""Context manager entry."""
def __exit__(self, exc_type, exc_val, exc_tb):
"""Context manager exit - calls done() automatically."""Simple progress indicator that prints dots for each processed item.
def dots(it, label='', hide=None, every=1):
"""
Progress iterator that prints a dot for each item being iterated.
Parameters:
- it: iterable, items to iterate over
- label: str, label to display before dots (default: '')
- hide: bool, force hide dots (default: None, auto-detect TTY)
- every: int, print dot every N items (default: 1)
Yields:
items from the wrapped iterable
"""Rotating character spinner for indicating ongoing progress without known total.
def mill(it, label='', hide=None, expected_size=None, every=1):
"""
Progress iterator that prints a mill/spinner while iterating.
Parameters:
- it: iterable, items to iterate over
- label: str, label to display before spinner (default: '')
- hide: bool, force hide spinner (default: None, auto-detect TTY)
- expected_size: int, override length detection (default: None)
- every: int, update spinner every N items (default: 1)
Yields:
items from the wrapped iterable
"""from clint.textui.progress import bar, dots, mill, Bar
import time
# Basic progress bar with iterator
items = range(100)
for item in bar(items, label='Processing: '):
time.sleep(0.1) # Simulate work
# Process item
# Custom progress bar appearance
for item in bar(items, label='Custom: ', width=50,
filled_char='█', empty_char='░'):
time.sleep(0.05)
# Process item
# Manual progress bar
with Bar(label='Manual Progress: ', expected_size=100) as progress_bar:
for i in range(100):
# Do some work
time.sleep(0.05)
progress_bar.show(i + 1)
# Dots progress indicator
large_dataset = range(1000)
for item in dots(large_dataset, label='Loading data', every=10):
# Process item (prints dot every 10 items)
pass
# Mill/spinner for unknown progress
unknown_items = some_generator()
for item in mill(unknown_items, label='Processing: '):
# Process item with spinner
passfrom clint.textui.progress import Bar, bar
import requests
# Download progress with actual size
def download_file(url, filename):
response = requests.get(url, stream=True)
total_size = int(response.headers.get('Content-Length', 0))
with open(filename, 'wb') as f:
with Bar(label=f'Downloading {filename}: ',
expected_size=total_size) as progress_bar:
downloaded = 0
for chunk in response.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
downloaded += len(chunk)
progress_bar.show(downloaded)
# Nested progress bars for complex operations
def process_files(file_list):
for file_path in bar(file_list, label='Files: '):
with open(file_path, 'r') as f:
lines = f.readlines()
# Process each line with its own progress bar
processed_lines = []
for line in bar(lines, label=f' {file_path}: ', width=20):
# Process line
processed_line = line.strip().upper()
processed_lines.append(processed_line)
time.sleep(0.01)
# Custom update frequency for performance
huge_dataset = range(1000000)
for item in bar(huge_dataset, label='Big data: ', every=1000):
# Only update progress every 1000 items for better performance
pass
# Progress bar with error handling
def safe_processing(items):
progress_bar = Bar(label='Safe processing: ', expected_size=len(items))
try:
for i, item in enumerate(items):
try:
# Process item that might fail
result = risky_operation(item)
progress_bar.show(i + 1)
except Exception as e:
# Handle individual item errors
print(f"Error processing item {i}: {e}")
continue
finally:
progress_bar.done()
# Conditional progress display
def process_with_conditional_progress(items, show_progress=True):
if show_progress:
iterator = bar(items, label='Processing: ')
else:
iterator = items
for item in iterator:
# Process item
passhide=False to force display or hide=True to force hidewidth parameter# Force progress display even when piped
for item in bar(items, hide=False):
pass
# High-performance mode with reduced updates
for item in bar(huge_list, every=100, label='Fast: '):
pass
# Custom appearance
for item in bar(items, filled_char='●', empty_char='○', width=60):
passInstall with Tessl CLI
npx tessl i tessl/pypi-clint