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

Fix incomplete progress bar status upon closing when using rich in interactive sessions #1580

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

Conversation

ma-sadeghi
Copy link

Fixes #1306

PR #1395 mostly addressed this issue, except that it doesn't still work with interactive windows (e.g., VS Code Interactive Jupyter). This PR explicitly asks rich to refresh when progress bar is closing.

@ma-sadeghi ma-sadeghi requested a review from casperdcl as a code owner May 7, 2024 20:30
@OverLordGoldDragon
Copy link

A note for others: for manual management, need to call next() once before iterating, since the first call is meant to start the progress bar at 0 (since normally we do for etc in tqdm(), at which point, of course, no progress has been made). Python note, need the try-except since that's how for etc in iterator is terminated in the first place (tqdm throws StopIteration, after printing 100%, then iteration stops).

from tqdm import tqdm
from time import sleep

progbar = iter(tqdm(range(9)))
next(progbar);

for _ in range(9):
    try:
        next(progbar);
    except StopIteration as e:
        pass
    sleep(.1)

This is minimal, ideally there's logic to raise StopIteration if it repeats more than once.

@OverLordGoldDragon
Copy link

OverLordGoldDragon commented Jun 1, 2024

E.g.

from tqdm import tqdm
from time import sleep

progbar = manual_tqdm(range(9))
for _ in range(9):
    next(progbar);
    sleep(.1)

with

def manual_tqdm(obj, **kwargs):
    progbar = iter(tqdm(obj, **kwargs))
    next(progbar);
    yield from progbar
    yield

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants