Skip to content

Instantly share code, notes, and snippets.

@blixt
Last active July 5, 2018 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blixt/5ea5c3eba0054917030f1938e3200f82 to your computer and use it in GitHub Desktop.
Save blixt/5ea5c3eba0054917030f1938e3200f82 to your computer and use it in GitHub Desktop.
Simple progress indicator copy/paste function for Python scripts
# -*- coding: utf-8 -*-
import sys
_last_status = None
_last_status_permille = 0
def print_status(status=None, fraction=0):
global _last_status, _last_status_permille
permille = int(fraction * 1000)
if status != _last_status:
if _last_status:
sys.stdout.write(u'\r{} ✅ \n'.format(_last_status))
sys.stdout.flush()
_last_status = status
elif permille == _last_status_permille:
return
if not status:
_last_status_permille = 0
return
_last_status_permille = permille
sys.stdout.write(u'\r{}… {:.01f}% ⏳'.format(status, fraction * 100))
sys.stdout.flush()
# Usage:
print_status('Performing slow task')
# Outputs: Performing slow task… 0.0% ⏳
print_status('Performing slow task', 0.123)
# Outputs: Performing slow task… 12.3% ⏳
print_status('Performing slow task', 0.1234)
# Outputs nothing! (Only outputs something when display needs to change.)
# Show that previous task is complete before starting another one (optional):
print_status()
# Outputs: Performing slow task ✅
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment