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.
Asked
Active
Viewed 143 times
0
Ali Abdi
- 126
- 4
- 14
-
How are you uploading the file? FTP, HTTP, etc? Or are you only concerned with showing the progress bar and good on the upload portion? – aviso Feb 23 '22 at 20:03
2 Answers
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)
Paul
- 104
- 2
- 7
-
Thank you! but what about the file size? how can I create a progress-bar dependent on the file size? (so I can determine how much uploaded). – Ali Abdi Feb 22 '22 at 12:01
0
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.
Paul
- 104
- 2
- 7