progressbar#
Lightweight text progress bar for terminal loops, with tick() and multiprocessing support.
Works as an iterator OR as a manual progress meter via tick().
For multiprocessing, pass a multiprocessing.Value(‘i’) as shared_counter and call tick() from workers; the bar in the main process will display shared progress.
- Example (iterator):
>>> from time import sleep >>> for _ in bar(range(100), comment="processing"): ... sleep(0.01)
- Example (manual tick):
>>> pb = bar(total=100, comment="manual") >>> for _ in range(100): ... pb.tick() ... pb.refresh() # final refresh if needed
- Example (multiprocessing):
>>> import multiprocessing as mp, time >>> total = 200 >>> counter = mp.Value('i', 0) >>> def work(counter): ... for _ in range(50): ... time.sleep(0.01) ... with counter.get_lock(): ... counter.value += 1 >>> procs = [mp.Process(target=work, args=(counter,)) for _ in range(4)] >>> for p in procs: p.start() >>> pb = bar(total=total, shared_counter=counter, comment="mp") >>> while any(p.is_alive() for p in procs): ... pb.refresh() # updates display based on shared counter ... time.sleep(0.05) >>> for p in procs: p.join() >>> pb.refresh() # final refresh
Classes
|
A simple terminal progress bar. |
alias of |
- class ProgressBar(iterable=None, *, total=None, comment=None, width=80, alpha=0.2, update_interval=0.5, shared_counter=None)#
Bases:
Iterator
[T
]A simple terminal progress bar.
- Parameters:
iterable (Optional[Iterable[T]]) – Any iterable to consume. If omitted, you can drive progress via tick().
total (int) – Total number of items (required for manual / shared-counter modes).
comment (str | None) – Optional right-side comment (changes at runtime via
set_comment()
).width (int) – Total character width of the bar line.
alpha (float) – Smoothing factor for EMA speed (0..1).
update_interval (float) – Minimum seconds between screen updates.
shared_counter (Any) – Optional multiprocessing.Value(‘i’) (or duck with .value and .get_lock()). If provided, the bar will display this shared counter. You can call tick() from workers or just mutate shared_counter.value.
Notes
If iterable is None, total must be provided.
To use in multiprocessing, keep the bar in the main process and update shared_counter from workers. Call refresh() periodically in the main process to redraw.
- BLUE = '\x1b[0;34m'#
- CYAN = '\x1b[1;35m'#
- GRAY = '\x1b[0;37m'#
- RESET = '\x1b[0m'#
- set_comment(text)#
Set/clear the comment displayed on the right side of the bar.
- Parameters:
text (str | None)
- Return type:
None
- tick(n=1)#
Increment progress by n and maybe redraw (throttled by update_interval).
- Parameters:
n (int)
- Return type:
None
- refresh()#
Force a redraw (useful in multiprocessing main loop).
- Return type:
None
- bar#
alias of
ProgressBar