python-progressbar icon indicating copy to clipboard operation
python-progressbar copied to clipboard

Progress bar out of range error

Open juan190199 opened this issue 1 year ago • 1 comments

Description

I have a fit function with a for-loop inside of it: for epoch in self.progressbar(range(50)):

When using a Jupyter notebook, when running the cell for the first time, it works fine. However if I rerun the cell, it just throws me an error:

File ~/.local/share/virtualenvs/ML_Zero_To_Hero-tWttz9GG/lib/python3.11/site-packages/progressbar/progressbar.py:154, in ProgressBar.__next__(self)
    152         self.start()
    153     else:
--> 154         self.update(self.currval + 1)
    155     return value
    156 except StopIteration:

File ~/.local/share/virtualenvs/ML_Zero_To_Hero-tWttz9GG/lib/python3.11/site-packages/progressbar/progressbar.py:250, in ProgressBar.update(self, value)
    246 if value is not None and value is not widgets.UnknownLength:
    247     if (self.maxval is not widgets.UnknownLength
    248         and not 0 <= value <= self.maxval):
--> 250         raise ValueError('Value out of range')
    252     self.currval = value
    255 if not self._need_update(): return

ValueError: Value out of range

I am instantiating the progress bar as: progressbar.ProgressBar(widgets=bar_widgets) where bar_widgets = [ 'Training: ', progressbar.Percentage(), ' ', progressbar.Bar(marker='-', left='[', right=']'), ' ', progressbar.ETA() ]

Do I have to somehow reset the progress bar?

juan190199 avatar May 30 '24 19:05 juan190199

You can just call the start() method again, that calls the init() method internally which (re)sets the progressbar.

I would suggest the usage of a contextmanager to make sure everything is started and stopped properly in all cases :)

with self.progressbar as bar:
    for epoch in bar(range(50)):
        ...

wolph avatar May 30 '24 21:05 wolph