Skip to content

Instantly share code, notes, and snippets.

@RyanMillerC
Forked from aubricus/License
Last active May 14, 2018 17:28
Show Gist options
  • Save RyanMillerC/fc7a8933e09fc84727fc03003f90a7d3 to your computer and use it in GitHub Desktop.
Save RyanMillerC/fc7a8933e09fc84727fc03003f90a7d3 to your computer and use it in GitHub Desktop.
Printable Progress Bar for Python
# -*- coding: utf-8 -*-
import sys
class ProgressBar():
"""Printable progress bar.
"""
def __init__(
self, total, iterator=0, prefix='Progress',
suffix='Completed', decimals=1, bar_length=50
):
"""Initialize instance and print process bar at 0 percent
completed.
:param int total:
Total number of cycles to iterate through.
:param int iterator:
Current cycle. Typically leave at default value.
:param str prefix:
Text to prepend before progress bar.
:param str suffix:
Text to append after progress bar.
:param int decimals:
Round percent shown to this decimal place.
:param int bar_length:
Display length of progress bar.
"""
self.bar_len = bar_length
self.decimals = decimals
self.iterator = iterator
self.prefix = prefix
self.suffix = suffix
self.total = total
self.update()
def update(self):
"""Update progress bar. Call once per cycle.
"""
str_format = "{0:." + str(self.decimals) + "f}"
percents = str_format.format(100 * (self.iterator / float(self.total)))
f_length = int(round(self.bar_len * self.iterator / float(self.total)))
bar = '█' * f_length + '-' * (self.bar_len - f_length)
sys.stdout.write('\r%s |%s| %s%s %s'
%(self.prefix, bar, percents, '%', self.suffix))
if self.iterator == self.total:
sys.stdout.write('\n')
sys.stdout.flush()
self.iterator += 1
@RyanMillerC
Copy link
Author

Change character to = for Windows compatibility

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment