Skip to content

Instantly share code, notes, and snippets.

@justinmklam
Last active June 4, 2021 15:17
Show Gist options
  • Save justinmklam/58971adb68269336177b4fc8b1c76078 to your computer and use it in GitHub Desktop.
Save justinmklam/58971adb68269336177b4fc8b1c76078 to your computer and use it in GitHub Desktop.
Simple progress bar without external dependencies. Adapted from https://stackoverflow.com/a/34482761
import os
import sys
def progressbar(it, prefix="", file=sys.stdout):
count = len(it)
num_bar_chars = 5
width = os.get_terminal_size().columns - (
len(prefix) + len(str(count)) * 2 + num_bar_chars
)
def show(j):
x = int(width * j / count)
file.write("%s[%s%s] %i/%i\r" % (prefix, "#" * x, "." * (width - x), j, count))
file.flush()
show(0)
for i, item in enumerate(it):
yield item
show(i + 1)
file.write("\n")
file.flush()
if __name__ == "__main__":
import time
for i in progressbar(range(0, 10), "Processing: "):
time.sleep(0.5)
# Final output:
# Processing: [#################################################] 10/10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment