UnitProgress

UnitProgress shows a count against its total with a unit label.

Reach for it when the count needs a unit: “12 of 24 files”. unit sets the label and unit_scale enables 1024-based scaling for large counts.

class progressbar.widgets.UnitProgress(unit=<object object>, unit_scale=<object object>, **kwargs: Any)[source]

Bases: WidgetBase

Displays progress as a count with an optional unit and 1024 scaling.

Create a UnitProgress.

Parameters:
  • unit – Unit label. Defaults to following data[‘unit’] (the bar’s own unit=) rather than a fixed value.

  • unit_scale – Whether to IEC-scale the count. Defaults to following data[‘unit_scale’].

  • **kwargs – Forwarded to WidgetBase.__init__.

Example

UnitProgress
"""``UnitProgress`` shows a count against its total with a unit label.

Reach for it when the count needs a unit -- "12 of 24 files" -- with
optional 1024-based scaling for large counts. Compare ``SimpleProgress``
for the same idea without a unit, and ``DataSize`` for a single scaled
byte value rather than a count against a total.
"""

import time

import progressbar

STEPS = 24


def main() -> None:
    widgets = [progressbar.UnitProgress(unit='files')]
    with progressbar.ProgressBar(max_value=STEPS, widgets=widgets) as bar:
        for step in range(STEPS):
            bar.update(step + 1)
            time.sleep(0.005)


if __name__ == '__main__':
    main()

See also

  • SimpleProgress: the same idea without a unit label.

  • DataSize: a single scaled byte value, no total.

  • Counter: the same count with no total to compare against.