rsalmei / alive-progress

A new kind of Progress Bar, with real-time throughput, ETA, and very cool animations!
MIT License
5.45k stars 205 forks source link

How to use the progress bar without the context manager? #200

Closed Advik-B closed 1 year ago

Advik-B commented 1 year ago

I dont want to use the with context manager

from alive_progress import alive_bar

bar = alive_bar()
# Set the total number of steps
bar.total = 100
# Increment the progress bar
???
rsalmei commented 1 year ago

Why not? I made it that way so it could be always correctly initialized and finalized, there isn't much sense trying to not use it.

Advik-B commented 1 year ago

Cause of this

Screenshot 2022-10-30 at 12 26 46 AM
rsalmei commented 1 year ago

Ok, you'd do it like this:

class ProgressBar:
    def __init__(self, value: int = None):
        self.cm = alive_bar(total=value)
        self.bar = self.cm.__enter__()

    def step(self):
        self.bar()

    def close(self):
        self.cm.__exit__(None, None, None)

To use it:


progress = ProgressBar(1000)
for i in range(1000):
    time.sleep(.01)
    progress.step()
progress.close()
Advik-B commented 1 year ago

Thank you will. Try this. Please keep the issue open

Advik-B commented 1 year ago

Ok, but how do I access the usefull variables like:

bar.total
bar.current
rsalmei commented 1 year ago

self.bar field is the exact alive_progress' bar, so you can use anything that I provide there. There isn't the total for example, but you should know with which you started, so you could do:

class ProgressBar:
    def __init__(self, value: int = None):
        self._total = total
        self.cm = alive_bar(total=value)
        self.bar = self.cm.__enter__()

    def step(self):
        self.bar()

    def close(self):
        self.cm.__exit__(None, None, None)

    @property
    def total(self):
        return self._total

    @property
    def current(self):
        return self.bar.current()
Advik-B commented 1 year ago

Okay, will try this

rsalmei commented 1 year ago

Ok, I'm closing this one, but feel free to reply here if you need any more help 👍