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

notebook: support changing total #1145

Open
r786786 opened this issue Mar 12, 2021 · 3 comments · May be fixed by #1480
Open

notebook: support changing total #1145

r786786 opened this issue Mar 12, 2021 · 3 comments · May be fixed by #1480
Assignees
Labels
p2-bug-warning ⚠ Visual output bad p3-enhancement 🔥 Much new such feature submodule-notebook 📓 Much web such IDE to-fix ⌛ In progress
Projects
Milestone

Comments

@r786786
Copy link

r786786 commented Mar 12, 2021

import urllib, os
from tqdm.notebook import tqdm
urllib = getattr(urllib, 'request', urllib)

class TqdmUpTo(tqdm):
    """Provides `update_to(n)` which uses `tqdm.update(delta_n)`."""
    def update_to(self, b=1, bsize=1, tsize=None):
        """
        b  : int, optional
            Number of blocks transferred so far [default: 1].
        bsize  : int, optional
            Size of each block (in tqdm units) [default: 1].
        tsize  : int, optional
            Total size (in tqdm units). If [default: None] remains unchanged.
        """
        if tsize is not None:
            self.total = tsize
        return self.update(b * bsize - self.n)  # also sets self.n = b * bsize

eg_link = "https://sample-videos.com/zip/100mb.zip"
with TqdmUpTo(unit='B', unit_scale=True, unit_divisor=1024, miniters=1,
              desc=eg_link.split('/')[-1]) as t:  # all optional kwargs
    urllib.urlretrieve(eg_link, filename=os.devnull, reporthook=t.update_to, data=None)
    t.total = t.n

This is literally the same exact code as was presented in the readme page (https://github.com/tqdm/tqdm#hooks-and-callbacks) except instead of

from tqdm import tqdm

I inputted

from tqdm.notebook import tqdm

The issue I am getting is that the progress bar doesn't update until it finishes to 100% (turns green). Other than that, during the entire duration of the download, this is how the progress bar looks like:
image

Another thing to point out is that the figures work perfectly fine, it's just the visual interface is stuck at 0% it seems like.

I've tried this on both Google Colab and Jupyter notebook, same issue.

@casperdcl
Copy link
Sponsor Member

casperdcl commented Mar 12, 2021

Hmm that's because there's no initial total. Actually I don't think notebook works at all when total is changing; so I've changed the issue title accordingly. (Note to self: related #1143 finally adding some notebook tests.)

Anyway in the meantime it's probably better to use the requests version anyway (further down in the documentation):

import requests, os
from tqdm.auto import tqdm

eg_link = "https://sample-videos.com/zip/100mb.zip"
response = requests.get(eg_link, stream=True)
with tqdm.wrapattr(open(os.devnull, "wb"), "write",
                   miniters=1, desc=eg_link.split('/')[-1],
                   total=int(response.headers.get('content-length', 0))) as fout:
    for chunk in response.iter_content(chunk_size=4096):
        fout.write(chunk)

image

@casperdcl casperdcl self-assigned this Mar 12, 2021
@casperdcl casperdcl added p2-bug-warning ⚠ Visual output bad p3-enhancement 🔥 Much new such feature submodule-notebook 📓 Much web such IDE to-fix ⌛ In progress labels Mar 12, 2021
@casperdcl casperdcl changed the title Progress bar doesn't work in notebook notebook: support changing total Mar 12, 2021
@casperdcl casperdcl added this to the Non-breaking milestone Mar 12, 2021
@casperdcl casperdcl added this to In Progress in Casper Mar 12, 2021
@redbrain
Copy link

Still a bug on Colab today. Any updates?

@prohde
Copy link

prohde commented Jun 28, 2023

This is still an issue in version 4.65.0. Any updates?

prohde added a commit to prohde/tqdm that referenced this issue Jul 5, 2023
@prohde prohde linked a pull request Jul 5, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-bug-warning ⚠ Visual output bad p3-enhancement 🔥 Much new such feature submodule-notebook 📓 Much web such IDE to-fix ⌛ In progress
Projects
Casper
  
In Progress
Development

Successfully merging a pull request may close this issue.

4 participants