Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to an user defined progress bar inside download function. #241

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jcfaracco
Copy link

@jcfaracco jcfaracco commented Feb 11, 2023

This commit just introduces a new argument to the download function: bar. The argument let the person who is developing code define his own progress bar using whatever he wants to. It also requires the pattern of passing the current size of the file and the total size.

An external function should be defined like

class MyBar:
    def __init__(self, total):
        self._total = total
        self.n = 0.0

    def update(self, cur_chunk_size):
        self.n += cur_chunk_size / self._total
        print(f"{self.n} %")

And then used as:

mybar = MyBar(total=total)
gdown.download(id=id, bar=mybar.update)

Signed-off-by: Julio Faracco [email protected]

pbar.update(len(chunk))
else:
chunk_count += len(chunk)
bar(chunk_count, total)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can make bar as the same api as tqdm.tqdm? and rename it to pbar in the argument?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @wkentaro. Do you mean have the attributes update and close like tqdm or just a function with one parameter?

Copy link
Owner

@wkentaro wkentaro Feb 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jcfaracco. Yeah, I don't mind dropping tqdm if there is an ultimate API to support pbar as function argument.
I wonder how the other libraries are doing this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we analyze progress, we can see a similar approach to tqdm but using different function names. Again, I'm not proposing to drop tqdm, just the ability to use a user-defined pbar. For example, mine is a ipywidget that unfortunately does not work well with tqdm :-( I would love to use gdown with my widget. :-)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the example of progress. To understand more deeply about your needs, can you give me a short snippet of how you want to use with ipywidget?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure @wkentaro

This is some simple example

import threading

from IPython import display as disp
from ipywidgets import HBox, FloatProgress, Label

class NotebookProgressBar(threading.Thread):
    MIN_CUR = -2
    MIN_TOTAL = -1

    def __init__(self):
        threading.Thread.__init__(self)

        self.bar = None
        self.percentage = None
        self.data = None

        self.__lock = threading.Lock()
        self.__current = self.MIN_CUR
        self.__total = self.MIN_TOTAL
        self.__error = False

    def show(self):
        self.bar = FloatProgress(value=0, min=0, max=100)
        self.percentage = Label(value='0 %')
        self.data = Label(value='')
        box = HBox((self.percentage, self.bar, self.data))
        disp.display(box)

    def set_current(self, current, total):
        with self.__lock:
            self.__current = current
            self.__total = total

    def set_error(self, error):
        self.__error = error

    def run(self):
        while (not self.__error and self.__current < self.__total):
            time.sleep(1)

            if self.__current != self.MIN_CUR and self.__total != self.MIN_TOTAL:
                progress = (self.__current / self.__total) * 100
                self.bar.value = progress
                self.percentage.value = f"{int(self.bar.value)} %%"
                self.data.value = f"{int(self.__current)} / {int(self.__total)}"

        if not self.__error:
            self.bar.style.bar_color = '#03c04a'
        else:
            self.bar.style.bar_color = '#ff0000'

All I need to do is update the bar with set_current() method. I usually use with wget, but this module does not work properly with Google Files for obvious reasons.

@wkentaro wkentaro added the feature for pr label Feb 13, 2023
@ageekhere
Copy link

What is the status of this pull request? would like to use a progress bar in my python app.

@jcfaracco
Copy link
Author

jcfaracco commented Sep 29, 2024

I think I can jump back to this feature, because I still need it @wkentaro. I also updated the PR description.

This commit just introduces a new argument to the download function: `bar`.
The argument let the person who is developing code define his own progress
bar using whatever he wants to. It also requires the pattern of passing
the current size of the downloaded chunk. This callable function respects the
tqdm API.

Signed-off-by: Julio Faracco <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature for pr
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants