rich/examples/tqdm_adapter_basic.py
K2 adcb5c748c Add tqdm compatibility shim with Rich progress backend
Implements a runtime adapter that replaces tqdm imports with Rich's
progress bars via install_tqdm(). The shim preserves common tqdm APIs
(iteration, len(), postfix, write, wrapattr) while routing rendering
through rich.progress.Progress.

Features:
- Opt-in monkeypatching of tqdm/trange and notebook variants
- Shared global Progress instance for performance
- Render throttling via mininterval/miniters/maxinterval
- Simplified text backend for file outputs (partial mode)
- Postfix value caching to avoid redundant formatting

Includes runnable examples demonstrating basic replacement and
aggressive reference overwriting, plus comprehensive unit tests
covering adapter installation, throttling, and postfix handling.

Updates README and contributor list.
2025-12-05 07:41:51 -05:00

22 lines
409 B
Python

"""Replace tqdm with rich.progress at runtime (basic example).
Run:
PYTHONPATH=. python examples/tqdm_adapter_basic.py
"""
from time import sleep
from rich.tqdm import install_tqdm
def main() -> None:
install_tqdm()
from tqdm import tqdm # resolved to the rich-backed shim after install
for _ in tqdm(range(10), desc="basic"):
sleep(0.05)
if __name__ == "__main__":
main()