Skip to content

Instantly share code, notes, and snippets.

@blixt blixt/progress.py
Last active Jul 5, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.