DataSize

DataSize shows a single byte count scaled with binary prefixes.

Reach for it to show an amount of data transferred or processed so far: “12.5 MiB” instead of a raw byte count. variable picks which bar value it renders, and unit/prefixes control the scaling labels.

class progressbar.widgets.DataSize(variable='value', format='%(scaled)5.1f %(prefix)s%(unit)s', unit='B', prefixes=('', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'), **kwargs)[source]

Bases: FormatWidgetMixin, WidgetBase

Widget for showing an amount of data transferred/processed.

Automatically formats the value (assumed to be a count of bytes) with an appropriate sized unit, based on the IEC binary prefixes (powers of 1024).

Create a DataSize.

Parameters:
  • variable – Key in data holding the byte count to render.

  • format – The template string (see FormatWidgetMixin).

  • unit – Unit label appended after the IEC prefix.

  • prefixes – IEC binary prefixes, smallest (none) to largest.

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

Example

DataSize
"""``DataSize`` shows a single byte count scaled with binary prefixes.

Reach for it to show an amount of data transferred or processed so far
-- "12.5 MiB" instead of a raw byte count -- as one value scaled to a
sensible unit. Compare ``FileTransferSpeed``, which shows a *rate*
instead of a static amount.
"""

import time

import progressbar

STEPS = 24


def main() -> None:
    widgets = [progressbar.DataSize(), ' ', progressbar.Bar()]
    max_value = 12_500_000
    with progressbar.ProgressBar(max_value=max_value, widgets=widgets) as bar:
        for step in range(STEPS):
            bar.update(int(max_value * (step + 1) / STEPS))
            time.sleep(0.005)


if __name__ == '__main__':
    main()

See also