Skip to content

Latest commit

 

History

History
212 lines (146 loc) · 6.89 KB

File metadata and controls

212 lines (146 loc) · 6.89 KB

progressbar2

A typed terminal progress bar library for Python. It handles custom widgets, clean output around prints and logs, multiple concurrent bars, unknown-length progress, and pipe-friendly CLI usage.

python-progressbar test status coverage status

Install

pip install progressbar2

Quick start

import time
import progressbar

for item in progressbar.progressbar(range(100), desc='Loading'):
    time.sleep(0.02)

Try it in your browser

Every example in the documentation runs live in the page. Press Run on any code block, no install required.

Progress with clean logs

progressbar2 showing clean progress output with logs

"""A build log printing above a progress bar without corrupting it."""

from __future__ import annotations

import time

import progressbar

STEPS = 24


def main() -> None:
    with progressbar.ProgressBar(
        max_value=STEPS,
        prefix='Build ',
        redirect_stdout=True,
    ) as bar:
        for step in range(STEPS):
            if step in {8, 16}:
                print(f'log: completed step {step}')
            bar.update(step + 1)
            # Longer than the bar's 0.05s update gate, so every step
            # lands as a visible redraw.
            time.sleep(0.1)


if __name__ == '__main__':
    main()

Multiple bars

multiple progress bars updating together

"""Two named bars progressing at different rates in one terminal."""

from __future__ import annotations

import sys
import time

import progressbar

STEPS = 24


def main() -> None:
    with progressbar.MultiBar(fd=sys.stdout) as multibar:
        build = multibar['build']
        test = multibar['test']
        build.max_value = STEPS
        test.max_value = STEPS
        for step in range(STEPS):
            build.update(step + 1)
            test.update(min(STEPS, max(0, round((step - 3) * 1.2))))
            # Longer than the bars' 0.05s update gate, so every step
            # lands as a visible redraw.
            time.sleep(0.1)

        # Reaching max_value doesn't finish a bar -- only finish() does.
        # A MultiBar waits for every bar to report finished() before its
        # context manager can exit, so without these calls the block
        # above would hang forever on exit.
        build.finish()
        test.finish()


if __name__ == '__main__':
    main()

Parallel execution

Run a function over a batch of items -- threads, processes, or asyncio -- with a live progress bar, in one call:

import progressbar

results = progressbar.map(fetch, urls, workers=8)          # threads
results = progressbar.map(crunch, files, pool='process')   # processes
results = await progressbar.amap(fetch, urls)              # asyncio

# A progress-bar'd xargs -P:
progressbar.run('gzip -k {}', files, workers=4)

# An overall bar plus one bar per in-flight task:
progressbar.map(crunch, files, workers=4, bar='multi')

Results come back in input order; imap/imap_unordered stream them instead, gather is a drop-in asyncio.gather with a bar, and the bar keeps animating even while long tasks are running. See the parallel execution guide for errors, timeouts, pools, and the decorator form.

Unknown length and animated bars

unknown length progress with an animated marker

"""A bar for work whose total is not known up front."""

from __future__ import annotations

import time

import progressbar


def main() -> None:
    with progressbar.ProgressBar(
        max_value=progressbar.UnknownLength,
    ) as bar:
        for value in range(0, 120, 10):
            bar.update(value)
            # Longer than the bar's 0.05s update gate, so every step
            # lands as a visible redraw.
            time.sleep(0.1)


if __name__ == '__main__':
    main()

CLI usage

progressbar --progress --timer --eta --rate --bytes input.bin -o output.bin

Known terminal caveats

  • JetBrains IDEs need "Enable terminal in output console" for advanced terminal behavior such as MultiBar.
  • IDLE does not support terminal progress bars.
  • Jupyter buffers stdout; call sys.stdout.flush() when output appears late.

Project history

progressbar2 is based on the old Python progressbar package that was published on the now defunct Google Code. Since that project was completely abandoned by its developer and the developer did not respond to email, I decided to fork the package.

This package is still backwards compatible with the original progressbar package so you can use it as a drop-in replacement for existing projects.

Links