attat
attat

Reputation: 57

Adding a progress bar in pytube

I was able to add a progress bar with 1 using Zubo's answer. However, I wanted to only show multiples of 10 (10%,20%,30%....100%) so I added if statement

But, when I run the code, the outcome was printing multiples of 10. How do I code so that it only prints once?

def progress_function(stream, chunk, file_handle, bytes_remaining):
    percent = round((1-bytes_remaining/video.filesize)*100)
    if( percent%10 == 0):
        print(percent, '% done...')

The other question is for Ismael GraHms's answer, he added self inside the method parameter. def progress_function(self,stream, chunk,file_handle, bytes_remaining): However, when I ran his code, it showed an error progress_function() missing 1 required positional argument: 'bytes_remaining'. I don't quite get why his code is not running.

0 % done...
0 % done...
0 % done...
0 % done...
0 % done...
0 % done...
0 % done...
0 % done...
0 % done...
0 % done...
0 % done...
10 % done...
10 % done...
10 % done...
10 % done...
10 % done...
10 % done...
10 % done...
10 % done...
.
.omitted for space issue but same goes on for 20%,30%...
.
100 % done...
100 % done...
100 % done...
100 % done...
100 % done...
100 % done...
100 % done...
100 % done...
100 % done...
100 % done...
100 % done...
100 % done...
100 % done...

Upvotes: 0

Views: 842

Answers (1)

klutt
klutt

Reputation: 31409

A quick fix is something like this:

progress = 0
if(progress <= round((1-bytes_remaining/video.filesize)*100)):
    print(progress, '% done...')
    progress += 10

Upvotes: 1

Related Questions