Fast, extensible progress meter for loops and iterators in Python
npx @tessl/cli install tessl/pypi-tqdm@4.67.0A fast, extensible progress meter for loops and iterators in Python. tqdm wraps any iterable to create smart progress bars with minimal overhead (60ns per iteration) and works across all environments including terminals, GUIs, and Jupyter notebooks.
pip install tqdmfrom tqdm import tqdm, trangeFor specific environments and integrations:
from tqdm.auto import tqdm, trange # Auto-detects best variant
from tqdm.notebook import tqdm, trange # Jupyter notebooks
from tqdm.asyncio import tqdm, trange # Async/await support
from tqdm.gui import tqdm, trange # Matplotlib GUI
from tqdm.rich import tqdm, trange # Rich integration
from tqdm.contrib.concurrent import thread_map, process_map # Parallel processingfrom tqdm import tqdm, trange
import time
# Wrap any iterable
for i in tqdm(range(10000)):
time.sleep(0.0001) # Simulate work
# Range shortcut
for i in trange(10000, desc="Processing"):
time.sleep(0.0001)
# Manual updates
pbar = tqdm(total=100)
for i in range(100):
time.sleep(0.01)
pbar.update(1)
pbar.close()
# Context manager
with tqdm(total=100) as pbar:
for i in range(100):
time.sleep(0.01)
pbar.update(1)
# Custom formatting
for i in tqdm(range(1000), desc="Training", unit="batch",
bar_format="{l_bar}{bar:30}{r_bar}"):
time.sleep(0.001)tqdm uses a modular architecture enabling extensive customization and multiple interfaces:
tqdm.std): Foundation progress bar with format strings, timing, and display logicThis design allows tqdm to automatically adapt to any environment while providing consistent APIs across all variants.
Essential progress bar functionality including iterator wrapping, manual updates, customizable formatting, and context manager support. Forms the foundation for all tqdm variants.
class tqdm(Comparable):
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=None, lock_args=None,
nrows=None, colour=None, delay=0, gui=False, **kwargs): ...
def update(self, n=1): ...
def close(self): ...
def set_description(self, desc=None, refresh=True): ...
def set_postfix(self, ordered_dict=None, refresh=True, **kwargs): ...
def trange(*args, **kwargs): ...Specialized progress bar implementations for different environments including automatic selection, Jupyter notebooks, async/await support, and GUI interfaces.
# Auto-detection
from tqdm.auto import tqdm, trange
# Jupyter notebooks
from tqdm.notebook import tqdm as notebook_tqdm, trange as notebook_trange
# Async support
from tqdm.asyncio import tqdm as async_tqdm, trange as async_trange
# GUI variants
from tqdm.gui import tqdm as gui_tqdm
from tqdm.rich import tqdm as rich_tqdmIntegration modules for popular Python frameworks including Keras callbacks, Dask computation tracking, and pandas DataFrame operations.
# Keras integration
from tqdm.keras import TqdmCallback
# Dask integration
from tqdm.dask import TqdmCallback
# Pandas integration
tqdm.pandas() # Enables .progress_apply() methodsUtilities for parallel processing with progress tracking including thread-based and process-based mapping functions with automatic progress bars.
from tqdm.contrib.concurrent import thread_map, process_map
def thread_map(fn, *iterables, max_workers=None, chunksize=1, **tqdm_kwargs): ...
def process_map(fn, *iterables, max_workers=None, chunksize=1, **tqdm_kwargs): ...Additional utilities including iterator helpers, logging integration, external service notifications, and low-level I/O wrappers for advanced use cases.
from tqdm.contrib import tenumerate, tzip, tmap
from tqdm.contrib.logging import tqdm_logging_redirect
from tqdm.utils import FormatReplace, CallbackIOWrapper# Custom description and units
for i in tqdm(range(1000), desc="Processing items", unit="item"):
pass
# Custom bar format
for i in tqdm(range(1000),
bar_format="{desc}: {percentage:3.0f}%|{bar}| {n_fmt}/{total_fmt}"):
pass
# Dynamic postfix updates
pbar = tqdm(range(1000))
for i in pbar:
pbar.set_postfix({"loss": f"{i*0.001:.3f}", "accuracy": f"{95+i*0.01:.1f}%"})from tqdm import tqdm
import time
# Sequential bars
for epoch in trange(5, desc="Epochs"):
for batch in trange(100, desc="Batches", leave=False):
time.sleep(0.01)
# Nested bars with position
outer = tqdm(range(5), desc="Outer", position=0)
for i in outer:
inner = tqdm(range(100), desc=f"Inner {i}", position=1, leave=False)
for j in inner:
time.sleep(0.001)
inner.close()
outer.close()from tqdm import TqdmWarning, TqdmKeyError
import warnings
# Handle tqdm-specific warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore", TqdmWarning)
# Your tqdm code here
# Exception handling
try:
pbar = tqdm(total=100)
# Progress bar operations
except TqdmKeyError as e:
print(f"Configuration error: {e}")
finally:
pbar.close()