I have a console python program which does a bunch stuff analyzing excel documents. It works well but some of the operations take a long time and so I wanted to implement some progress bars for user convenience. I wrote this function:
def printProgress(message,value,finalvalue,length=40,mark="#",empty="-"):
n = math.ceil(length * value / finalvalue)
m = length - n
text = message + f"[{mark*n}{empty*m}]"
print(text,end='r')
This function is called from within some of the longer loops in the program and it does work. But it only prints the bar once the process is complete so not really very useful.
I have tried using
print(text,end='r',flush=True)
and also placing
sys.stdout.flush()
after the print statement. But to no-avail, I can’t seem to get my bar to update in real time. I did test this function in a separate program where it was just called from a long loop with a sleep function in it and then it worked perfectly updating each iteration of the loop. But I don’t want to introduce a sleep as that’s just going to make my process even longer!
What am I missing here? I do know there are libraries available that do this but I’d like to understand why this code doesn’t work.
I am running this on Python 3.7 in an Anaconda terminal on Windows 10.