FileTransferSpeed

FileTransferSpeed shows the transfer rate averaged over the run.

Reach for it whenever a bar tracks bytes moved rather than abstract units: a transfer rate, in a sensibly scaled unit, alongside the count. For very slow transfers it flips to inverse_format and renders seconds per unit instead.

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

Bases: FormatWidgetMixin, TimeSensitiveWidgetBase

Widget showing the transfer speed (useful for file transfers).

Create a FileTransferSpeed.

Parameters:
  • format – Template used once a speed can be computed.

  • inverse_format – Template used for slow transfers (see __call__), rendering seconds-per-unit instead of units-per-second.

  • 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

FileTransferSpeed
"""``FileTransferSpeed`` shows the transfer rate averaged over the run.

Reach for it whenever a bar tracks bytes moved rather than abstract
units -- a transfer rate, in a sensibly scaled unit, alongside the
count. For a rate that reacts to recent speed changes instead of the
whole-run average, see ``AdaptiveTransferSpeed``.
"""

import time

import progressbar

STEPS = 24


def main() -> None:
    widgets = [
        progressbar.Percentage(),
        ' ',
        progressbar.Bar(),
        ' ',
        progressbar.FileTransferSpeed(),
    ]
    max_value = 1_000_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