Fast, extensible progress meter for loops and iterators in Python
Essential progress bar functionality that forms the foundation of tqdm. The core tqdm class provides iterator wrapping, manual updates, extensive customization options, and thread-safe operations.
The core tqdm class that wraps iterables and provides progress tracking with extensive customization options for display formatting, update intervals, and output control.
class tqdm(Comparable):
"""
Decorate an iterable object, returning an iterator which acts exactly
like the original iterable, but prints a dynamically updating
progressbar every time a value is requested.
Parameters:
- iterable: Iterable to decorate with progress meter
- desc: Prefix for progress meter description
- total: The number of expected iterations
- leave: If True, keeps all traces of the progressbar upon termination
- file: Output stream (default: sys.stderr)
- ncols: The width of the entire output message
- mininterval: Minimum progress update interval in seconds (default: 0.1)
- maxinterval: Maximum progress update interval in seconds (default: 10.0)
- miniters: Minimum progress display update interval in iterations
- ascii: If True, use ASCII characters for progress bar
- disable: Whether to disable progress bar entirely
- unit: String defining unit of each iteration (default: 'it')
- unit_scale: If True, number of iterations will be scaled/formatted
- dynamic_ncols: If True, constantly alters ncols to environment
- smoothing: Exponential moving average smoothing factor (0-1)
- bar_format: Custom format string for progress bar
- initial: Initial counter value (default: 0)
- position: Specify line offset to print progress bar
- postfix: Specify additional stats as dict or str
- unit_divisor: Divisor for unit scaling (default: 1000)
- write_bytes: If True and unit_scale, write data size in bytes
- lock_args: Passed to refresh/write lock
- nrows: Screen height for positioning multiple bars
- colour: Bar colour (e.g. 'green', '#00ff00')
- delay: Don't display until at least delay seconds have elapsed
- gui: Internal GUI parameter
"""
def __init__(self, iterable=None, desc=None, total=None, leave=True,
file=None, ncols=None, mininterval=0.1, maxinterval=10.0,
miniters=None, ascii=None, disable=False, unit='it',
unit_scale=False, dynamic_ncols=False, smoothing=0.3,
bar_format=None, initial=0, position=None, postfix=None,
unit_divisor=1000, write_bytes=False, lock_args=None,
nrows=None, colour=None, delay=0.0, gui=False, **kwargs): ...Core iterator methods enabling tqdm to wrap any iterable while providing progress tracking functionality.
def __iter__(self):
"""Backward-compatibility to use: for x in tqdm(iterable)"""
def __next__(self):
"""Implementation of iterator protocol"""
def __enter__(self):
"""Context manager entry"""
def __exit__(self, exc_type, exc_value, traceback):
"""Context manager exit with cleanup"""Methods for manually controlling progress bar updates, useful when not using iterator interface or when processing doesn't follow simple iteration patterns.
def update(self, n=1):
"""
Manually update progress bar by n steps.
Parameters:
- n: Number of steps to increment (default: 1)
Returns:
True if progress display was updated, False otherwise
"""
def refresh(self, nolock=False):
"""Force refresh display, potentially interrupting output"""
def reset(self, total=None):
"""
Resets to 0 iterations for repeated use.
Parameters:
- total: New total iterations (defaults to current total)
"""
def close(self):
"""Cleanup and close progress bar, removing it if leave=False"""
def clear(self, nolock=False):
"""Clear current progress bar display"""
def unpause(self):
"""Restart tqdm timer from last print time"""Methods for dynamically updating progress bar appearance and information display during execution.
def set_description(self, desc=None, refresh=True):
"""
Set/update description prefix.
Parameters:
- desc: New description string
- refresh: Whether to refresh display immediately
"""
def set_description_str(self, desc=None, refresh=True):
"""Set/update description without parsing"""
def set_postfix(self, ordered_dict=None, refresh=True, **kwargs):
"""
Set/update postfix (additional stats) displayed after progress bar.
Parameters:
- ordered_dict: Dict of stats to display
- refresh: Whether to refresh display immediately
- **kwargs: Additional stats as keyword arguments
"""
def set_postfix_str(self, s='', refresh=True):
"""Set postfix string directly without formatting"""
def moveto(self, n):
"""Move cursor to line n for multi-bar displays"""Methods to retrieve current progress state and formatting information programmatically.
def format_dict(self):
"""
Return current status as a formatted dictionary.
Returns:
Dict containing: n, total, elapsed, ncols, nrows, prefix, ascii, unit,
unit_scale, rate, bar_format, postfix, unit_divisor, initial, colour
"""
def display(self, msg=None, pos=None):
"""
Use print() to display message, temporarily clearing progress bar.
Parameters:
- msg: Message to display (defaults to current progress)
- pos: Position for display (defaults to current position)
"""
@property
def format_meter(self):
"""Current progress bar string representation"""Thread-safe operations for concurrent environments, enabling safe output and lock management across multiple threads.
@classmethod
def write(cls, s, file=None, end="\n", nolock=False):
"""
Print message via tqdm, clearing all progress bars temporarily.
Parameters:
- s: String message to print
- file: Output stream (defaults to sys.stderr)
- end: String appended after last value (default: newline)
- nolock: If True, don't acquire/release lock
"""
@classmethod
def external_write_mode(cls, file=None, nolock=False):
"""
Context manager for temporarily disabling progress bars for external writes.
Parameters:
- file: Output stream to manage
- nolock: If True, don't acquire lock
Yields:
Context where external writes won't interfere with progress bars
"""
@classmethod
def set_lock(cls, lock):
"""Set lock for thread-safe operations"""
@classmethod
def get_lock(cls):
"""Get current lock object"""High-level integration methods for popular libraries and common use cases.
@classmethod
def pandas(cls, **tqdm_kwargs):
"""
Register tqdm instance for pandas operations.
Enables .progress_apply() and similar methods on DataFrames.
Parameters:
- **tqdm_kwargs: Arguments passed to tqdm constructor
Returns:
Deprecated TqdmPandas instance (for backward compatibility)
"""
@classmethod
def wrapattr(cls, stream, method, total=None, bytes=True, **tqdm_kwargs):
"""
Stream attribute wrapper with progress bar.
Parameters:
- stream: IO stream object
- method: Attribute name to wrap (e.g., 'read', 'write')
- total: Total expected bytes/calls
- bytes: If True, update by bytes; if False, by method calls
- **tqdm_kwargs: Additional tqdm arguments
Returns:
Wrapped stream object
"""Utility methods for consistent number and time formatting across tqdm displays.
@staticmethod
def format_sizeof(num, suffix='', divisor=1000):
"""
Format numbers with metric prefixes (k, M, G, T, P, E, Z, Y).
Parameters:
- num: Number to format
- suffix: Unit suffix (e.g., 'B' for bytes)
- divisor: Base for scaling (1000 or 1024)
Returns:
Formatted string with appropriate prefix
"""
@staticmethod
def format_interval(t):
"""
Format time intervals as human-readable strings.
Parameters:
- t: Time interval in seconds
Returns:
Formatted time string (e.g., '1:23:45', '2.5s')
"""
@staticmethod
def format_num(n):
"""
Intelligent number formatting with appropriate precision.
Parameters:
- n: Number to format
Returns:
Formatted number string
"""
@staticmethod
def status_printer(file):
"""
Create status printer function for custom output streams.
Parameters:
- file: Output stream
Returns:
Function(s) -> prints status to file
"""Convenience function combining range() with tqdm for simple counting loops with progress bars.
def trange(*args, **kwargs):
"""
Shortcut for tqdm(range(*args), **kwargs).
Parameters:
- *args: Arguments passed to range()
- **kwargs: Arguments passed to tqdm constructor
Returns:
tqdm instance wrapping range iterator
"""from tqdm import tqdm
import time
# Wrap any iterable
data = range(1000)
for item in tqdm(data, desc="Processing"):
time.sleep(0.001) # Simulate work
# Direct iteration with custom formatting
for i in tqdm(range(1000), unit="items", unit_scale=True):
passfrom tqdm import tqdm
import time
# Manual updates with known total
pbar = tqdm(total=100, desc="Manual Progress")
for i in range(100):
# Do some work
time.sleep(0.01)
pbar.update(1)
pbar.close()
# Context manager ensures cleanup
with tqdm(total=1000, desc="Batch Processing") as pbar:
batch_size = 50
for batch_start in range(0, 1000, batch_size):
# Process batch
time.sleep(0.1)
pbar.update(batch_size)from tqdm import tqdm
import time
import random
# Dynamic description and postfix
pbar = tqdm(range(100), desc="Training")
for epoch in pbar:
# Simulate training metrics
loss = random.uniform(0.1, 1.0)
accuracy = random.uniform(0.8, 0.99)
# Update description and postfix
pbar.set_description(f"Epoch {epoch}")
pbar.set_postfix({
'loss': f'{loss:.3f}',
'acc': f'{accuracy:.2%}'
})
time.sleep(0.05)from tqdm import tqdm
import threading
import time
def worker(worker_id):
for i in tqdm(range(100), desc=f"Worker {worker_id}", position=worker_id):
time.sleep(0.01)
# Thread-safe printing
if i % 20 == 0:
tqdm.write(f"Worker {worker_id}: checkpoint at {i}")
# Create multiple worker threads
threads = []
for i in range(3):
t = threading.Thread(target=worker, args=(i,))
threads.append(t)
t.start()
for t in threads:
t.join()from tqdm import tqdm
import time
# Custom bar format
custom_format = "{desc}: {percentage:3.0f}%|{bar:50}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}]"
for i in tqdm(range(1000), desc="Custom Format", bar_format=custom_format):
time.sleep(0.001)
# Minimal format
minimal_format = "{desc} {percentage:3.0f}% {bar}"
for i in tqdm(range(500), desc="Minimal", bar_format=minimal_format):
time.sleep(0.002)class TqdmTypeError(TypeError):
"""Type-related errors in tqdm configuration or usage"""class TqdmKeyError(KeyError):
"""Key-related errors in tqdm parameter specification"""class TqdmWarning(Warning):
"""
Base class for all tqdm warnings.
Used for non-external-code-breaking errors, such as garbled printing.
"""
def __init__(self, msg, fp_write=None, *a, **k): ...class TqdmExperimentalWarning(TqdmWarning, FutureWarning):
"""Warnings for experimental features that may change in future versions"""class TqdmDeprecationWarning(TqdmWarning, DeprecationWarning):
"""Warnings for deprecated features scheduled for removal"""class TqdmMonitorWarning(TqdmWarning, RuntimeWarning):
"""Runtime warnings from the TMonitor monitoring thread"""Install with Tessl CLI
npx tessl i tessl/pypi-tqdm