How to display mb/s instead of b/s in progressbar and Python?
<p>I am trying to change the way progressbar (version 2) displays the transferred/total file size in Python 3. Here's the code:     </p>

<pre><code>import progressbar
import requests

url = 'http://download.openbricks.org/sample/H264/big_buck_bunny_1080p_H264_AAC_25fps_7200K_short.MP4'

request = requests.get(url, stream=True)

file         = open('test.mp4', 'wb')
file_size    = int(request.headers['Content-Length'])
file_size_mb = round(file_size / 1024 / 1024,2)

chunk_sz = 512

widgets = [progressbar.Bar(marker="#",left="[",right="]"),
           progressbar.Percentage()," | ",
           progressbar.FileTransferSpeed()," | ",
           progressbar.SimpleProgress()," | ",
           progressbar.ETA()]

bar = progressbar.ProgressBar(widgets=widgets, maxval=file_size).start()

i = 0

for chunk in request.iter_content(chunk_size=chunk_sz):
    file.write(chunk)
    i += len(chunk)
    bar.update(i)

bar.finish()
print('File size: {0} MB'.format(file_size_mb))

file.close()
</code></pre>

<p>The output:</p>

<blockquote>
  <p>[#########################] 91% | 741.3 KiB/s | 3397632 of 3714474 | Time: 00:00:08</p>
  
  <p>File size: 3.54 MB</p>
</blockquote>

<p>I want to be able to make the "<strong>3397632</strong> of <strong>3714474</strong>" be displayed in the MB format (like in the <em>file_size_mb</em> variable), not in Bytes as it is being displayed by progressbar by default.</p>

<p>I've read the docs in <a href="http://progressbar-2.readthedocs.io/en/latest/" rel="nofollow" title="Welcome to Progress Barâs documentation!">Progress Barâs documentation</a> but I couldn't find the answer to my question in any of the examples given there.</p>

<p>you can obtain this kind of display relatively easily with <a href="http://progressbar-2.readthedocs.io/en/latest/progressbar.widgets.html#progressbar.widgets.DataSize" rel="nofollow">DataSize</a> widget:</p>

<pre><code>[######                                 ] 17% | 256.0 KiB/s | 631.0 KiB /   3.5 MiB | ETA: 0:00:11
</code></pre>

<p>you can do this by replacing the <a href="http://progressbar-2.readthedocs.io/en/latest/progressbar.widgets.html#progressbar.widgets.SimpleProgress" rel="nofollow">SimpleProgress</a> widgets by two <a href="http://progressbar-2.readthedocs.io/en/latest/progressbar.widgets.html#progressbar.widgets.DataSize" rel="nofollow">DataSize</a> widgets, as shown below:</p>

<pre><code>widgets = [progressbar.Bar(marker="#",left="[",right="]"),
           progressbar.Percentage(), " | ",
           progressbar.FileTransferSpeed(), " | ",
           progressbar.DataSize(), " / ",
           progressbar.DataSize(variable="max_value"), " | ",
           progressbar.ETA()]
</code></pre>

<p>By default, the <a href="http://progressbar-2.readthedocs.io/en/latest/progressbar.widgets.html#progressbar.widgets.DataSize" rel="nofollow">DataSize</a> widget displays the current value, but you can specify that you want it to display the max value by passing it "max_value" as <em>variable</em> parameter.</p>

<p>The problem is that it is not documented and you have to inspect the code to find this information. So this could be changed one day and no more work with a later version of the module.</p>

<p>Considering <a href="http://progressbar-2.readthedocs.io/en/latest/progressbar.widgets.html#progressbar.widgets.DataSize" rel="nofollow">the DataSize doc</a>, it seems you can alter the default format by passing a custom value to <em>format</em> parameter. The default format is</p>

<pre><code>format='%(scaled)5.1f %(prefix)s%(unit)s'
</code></pre>

<p>I think you could get two decimal numbers by specifying this format:</p>

<pre><code>format='%(scaled)5.2f %(prefix)s%(unit)s'
</code></pre>

<p>Concerning the displayed unit, the doc mentions the prefixes parameter which defaults to</p>

<pre><code>prefixes=('', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi')
</code></pre>

<p>You could probably pass another tuple with the prefixes you want. But consider this <a href="https://en.wikipedia.org/wiki/Binary_prefix" rel="nofollow">article discussing about binary prefixes</a> before doing that. If you use powers of 1024 to convert your units, and if you want to be exact, then you probably wants to use the IEC binary prefixes with the "i" character.</p>

<hr>

<p>An other, but longer, way to achieve what you want would be to derive the <a href="http://progressbar-2.readthedocs.io/en/latest/progressbar.widgets.html#progressbar.widgets.SimpleProgress" rel="nofollow">SimpleProgress</a> class to make one which fits you needs.</p>

python python-3.x format progress-bar transfer