How do I use a progress bar when my script is doing some task that is likely to take time?
For example, a function which takes some time to complete and returns True
when done. How can I display a progress bar during the time the function is being executed?
Note that I need this to be in real time, so I can't figure out what to do about it. Do I need a thread
for this? I have no idea.
Right now I am not printing anything while the function is being executed, however a progress bar would be nice. Also I am more interested in how this can be done from a code point of view.
I really like the python-progressbar, as it is very simple to use.
For the most simple case, it is just:
The appearance can be customized and it can display the estimated remaining time. For an example use the same code as above but with:
I like Gabriel answer, but i changed it to be flexible. You can send bar-length to the function and get your progress bar with any length that you want. And you can't have a progress bar with zero or negative length. Also, you can use this function like Gabriel answer (Look at the Example #2).
Result:
Use this library:
fish
(GitHub).Usage:
Have fun!
If your work can't be broken down into measurable chunks, you could call your function in a new thread and time how long it takes:
You can obviously increase the timing precision as required.
a little more generic answer of jelde015 (credit to him of course)
for updating the loading bar manually will be:
and calling it by:
will result:
just call it whenever you want with the current
i
value.set the
size
as the number of chars the bar should beYou can also use enlighten. The main advantage is you can log at the same time without overwriting your progress bar.
It also handles multiple progress bars.