Welcome, guest | Sign In | My Account | Store | Cart

A progressbar utility for command line programs, very easy to use. The original code is hosted in github: https://github.com/ikame/progressbar

Python, 81 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import sys
import time

class ProgressBar(object):
    """ProgressBar class holds the options of the progress bar.
    The options are:
        start   State from which start the progress. For example, if start is 
                5 and the end is 10, the progress of this state is 50%
        end     State in which the progress has terminated.
        width   --
        fill    String to use for "filled" used to represent the progress
        blank   String to use for "filled" used to represent remaining space.
        format  Format
        incremental
    """
    def __init__(self, start=0, end=10, width=12, fill='=', blank='.', format='[%(fill)s>%(blank)s] %(progress)s%%', incremental=True):
        super(ProgressBar, self).__init__()

        self.start = start
        self.end = end
        self.width = width
        self.fill = fill
        self.blank = blank
        self.format = format
        self.incremental = incremental
        self.step = 100 / float(width) #fix
        self.reset()

    def __add__(self, increment):
        increment = self._get_progress(increment)
        if 100 > self.progress + increment:
            self.progress += increment
        else:
            self.progress = 100
        return self

    def __str__(self):
        progressed = int(self.progress / self.step) #fix
        fill = progressed * self.fill
        blank = (self.width - progressed) * self.blank
        return self.format % {'fill': fill, 'blank': blank, 'progress': int(self.progress)}

    __repr__ = __str__

    def _get_progress(self, increment):
        return float(increment * 100) / self.end

    def reset(self):
        """Resets the current progress to the start point"""
        self.progress = self._get_progress(self.start)
        return self


class AnimatedProgressBar(ProgressBar):
    """Extends ProgressBar to allow you to use it straighforward on a script.
    Accepts an extra keyword argument named `stdout` (by default use sys.stdout)
    and may be any file-object to which send the progress status.
    """
    def __init__(self, *args, **kwargs):
        super(AnimatedProgressBar, self).__init__(*args, **kwargs)
        self.stdout = kwargs.get('stdout', sys.stdout)

    def show_progress(self):
        if hasattr(self.stdout, 'isatty') and self.stdout.isatty():
            self.stdout.write('\r')
        else:
            self.stdout.write('\n')
        self.stdout.write(str(self))
        self.stdout.flush()


if __name__ == '__main__':
    p = AnimatedProgressBar(end=100, width=80)

    while True:
        p + 5
        p.show_progress()
        time.sleep(0.1)
        if p.progress == 100:
            break
    print #new line

Python Progressbar

A progressbar utility for command line programs. Progressbar is a Python module which contains two class so far:

ProgressBar
AnimatedProgressBar

ProgressBar class implements all the base stuffs that makes progress bars work as they does and admit some basic customization. AnimatedProgressBar class extends ProgressBar to allow you to use it straightforward in your scripts. By default the AnimatedProgressBar sends the output to sys.stdout but you can change this passing a the stdout keyword parameter which must be a file-like object.

Here is some basic usage with the default options:

>>> from progressbar import ProgressBar
>>> p = ProgressBar()
>>> print p
[>............] 0%
>>> p + 1
>>> print p
[=>...........] 10%
>>> p + 9
>>> print p
[============>] 0%

And here another example with different options:

>>> from progressbar import ProgressBar
>>> custom_options = {
...     'end': 100, 
...     'width': 20, 
...     'fill': '#',
...     'format': '%(progress)s%% [%(fill)s%(blank)s]'
... }
>>> p = ProgressBar(**custom_options)
>>> print p
0% [....................]
>>> p + 5
>>> print p
5% [#...................]
>>> p + 9
>>> print p
100% [####################]

Finally, a real example where I had to use it:

>>> import ftplib
>>> import progressbar
>>>
>>> ftp = ftplib.FTP('ftp.myserver.com', 'user', 'passwd')
>>> filesize = ftp.size('path/to/remotefile.zip')
>>> progress = progressbar.AnimatedProgressBar(end=filesize, width=50)
>>>
>>> with open('localfile.zip', 'w') as f:
>>>     def callback(chunk):
>>>         f.write(chunk)
>>>         progress + len(chunk)
>>>
>>>         # Visual feedback of the progress!
>>>         progress.show_progress()
>>>     
>>>     ftp.retrbinary('RETR path/to/remotefile.zip', callback)

9 comments

Herbert 12 years, 6 months ago  # | flag

it's useful, I like it:)

Michal Niklas 12 years ago  # | flag

It works with Jython! Thanks for useful code.

Michal Niklas 12 years ago  # | flag

I noticed that AnimatedProgressBar blinks, so I changed show_progress() method to check if progress indicator changed:

def __init__(self, *args, **kwargs):
    super(AnimatedProgressBar, self).__init__(*args, **kwargs)
    self.stdout = kwargs.get('stdout', sys.stdout)
    self.last_indicator = ''

def show_progress(self):
    indicator = str(self)
    if indicator == self.last_indicator:
      return
    if hasattr(self.stdout, 'isatty') and self.stdout.isatty():
        self.stdout.write('\r')
    else:
        self.stdout.write('\n')
    self.stdout.write(indicator)
    self.stdout.flush()
    self.last_indicator = indicator

Thanks a lot Michal. Also, send me a pull request in github if you wants to.

George 11 years, 8 months ago  # | flag

Hello Sir !,

Thank you very much for the useful code. I usually use for a FTP download. It show me 98 lines : [>..................................................] 0%[>..................................................] 1%[=>.................................................] 2%[=>.................................................] ... 98%[=================================================>.] It is possible to show me only one line and change(increment) only the progress value? Because I want to show the progress bar in a tkMessageBox.

Thank you very much in advance

Anler Hernández Peral (author) 11 years, 8 months ago  # | flag

Hi George,

That's the way it should works! It must be a bug, can you give me more details about your platform? I'll try to fix it as soon as possible.

Thanks

George 11 years, 8 months ago  # | flag

Hello,

I work on Windows 7, 32 bytes and I run code from Komodo Edit 7.0. My code is:

import ftplib import progressbar

ftp = ftplib.FTP('....', '.....', '.....') filesize = ftp.size('NestPrep.zip') progress = progressbar.AnimatedProgressBar(end=filesize, width=50)

with open('NestPrep.zip', 'wb') as f: def callback(chunk): f.write(chunk) progress + len(chunk)

    # Visual feedback of the progress!
    progress.show_progress()

ftp.retrbinary('RETR NestPrep.zip', callback)

Output: [>..................................................] 0% [>..................................................] 1% [=>.................................................] 2% ..... [================================================>..] 97% [=================================================>.] 98% [=================================================>.] 99%

I want to use progress bar like this:

messagebox.showinfo(title="Info", message="Download progress: {0}".format(progress.show_progress()))

Thank you very much Sir !

Anler Hernández Peral (author) 11 years, 8 months ago  # | flag

Hi George, the progress bar internally try to detect if is running inside a terminal otherwise it prints the output as you see. Can you try to run your code from the terminal directly instead of using Komodo to see if works as expected?

Thanks

George 11 years, 8 months ago  # | flag

Yes work fine when run from Command Prompt. Now I understand that to show the progress bar into the messagebox it's very hard. It's necessary to change the code from class 'AnimatedProgressBar'.