Intro (Riding in on a Interstellar Office Chair)
I have a program that uses a progress bar to show the end-user if the program is working or not. This should tell the end-user how long they have to wait and if the program is still working. This question came after reading this interesting stack overflow thread: Text progress bar in terminal with block characters
Problems/Challenges
The first problem is the progress bar only works in a loop currently and just prints out the numbers in the range. This is not helpful. Why would anyone want a program with a progress bar that just shows the progress of a single loop but not what the rest of the program is doing.
The second problem is that when I have other print statements with the progress bar there are suddenly multiple progress bars printed to the command prompt instead of a single progress bar that appears to animate with each progress bar update.
How can I have the progress bar always at the bottom of the terminal with the other print statements above it?
Another challenge is I am using the Windows 7 operating-system (OS).
Code
Please check out the following example code I have for you:
import sys
import time
import threading
def progress_bar(progress):
sys.stdout.write('\r[{0}] {1}'.format('#' * int(progress/10 * 10), progress))
def doThis():
for i in range(10):
print("Doing this.")
def doThat():
for i in range(3):
print("Doing that.")
def wrapUp():
total = 2+ 2
print("Total found")
return total
if __name__ == "__main__":
print("Starting in main...")
progress_bar(1)
print("\nOther print statement here.")
print("Nice day, expensive day.")
progress_bar(3)
doThis()
progress_bar(4)
doThat()
progress_bar(5)
doThis()
doThat()
progress_bar(6)
progress_bar(7)
doThat()
doThat()
progress_bar(8)
wrapUp()
progress_bar(9)
progress_bar(10)
What the Program Prints
Starting in main...
[#] 1
Other print statement here.
Nice day, expensive day.
[###] 3Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
[####] 4Doing that.
Doing that.
Doing that.
[#####] 5Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing this.
Doing that.
Doing that.
Doing that.
[#######] 7Doing that.
Doing that.
Doing that.
Doing that.
Doing that.
Doing that.
[########] 8Total found
[##########] 10