Skip to content

Instantly share code, notes, and snippets.

@sebble
Last active August 29, 2015 14:11
Show Gist options
  • Save sebble/7c985599e2fe1a9df97a to your computer and use it in GitHub Desktop.
Save sebble/7c985599e2fe1a9df97a to your computer and use it in GitHub Desktop.
Really simple (but flexible) python progress bar
import sys
def progress(current, total=1., text='', width=80, fillchar='#', padchar=' ', startchar='[', endchar=']', done='\n'):
if current > total:
raise ValueError('current is greater than total')
if text is not '':
text = text + ': '
bar = width-len(startchar)-len(endchar)-len(text)
fill = int(float(bar)*current/total)
pad = bar - fill
reset = '' if current == 0 else '\r'
if done is not '\n':
done = '\r'+done+' '*(width-len(done))+'\n'
if current < total:
done = ''
sys.stdout.write(reset+text+startchar+fillchar*fill+padchar*pad+endchar+done)
sys.stdout.flush()
import time
print("This text is before the progress bar and will not be cleared.")
n=17
progress(0,n,'Completed',fillchar='=',padchar='-')
for i in range(n):
time.sleep(1)
progress(i+1,n,'Completed',fillchar='=',padchar='-',done='The thing has completed.')
print("This text is after the progress bar and will be on a new line.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment