Percentage

Percentage displays the current progress as N%.

Reach for it as the simplest possible progress readout: just the percentage, with no bar. When max_value is unknown it renders the na text instead (default N/A%).

class progressbar.widgets.Percentage(format='%(percentage)3d%%', na='N/A%%', **kwargs: Any)[source]

Bases: FormatWidgetMixin, ColoredMixin, WidgetBase

Displays the current percentage as a number with a percent sign.

Create a Percentage.

Parameters:
  • format – Template used once a percentage is available.

  • na – Template used when it isn’t (data[‘percentage’] is None).

  • **kwargs – Forwarded to the next class in the cooperative __init__ chain.

get_format(progress: ProgressBarMixinBase, data: Data, format=None)[source]

Return self.na, colored, when no percentage is available yet.

Example

Percentage
"""``Percentage`` displays the current progress as ``N%``.

Reach for it as the simplest possible progress readout -- just the
percentage, with no bar. Compare ``SimpleProgress`` ("5 of 47") when
the raw counts matter more than the ratio, and ``PercentageLabelBar``
to overlay the same percentage on a fill bar instead of beside it.
"""

import time

import progressbar

STEPS = 24


def main() -> None:
    widgets = [progressbar.Percentage()]
    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