Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python command line progress bar in less than 10 lines of code.
# The MIT License (MIT)
# Copyright (c) 2016 Vladimir Ignatev
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the Software
# is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
# OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import sys
def progress(count, total, status=''):
bar_len = 60
filled_len = int(round(bar_len * count / float(total)))
percents = round(100.0 * count / float(total), 1)
bar = '=' * filled_len + '-' * (bar_len - filled_len)
sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percents, '%', status))
sys.stdout.flush() # As suggested by Rom Ruben (see: http://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console/27871113#comment50529068_27871113)
@vladignatyev

This comment has been minimized.

Copy link
Owner Author

vladignatyev commented Dec 28, 2015

Here is how it looks like: progress_bar_demo

@vladignatyev

This comment has been minimized.

Copy link
Owner Author

vladignatyev commented Dec 28, 2015

Usage:

import time 


total = 1000
i = 0
while i < total:
    progress(i, total, status='Doing very long job')
    time.sleep(0.5)  # emulating long-playing job
    i += 1
@FlorianSW

This comment has been minimized.

Copy link

FlorianSW commented Feb 26, 2016

I plan to have this code in a MediaWiki extension (https://gerrit.wikimedia.org/r/#/c/265529/2/captcha.py). However, the code is currently not licensed under a free software license (preferably a license which is compatible with GPL 2). Could you add such a license so I can use the code inside of this extension? :) Thanks!

@vladignatyev

This comment has been minimized.

Copy link
Owner Author

vladignatyev commented Feb 26, 2016

Added MIT license, it's GPL 2 compatible and will work for proprietary software too. Check my snippet, please! Is it okay?

@FlorianSW

This comment has been minimized.

Copy link

FlorianSW commented Mar 3, 2016

I'm sure, that's fine, thanks! :) And please excuse my late answer :(

@vladignatyev

This comment has been minimized.

Copy link
Owner Author

vladignatyev commented Mar 3, 2016

@FlorianSW no problem! thanks for your contribution :) Never thought before that it's necessary for Gists to have some public license attribution in order to use them by others anywhere in theirs own projects.

@satish-annigeri

This comment has been minimized.

Copy link

satish-annigeri commented Jul 4, 2017

Thanks @vladignatyev, I was looking for just such code with no dependencies (other than built-in modules) and worked extremely well for me.

@datatalking

This comment has been minimized.

Copy link

datatalking commented Aug 24, 2017

I plan to use this code for a program I am running and will use your license in this.

How do I figure out how to know how I plan for len of my code so this program know, ok the program takes 12 seconds;
len of progress bar = 12 etc.

Is there a variable in python that tells me how it takes to run my program as it changes.

@errard70101

This comment has been minimized.

Copy link

errard70101 commented Oct 5, 2017

sys.stdout.write('\r[%s] %s%s ...%s\r' % (bar, percents, '%', status))

Should add \r in the beginning so that there will be only one line of the progress bar.

Nice work.

@yifeikong

This comment has been minimized.

Copy link

yifeikong commented Oct 6, 2017

It works, but the cursor keeps blink between the start and the end of line, is it fixable?

@mike-zamayias

This comment has been minimized.

Copy link

mike-zamayias commented Nov 25, 2017

sys.stdout.write('\r[%s] %s%s ...%s\r' % (bar, percents, '%', status))
Should add \r in the beginning so that there will be only one line of the progress bar.

@errard70101 I did so, but I still get a new line whenever the percentage is increasing.

If I wanted go for 100% to 0%, what modifications would I have to do?

@mike-zamayias

This comment has been minimized.

Copy link

mike-zamayias commented Nov 25, 2017

sys.stdout.write('\r[%s] %s%s ...%s\r' % (bar, percents, '%', status))
Should add \r in the beginning so that there will be only one line of the progress bar.

@errard70101 I did so, but I still get a new line whenever the percentage is increasing.

Never mind, I made that work.

@Juliano051

This comment has been minimized.

Copy link

Juliano051 commented Jan 19, 2018

Very good. I was almost reinventing the wheel when I found your job. Thanks!

@HCrane

This comment has been minimized.

Copy link

HCrane commented Jan 31, 2018

Awesome progressbar!

@gkiryaziev

This comment has been minimized.

Copy link

gkiryaziev commented Feb 6, 2018

I've got always 99.9%, not 100%, why?

