Skip to content

Progress bar is duplicated when used both as a context manager and as an iterable wrapper #301

@dreamscached

Description

@dreamscached

Description

When used both as context manager (e.g. with) and as iterable wrapper, exhausting the iterable and exiting the with scope causes the progress bar to duplicate.

Code

Consider the following example code:

from time import sleep
from progressbar import ProgressBar
from progressbar.widgets import AnimatedMarker, SimpleProgress, GranularBar, Timer

def simple_iterator():
    for i in range(100):
        sleep(0.02)
        yield i

progress = ProgressBar(
    max_value=100,
    redirect_stdout=True,
    redirect_stderr=True,
    widgets=[AnimatedMarker("⡏⠟⠻⢹⣸⣴⣦⣇"), " ",
             SimpleProgress(), " ",
             GranularBar(markers="░▒▓█"), " ",
             Timer()]
)

with progress:
    for i in progress(simple_iterator()):
        pass

ASCIInema demo: https://asciinema.org/a/UEEc81s5GyrAR3QaqLDWGIMGX

Versions

  • Python version: 3.12.6 (main, Sep 8 2024, 13:18:56) [GCC 14.2.1 20240805]
  • Python distribution/environment: CPython
  • Operating System: Arch Linux, etc.
  • Package version: 4.5.0

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions