Skip to content

Instantly share code, notes, and snippets.

@ChesterChowWOV
Last active January 26, 2024 17:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChesterChowWOV/2b35c551b339adbf459363322aac5b4b to your computer and use it in GitHub Desktop.
Save ChesterChowWOV/2b35c551b339adbf459363322aac5b4b to your computer and use it in GitHub Desktop.

This gist remixes this answer on StackOverflow. My changes:

  • I changes the symbol from # to a unicode character that fills a whole character space - .
  • I added a % symbol after the numbers

This is an example: progress-bar

def progressbar(it, prefix="", size=60, file=sys.stdout):
count = len(it)
def show(j):
x = int(size*j/count)
file.write("{}[{}{}] {}%/{}%\r".format(prefix, "█"*x, "."*(size-x), j, count))
file.flush()
show(0)
for i, item in enumerate(it):
yield item
show(i+1)
file.write("\n")
file.flush()
@erichlf
Copy link

erichlf commented May 17, 2023

I made a version that allows you to specify the symbols and empty space:

def progressbar(it, prefix="", symbol="█", empty=" ", size=60, out=sys.stdout):
    count = len(it)
    def show(j):
        x = int(size*j/count)
        print(f"{prefix}[{symbol*x}{empty*(size-x)}] {j}/{count}",
                end='\r', file=out, flush=True)
    show(0)
    for i, item in enumerate(it):
        yield item
        show(i+1)
    print("\n", flush=True, file=out)

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