'Local file upload progress bar in console using Python
I need to upload a file and show its stats to the user (as it shows on the console).
Didn't find any library for this, the function can be as simple as showing the uploaded percentage (uploaded/filesize*100) and showing the uploaded size (20/50MB) with the upload speed and ETA.
There's a nice library named alive-progress on printing out the progress bar.
but I just don't have any idea on how to do this, like the simpler version of what is done on youtube-dl.
Solution 1:[1]
You could use the sys lib.
Using stdout & flush (more details here).
import sys, time
lenght_bar = 50
sys.stdout.write("Loading : |%s|" % (" " * lenght_bar))
sys.stdout.write("\b" * (lenght_bar+1)) #use backspace
for i in range(lenght_bar):
sys.stdout.write("?")
sys.stdout.flush()
time.sleep(0.1) #process
sys.stdout.write("| 100%")
sys.stdout.write("\nDone")
time.sleep(10)
Solution 2:[2]
There is another way. Combining \r with print(text, end='').
I don't know how you're getting your uploaded_size so there is a sample code that should work anyway.
import time
#Progress bar
def updateProgressBar(size_uploaded, size_file, size_bar=50):
perc_uploaded = round(size_uploaded / size_file * 100)
progress = round(perc_uploaded / 100 * size_bar)
status_bar = f"-{'?' * progress}{' ' * (size_bar - progress)}-"
status_count = f"[{size_uploaded}/{size_file}MB]"
#add your status_eta
#add your status_speed
print(f"\r{status_bar} | {status_count} | {perc_uploaded}%", end='')
#using the carriage-return (\r) to "overwrite" the previous line
#For the demo
file_size = 2**16 #fake file size
uploaded_size = 0
while uploaded_size < file_size:
uploaded_size += 1
updateProgressBar(uploaded_size, file_size)
print("\nDone!")
time.sleep(10)
I suggest that each time you're getting an update about your uploaded_size/ETA/upload_speed you call the updateProgressBar method.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Paul |
| Solution 2 |
