Print one line per update instead of overwriting¶
ProgressBar normally repaints one line in place, overwriting the
previous redraw – the right behavior for an interactive terminal, but
the wrong one once output is piped to a file, tee, or a log
collector, where overwriting means every redraw but the last is lost.
"""What the bar looks like once it detects stdout isn't a terminal.
`ProgressBar` normally checks whether its output stream is a terminal and,
if not, switches from overwriting one line to printing a new line per
update -- the shape you want once output is piped to a file, `tee`, or a
log collector, so each redraw survives instead of being lost to the next
carriage return. That auto-detection can't be demonstrated here, since this
demo is captured through something that always presents itself as a
terminal; `line_breaks=True` forces the same rendering explicitly, and is
also the parameter to reach for if you want that behaviour even when
stdout genuinely is a terminal -- a build log, say, where every line should
stay on screen.
"""
import time
import progressbar
STEPS = 12
def main() -> None:
with progressbar.ProgressBar(max_value=STEPS, line_breaks=True) as bar:
for step in range(STEPS):
bar.update(step + 1)
time.sleep(0.05)
if __name__ == '__main__':
main()
Pass line_breaks=True to print a full new line per update instead of
overwriting. Left to auto-detect (the default, line_breaks=None),
the bar checks whether its output stream is a terminal and switches to
one line per update on its own once it isn’t. The demo above passes the
explicit override because it records inside a terminal. The explicit
line_breaks=True is also worth passing when stdout genuinely is a
terminal but every line should stay on screen, such as a build log.