I would like show progress to user when my python script processing a big file.
I have seen script printings '\', "|', '/'
in the same cursor position in the shell to show progress.
How can I do that in python?
I would like show progress to user when my python script processing a big file.
I have seen script printings '\', "|', '/'
in the same cursor position in the shell to show progress.
How can I do that in python?
A simple "infinite spinner" implementation:
import time
import itertools
for c in itertools.cycle('/-\|'):
print(c, end = '\r')
time.sleep(0.2)
You should use python-progressbar
It's as simple to use as:
import progressbar as pb
progress = pb.ProgressBar(widgets=_widgets, maxval = 500000).start()
progvar = 0
for i in range(500000):
# Your code here
progress.update(progvar + 1)
progvar += 1
This will show a progress bar like:
Progress: |#################################################### |70%
tqdm is a more powerful one for this case. it has better features and comparability.
it is easy for usage, the code could be simple as:
from tqdm import tqdm
for i in tqdm(range(10000)):
pass # or do something else
customization is also easy for special cases.
here is a demo from the repo:
If you want to roll your own, you can do something like this:
import sys, time
for i in range(10):
print ".", # <- no newline
sys.stdout.flush() #<- makes python print it anyway
time.sleep(1)
print "done!"
This'll print one dot every second and then print "done!"
Or the usual helicopter (in Python 3):
import sys, time
for i in range(10):
print("/-\|"[i % 4], end="\b")
sys.stdout.flush() #<- makes python print it anyway
time.sleep(0.1)
print("\ndone!")
Made this for fun. It shows the spinning bar and the text loading. Like this:
|
\ l
- lo
/ loa
| load
\ loadi
- loadin
/ loading
The code was tested on windows.
'''
Spinner and a text showing loading.
'''
import sys
import time
def looper(text):
sys.stdout.write('\r')
sys.stdout.write(text)
sys.stdout.flush()
spinner = ["|", "\\" , "-", "/" ]
loading = ['l','o','a','d','i','n','g']
spin_1 = len(spinner)
spin_2 = len(loading) + 1
print("Starting program...")
for x in range(100):
spin_2mod = x%spin_2
looper(spinner[x%spin_1] + " " + "".join(loading[0: (spin_2mod) ]) + (" " * (spin_2 - spin_2mod)))
time.sleep(0.5)
print("\nExiting program...")
time.sleep(2)
This code works for me:
class ProgressBar(object):
def __init__(self, maxval=100):
self.currval = 0
self.maxval = int(maxval)
self.last_progress = 0
sys.stdout.write("0")
sys.stdout.flush()
def next(self, val = None):
if val:
self.currval = val
else:
self.currval = self.currval + 1
progress = round(20 * self.currval / self.maxval)
while self.last_progress < progress:
self.last_progress = self.last_progress + 1
self._print_progress(self.last_progress)
def finish(self):
while self.last_progress < 20:
self.last_progress = self.last_progress + 1
self._print_progress(self.last_progress)
sys.stdout.write("\n")
sys.stdout.flush()
def _print_progress(self, progress):
if progress % 5 == 0:
percent = int(progress * 5)
sys.stdout.write(str(percent))
else:
sys.stdout.write(".")
sys.stdout.flush()
This code works well with large amount of iterations like hundreds of millions - it doesn't redraw console on every iteration.
Use sample:
progress = ProgressBar(band.YSize)
for y in xrange(0, band.YSize, 256):
array = band.ReadAsArray(x, y, 256, 256)
map_array_values(array, values)
progress.next(y)
del array
progress.finish()
It will display progress in GDAL style: