Wrap an iterable without managing the bar yourself¶
Most loops don’t need explicit start()/update()/finish()
calls – they just need the bar to track a for loop over something
with a known length.
"""Wrap any iterable directly, without managing start/update/finish yourself.
`progressbar.progressbar(iterable)` is the shortcut for a `for` loop over a
fresh bar. Call an existing `ProgressBar` instance the same way --
`bar(iterable)` -- when you already built one for its own widgets or
prefix and want to reuse it as a wrapper.
"""
import time
import progressbar
STEPS = 24
def main() -> None:
for _ in progressbar.progressbar(range(STEPS)):
time.sleep(0.005)
bar = progressbar.ProgressBar(prefix='Second pass: ')
for _ in bar(range(STEPS)):
time.sleep(0.005)
if __name__ == '__main__':
main()
progressbar.progressbar(iterable) wraps any iterable and returns an
iterator that updates a fresh bar on every step, sized from
len(iterable) when available. If you already built a ProgressBar
instance for its own widgets, prefix, or other configuration, call it
directly as bar(iterable) instead of building a second bar just to
iterate – it wraps the same way, reusing the instance you already
configured.