ProgressBar#
- 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.
- __init__(iterable=None, *, total=None, comment=None, width=80, alpha=0.2, update_interval=0.5, shared_counter=None)#
- Parameters:
iterable (Iterable[T] | None)
total (int | None)
comment (str | None)
width (int)
alpha (float)
update_interval (float)
shared_counter (Any)
- Return type:
None
Methods
__init__
([iterable, total, comment, width, ...])refresh
()Force a redraw (useful in multiprocessing main loop).
set_comment
(text)Set/clear the comment displayed on the right side of the bar.
tick
([n])Increment progress by n and maybe redraw (throttled by update_interval).
Attributes
- BLUE = '\x1b[0;34m'#
- CYAN = '\x1b[1;35m'#
- GRAY = '\x1b[0;37m'#
- RESET = '\x1b[0m'#
- total: int#
- comment: str | None#
- width: int#
- alpha: float#
- update_interval: float#
- index: int#
- start_time: float#
- smoothed_speed: float | None#
- last_bar_size: int#
- last_update_time: float#
- 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