Skip to content

Instantly share code, notes, and snippets.

@shackenberg
Last active March 8, 2025 11:21
Show Gist options
  • Save shackenberg/3716039 to your computer and use it in GitHub Desktop.
Save shackenberg/3716039 to your computer and use it in GitHub Desktop.
super minimal progress bar (Python)
Code now at https://github.com/shackenberg/pbar.py
@kaecy
Copy link

kaecy commented Mar 8, 2025

I wrote one of these at https://pypi.org/project/minimal-progress/

Some minimal code for use:

from minimal import Progress
import time

# Program Main - Simulate progress
processed = 0
total = 50
pg = Progress()

try:
    while processed != total:
        processed += 1
        pg.update(processed / total)
        time.sleep(.1)
except BaseException:
    pg.clean()

A very minimal class that even you can write it.

from signal import signal, SIGINT

bar = chr(0x2501)
barsize = 40

class Progress:
    def __init__(self):
        self.trackedProgress = -1
        self.oldsignal = signal(SIGINT, self.signal)

    def update(self, percent):
        progressed = int(percent * barsize) # Tells us if the progress has changed
        if progressed != self.trackedProgress: # Decide if the progress needs to be updated
            self.trackedProgress = progressed
            total = int(percent * 100)
            unprogressed = barsize - progressed
            print("\r\033[?25l\033[92m" + bar * progressed + "\033[0m" + bar * unprogressed + f" {total}", end="")
        if progressed == barsize:
            self.clean()

    def clean(self):
        print("\033[?25h", end="")
        signal(SIGINT, self.oldsignal)
    
    def signal(self, sig, frame):
        print("\033[?25h")
        self.oldsignal(sig, frame)

image

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