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.
for a similar application (keeping track of the progress in a loop) I simply used the python-progressbar:
Their example goes something like this,
When running in jupyter notebooks use of normal tqdm doesn't work, as it writes output on multiple lines. Use this instead:
The above suggestions are pretty good, but I think most people just want a ready made solution, with no dependencies on external packages, but is also reusable.
I got the best points of all the above, and made it into a function, along with a test cases.
To use it, just copy the lines under "def update_progress(progress)" but not the test script. Don't forget to import sys. Call this whenever you need to display or update the progress bar.
This works by directly sending the "\r" symbol to console to move cursor back to the start. "print" in python does not recongise the above symbol for this purpose, hence we need 'sys'
This is what the result of the test script shows (The last progress bar animates):
If it is a big loop with a fixed amount of iterations that is taking a lot of time you can use this function I made. Each iteration of loop adds progress. Where count is the current iteration of the loop, total is the value you are looping to and size(int) is how big you want the bar in increments of 10 i.e. (size 1 =10 chars, size 2 =20 chars)
example:
output:
There are specific libraries (like this one here) but maybe something very simple would do:
Note: this progressbar is a fork of progressbar which hasn't been maintained in years.