rsalmei / alive-progress

A new kind of Progress Bar, with real-time throughput, ETA, and very cool animations!
MIT License
5.45k stars 205 forks source link

How to use with requests.get #218

Closed gtxg16 closed 1 year ago

TheTechRobo commented 1 year ago

Do you mean to show download progress? If so, I'm not sure Requests can do that.

gtxg16 commented 1 year ago

tqdm can do it

rsalmei commented 1 year ago

I'm not aware too of how this would work.

rsalmei commented 1 year ago

Ahh, it seems very easy. Requests support downloading in streaming mode, then you just open a progress bar for the chunks. I'll give an example later.

rsalmei commented 1 year ago

Here it is:

from alive_progress import alive_bar
import requests

chunk_size = 1024
resp = requests.get('https://releases.ubuntu.com/22.10/ubuntu-22.10-desktop-amd64.iso', stream=True)
total = int(resp.headers.get('content-length', 0))
with open('ubuntu.iso', 'wb') as file, alive_bar(total, unit='B', scale=True) as bar:
    for data in resp.iter_content(chunk_size=chunk_size):
        size = file.write(data)
        bar(size)

And with the beauty alive_progress is known for:

https://user-images.githubusercontent.com/6652853/210184357-28ba8e0f-b66f-45d1-823a-2419f7377800.mov

rsalmei commented 1 year ago

Ahh, the best would actually be to use a chunk size of 1000, since the SI scale I've used above also uses 1000 as its underlying divisor. Also, it is becoming the new common in modern operational systems like macOS to show file sizes in base 10 instead of base 2. Chunks of 1024 would be ideal to use with either the IEC scale (that uses 'i' kibibytes), or the modified SI2 I've created for this, which retains normal SI units but uses 1024 as divisor.