FormatLabel

FormatLabel renders an arbitrary %-style format string.

Reach for it to display fields from the bar’s own data snapshot – value, max, elapsed and friends – worded however fits, when none of the built-in widgets already say it the way you want.

class progressbar.widgets.FormatLabel(format: str, **kwargs: Any)[source]

Bases: FormatWidgetMixin, WidgetBase

Displays a formatted label.

>>> label = FormatLabel('%(value)s', min_width=5, max_width=10)
>>> class Progress:
...     pass
>>> label = FormatLabel('{value} :: {value:^6}', new_style=True)
>>> str(label(Progress, dict(value='test')))
'test ::  test '

Create a FormatLabel for the given format string.

Example

FormatLabel
"""``FormatLabel`` renders an arbitrary %-style format string.

Reach for it to display fields from the bar's own data snapshot --
``value``, ``max``, ``elapsed`` and friends -- worded however fits, when
none of the built-in widgets already say it the way you want.
"""

import time

import progressbar

STEPS = 24


def main() -> None:
    widgets = [
        progressbar.FormatLabel(
            'Processed: %(value)d lines (in: %(elapsed)s)'
        ),
    ]
    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