UPD:
increment have to be before progress bar

i += 1
progress(i, total, status='Doing very long job')
time.sleep(0.5) # emulating long-playing job

@xsponse

This comment has been minimized.

Copy link

xsponse commented Mar 6, 2018

@gkiryaziev
If you change "percents = round(100.0 * count / float(total), 1)" to "percents = round(100.1 * count / float(total), 1)", it will display 100%

@Niraj288

This comment has been minimized.

Copy link

Niraj288 commented Apr 25, 2018

Thanks

@davincif

This comment has been minimized.

Copy link

davincif commented May 6, 2018

ProgressBar
I've made a slight improvement that really came in handy in my case. Now I can add multiply loading bars.
Also there a tiny bug, if the size of the previous bar is greater than the next, there'll the trash characters printed after your bar, I solved that.

@jolespin

This comment has been minimized.

Copy link

jolespin commented May 9, 2018

Can't this to work in a jupyter notebook.

@joachimesque

This comment has been minimized.

Copy link

joachimesque commented Jun 23, 2018

If your status has variable length, you might want to insert the character for “erase to end of line” just before the first write, line 30: sys.stdout.write("\033[K")

@a8888951

This comment has been minimized.

Copy link

a8888951 commented Jul 2, 2018

very good! I take it. thanks

@pablosolar

This comment has been minimized.

Copy link

pablosolar commented Jul 4, 2018

Hello,
I can check that if the long job tries to print something, the progress bar breaks and starts to print in a new line. Is it possible to fix it???

@berensn

This comment has been minimized.

Copy link

berensn commented Nov 8, 2018

@gkiryaziev, @xsponse in the while loop that @vladignatyev used for demonstration, change the comparison operator to <= from < and it will show 100%

@biriyaniboy

This comment has been minimized.

Copy link

biriyaniboy commented Dec 12, 2018

Thanks for the progress bar. Will be using it on my own projects from now on!

@mikroskeem

This comment has been minimized.

Copy link

mikroskeem commented Jan 20, 2019

epic, many thanks.

@arbabnazar

This comment has been minimized.

Copy link

arbabnazar commented Mar 23, 2019

excellent

@teknopaul

This comment has been minimized.

Copy link

teknopaul commented Aug 9, 2019

If you put the percentage at the end of the line in windows you get visual feedback in the task bar.

echo -ne '\r [---] 10%'
sleep 1
echo -ne '\r [---] 20%'
sleep 1
echo -ne '\r [---] 30%'
sleep 1
@talltalltaylor

This comment has been minimized.

Copy link

talltalltaylor commented Dec 12, 2019

awesome, works like a charm

@MarcDirven

This comment has been minimized.

Copy link

MarcDirven commented Jan 10, 2020

I made a small improvement because I couldn't get it working, it wasn't printing anything cause of the \r character:

def progres(count, total, status='', bar_len=60):
    filled_len = int(round(bar_len * count / float(total)))

    percents = round(100.0 * count / float(total), 1)
    bar = '=' * filled_len + '-' * (bar_len - filled_len)

    fmt = '[%s] %s%s ...%s' % (bar, percents, '%', status)
    print('\b' * len(fmt), end='')  # clears the line
    sys.stdout.write(fmt)
    sys.stdout.flush()
@vladignatyev

This comment has been minimized.

Copy link
Owner Author

vladignatyev commented Jan 10, 2020

@MarcDirven thank you, for your contribution! Could you please tell more about your setup? What OS and terminal you use to reproduce the problem?

@vladignatyev

This comment has been minimized.

Copy link
Owner Author

vladignatyev commented Jan 10, 2020

By the way, 28th of December, 2020 will be the celebration of 5-years progress.py snippet. :D

@threewordphrase

This comment has been minimized.

Copy link

threewordphrase commented Jan 25, 2020

Genius, I love it!

@chrisgoodwins

This comment has been minimized.

Copy link

chrisgoodwins commented Feb 26, 2020

@vladignatyev Great snippet of code!! I will use this many times over.
As far as getting the progress bar to reach and display 100% -- I found that it works if you either start the counter value at 1, or increment the counter in the loop before calling the progress function (rather than incrementing after the function)

@cikal

This comment has been minimized.

Copy link

cikal commented May 9, 2020

simple and nice, thank you @vladignatyev for this snippet..

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.