I'm using Python 3.x and have downloaded the progressbar2 library; however, I'm struggling with how to use it in my program. Everything works fine when there's a specified range but how do I get the progressbar to update in real-time with my own code? I expect the progress bar to jump 20% at every iteration of the texts list with it being display first and refresh the progress as it prints out the results.
I've also found another question similar to mine on Stack but I'd prefer using the progressbar2 library since it's already there. Consider the following:
import time
import progressbar
texts = ['a', 'b', 'c', 'd', 'e']
def basic_widget_example (texts):
widgets = [progressbar.Percentage (), progressbar.Bar ()]
bar = progressbar.ProgressBar (widgets = widgets, max_value = len (texts)).start ()
for i in range (len (texts)):
print (texts [i])
time.sleep (0.1)
bar.update (i + 1)
bar.finish ()
Results:
N/A%| |a
20%|###################### |b
40%|############################################ |c
60%|################################################################## |d
80%|######################################################################################## |e
100%|###############################################################################################################|
I would like it to look like this:
100%|#############################################################|
a
b
c
d
e
One solution is to create 2 for-loop but as you can see the progressbar display is no longer accurate or in sync with my tasks. The progressbar will show 100% in less than a second even though my tasks may take 2 minutes to complete. Any idea how to achieve this while being close to accurate as possible?
def basic_widget_example (texts):
widgets = [progressbar.Percentage (), progressbar.Bar ()]
bar = progressbar.ProgressBar (widgets = widgets, max_value = len (texts)).start ()
for i in range (len (texts)):
# Do something
time.sleep (0.1)
bar.update (i + 1)
bar.finish ()
for text in texts:
print (text)