Here is a little class that lets you present percent complete information in the form of a progress bar using the '#' character to represent completed portions, space to represent incomplete portions, and the actual percent done (rounded to integer) displayed in the middle:
[############# 33% ]
When you initialize the class, you specify the minimum number (usually 0), the maximum number (your file size, for example), and the number of characters wide you would like the progress bar to be. Note that width includes the brackets [] on the ends of the progress bar as well.
You'd probably want to use this in conjuction with the curses module, or something like that so you can over-write the same portion of the screen to make your updates 'animated'.
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 | class progressBar:
def __init__(self, minValue = 0, maxValue = 10, totalWidth=12):
self.progBar = "[]" # This holds the progress bar string
self.min = minValue
self.max = maxValue
self.span = maxValue - minValue
self.width = totalWidth
self.amount = 0 # When amount == max, we are 100% done
self.updateAmount(0) # Build progress bar string
def updateAmount(self, newAmount = 0):
if newAmount < self.min: newAmount = self.min
if newAmount > self.max: newAmount = self.max
self.amount = newAmount
# Figure out the new percent done, round to an integer
diffFromMin = float(self.amount - self.min)
percentDone = (diffFromMin / float(self.span)) * 100.0
percentDone = round(percentDone)
percentDone = int(percentDone)
# Figure out how many hash bars the percentage should be
allFull = self.width - 2
numHashes = (percentDone / 100.0) * allFull
numHashes = int(round(numHashes))
# build a progress bar with hashes and spaces
self.progBar = "[" + '#'*numHashes + ' '*(allFull-numHashes) + "]"
# figure out where to put the percentage, roughly centered
percentPlace = (len(self.progBar) / 2) - len(str(percentDone))
percentString = str(percentDone) + "%"
# slice the percentage into the bar
self.progBar = self.progBar[0:percentPlace] + percentString +
self.progBar[percentPlace+len(percentString):]
def __str__(self):
return str(self.progBar)
|
I used this along with curses to display a progress bar while copying a large file over the network. It was better than looking at an apparently "frozen" screen for a long time telling the user that the copy is really happening.
Of course, nothing ties this progress bar to file copy progress. You can use it for anything that takes a while to do and progress can be measured.
How do you implement this into script? How can i implement this into my script?
If you want to ACTUALLY use it, you're going to have to think for yourself.
Best not to print it every iteration. It can be a substantial performance hit to print the bar every iteration, whether it changes or not. At 100 iterations, like Josiah's example, it changes every iteration, so it won't matter. But if you have more than a few thousand, it's worth checking and not printing an unchanged bar:
In this example (which is close to a real use I have, where I'm processing a file with a little over a million lines), the wall-clock time on my system consistently drops by about 80% (from over 2 minutes down to 24 seconds).
Also, remember to print a newline after your last print of the progress bar, or your next print statement will overlay it:
curses version... This was a great example. Thanks. I'm very new to curses programming, but I thought I'd try implementing this in a curses panel.
This gives me the option of placing (or even moving) the panel to specific parts of the screen, independent of other UI elements in the curses window. Pretty cool - python makes a lot of things easy.
This could be implemented even more easily using pythondialog (http://pythondialog.sourceforge.net/)
which does all the work for you, but pythondialog is based on "dialog" (http://dickey.his.com/dialog/dialog.html)
which is GPL rather than LGPL so it restricts what you can do with it within a commercial setting.
I was going to post my "cursified" version of this example code, but it's getting badly garbled and truncated (or "Preview" is broken.)
I guess it'll have to suffice to say that it can be done easily with the standard python curses modules.
How to use this code for a file remotely. I want to incorporate this class into my program which does the work of downloading debian based packages from the net. How to use it in that case ???
Minor improvements. I modified the class a little, mainly added self._old_pbar (initialized to '') and added draw() method:
I also created a new class AsciiProgressBar which derives from ProgressBar (draw() is an abstract method in the base class). I can send the code if somebody's interested.
Interested? Am I!? Of course I am interested! Please paste the AsciiProgressBar class here or email it to walterbrunswick@sympatico.ca. Thank you!
Instead... Instead send the entire code of your module, please. Your 'draw' function is not compatible with the existing code.
progress bar using curses. it was nice to see a code that works efficiently.. but how to implement this progress bar code onto curses window with color..
Intested in your progress bar. Please paste it here, or send to zz2liu@yahoo.com
cursified version. It was really good eg ,but its better to see the cursified versions of this.pls paste that code here...
using curses. hey pls paste code of cursified progressbar.. it was really cool to see this, waiting for curses display.
moving progressbar in curses. this is the code for cursified progressbar.. import curses if __name__ == "__main__": stdscr=curses.initscr() curses.start_color()
# s.bkgdset(ord(' '), curses.color_pair(4)) s.box()
Slightly improved. What a great class!
I tried to avoid the use of string concatenations (especially useful for quick updating progress bars; or when you don't have control over the update rate), and slightly modified the class (and added docstrings and a __call__ :p).
Just thought I'd share.
(comment continued...)
(...continued from previous comment)
dialog is LGPL. I reviewed it last year, found that I had replaced all of the original code (aside from the boilerplate license comments), and released it with an LGPL license.