Print while a bar is running¶
A plain print() call while a bar is redrawing collides with it on
the same line – the two fight over the same carriage return, and
neither comes out readable.
"""Let `print()` calls show up above the bar instead of corrupting it.
Pass `redirect_stdout=True` and call `print()` as normal from inside the
loop -- the bar holds the line back and flushes it above the moving bar on
the next redraw, rather than the two colliding over the same carriage
return.
"""
import time
import progressbar
FILES = ['report.csv', 'errors.log', 'summary.json']
STEPS_PER_FILE = 8
def main() -> None:
with progressbar.ProgressBar(
max_value=len(FILES) * STEPS_PER_FILE,
redirect_stdout=True,
) as bar:
for index, filename in enumerate(FILES):
print(f'Processing {filename}')
for step in range(STEPS_PER_FILE):
bar.update(index * STEPS_PER_FILE + step + 1)
time.sleep(0.01)
if __name__ == '__main__':
main()
Pass redirect_stdout=True and call print() normally from inside
the loop. The bar holds the line back and flushes it above the moving
bar on its next redraw. This only catches writes to sys.stdout:
print() and anything else writing there directly. To route the
stdlib logging module the same way, see
Send logging output above a running bar.