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 like this page.
Starts with simple example and moves onto a multi-threaded version. Works out of the box. No 3rd party packages required.
The code will look something like this:
Or here is example to use threads in order to run the spinning loading bar while the program is running:
The code below is a quite general solution and also has a time elapsed and time remaining estimate. You can use any iterable with it. The progress bar has a fixed size of 25 characters but it can show updates in 1% steps using full, half, and quarter block characters. The output looks like this:
Code with example:
Suggestions for improvements or other comments are welcome. Have fun.
With tqdm you can add a progress meter to your loops in a second:
I've just made a simple progress class for my needs after searching here for a equivalent solution. I thought I might a well post it.
Example :
Will print the following:
[======== ] 17/80 ( 21%) 63 to go
I like Brian Khuu's answer for its simplicity and not needing external packages. I changed it a bit so I'm adding my version here:
It takes the total number of runs (
total
) and the number of runs processed so far (progress
) assumingtotal >= progress
. The result looks like this: