user3812335
user3812335

Reputation: 125

Using Tqdm to add a Progress Bar when Downloading Files

I have been trying to set up a progress bar using the Tqdm module in python 3.6 but seems i am halfway there.

My code is the following:

from tqdm import tqdm
import requests
import time

url = 'http://alpha.chem.umb.edu/chemistry/ch115/Mridula/CHEM%20116/documents/chapter_20au.pdf'

# Streaming, so we can iterate over the response.
r = requests.get(url, stream=True)
#Using The Url as a filename
local_filename = url.split('/')[-1]
# Total size in bytes.
total_size = int(r.headers.get('content-length', 0))
#Printing Total size in Bytes
print(total_size)

#TQDM
with open(local_filename, 'wb') as f:
    for data in tqdm(r.iter_content(chunk_size = 512), total=total_size, unit='B', unit_scale=True):
        f.write(data)

The Issue is that, when i insert the chunk_size = 512 in r.iter_content the progress bar does not load at all while showing the downloading data, but when i remove chunk_size = 512 completely and leave the parentheses blank the bar loads up exactly as it should but the download speed is horrible.

What am i doing wrong here?

Upvotes: 5

Views: 6978

Answers (2)

Christian Steinmeyer
Christian Steinmeyer

Reputation: 954

Elevating @Yuval's comment to an answer for visibility: You can adjust your tqdm object as done in the tqdm requests wrapper.

progress_bar = tqdm(
    total=file_size,  # in bytes
    desc=f"Downloading {filename}",
    unit="B",
    unit_scale=True,
    unit_divisor=1024,  # make use of standard units e.g. KB, MB, etc.
    miniters=1,  # recommended for network progress that might vary strongly
)

Upvotes: 3

RedCode
RedCode

Reputation: 363

You're not far off but simply missing pretty much all the code to make the progress bar work accordingly. Assuming you have already created your interface, here is a method I used for my progress bar. It downloads the file and saves it on the desktop (but you can specify where you want to save it). It simply takes how much of the file is downloaded and divides it by the total file size then uses that value to update the progress bar. Let me know if this code helps:

url = 'http://alpha.chem.umb.edu/chemistry/ch115/Mridula/CHEM%20116/documents/chapter_20au.pdf'
save = 'C:/Users/' + username + "/Desktop/"    
r = requests.get(url, stream=True)
total_size = int(r.headers["Content-Length"])
downloaded = 0  # keep track of size downloaded so far
chunkSize = 1024
bars = int(total_size / chunkSize)
print(dict(num_bars=bars))
with open(filename, "wb") as f:
    for chunk in tqdm(r.iter_content(chunk_size=chunkSize), total=bars, unit="KB",
                                  desc=filename, leave=True):
        f.write(chunk)
        downloaded += chunkSize  # increment the downloaded
        prog = ((downloaded * 100 / total_size))
        progress["value"] = (prog)  # *100 #Default max value of tkinter progress is 100
            
return

progress = the progress bar

Upvotes: 2

Related Questions