web status display for long running program
Larry Bates
lbates at syscononline.com
Fri Feb 25 09:37:48 EST 2005
Not exactly on point, but this is what I use in many of my
programs to show progress on long running console apps.
Larry Bates
class progressbarClass:
def __init__(self, finalcount, progresschar=None):
import sys
self.finalcount=finalcount
self.blockcount=0
#
# See if caller passed me a character to use on the
# progress bar (like "*"). If not use the block
# character that makes it look like a real progress
# bar.
#
if not progresschar: self.block=chr(178)
else: self.block=progresschar
#
# Get pointer to sys.stdout so I can use the write/flush
# methods to display the progress bar.
#
self.f=sys.stdout
#
# If the final count is zero, don't start the progress gauge
#
if not self.finalcount : return
self.f.write('\n------------------ % Progress -------------------1\n')
self.f.write(' 1 2 3 4 5 6 7 8 9 0\n')
self.f.write('----0----0----0----0----0----0----0----0----0----0\n')
return
def progress(self, count):
#
# Make sure I don't try to go off the end (e.g. >100%)
#
count=min(count, self.finalcount)
#
# If finalcount is zero, I'm done
#
if self.finalcount:
percentcomplete=int(round(100*count/self.finalcount))
if percentcomplete < 1: percentcomplete=1
else:
percentcomplete=100
#print "percentcomplete=",percentcomplete
blockcount=int(percentcomplete/2)
#print "blockcount=",blockcount
if blockcount > self.blockcount:
for i in range(self.blockcount,blockcount):
self.f.write(self.block)
self.f.flush()
if percentcomplete == 100: self.f.write("\n")
self.blockcount=blockcount
return
if __name__ == "__main__":
from time import sleep
pb=progressbarClass(8,"*")
count=0
while count<9:
count+=1
pb.progress(count)
sleep(0.2)
pb=progressbarClass(100)
pb.progress(20)
sleep(0.2)
pb.progress(47)
sleep(0.2)
pb.progress(90)
sleep(0.2)
pb.progress(100)
print "testing 1:"
pb=progressbarClass(1)
pb.progress(1)
Brian Roberts wrote:
> I have a command line Python program that sometimes takes a bit
> (several minutes) to run. I want to provide an optional method for an
> impatient user (me!) to check the status of the program. The type and
> amount of status information doesn't fit nicely into a --verbose or
> logger -- either too little or too much information at different
> points.
>
> I think an optional web page would be convenient interface. The
> Python program would listen on some port, and if queried (by me
> browsing to localhost:12345 for example) would return a pretty status
> display. Hitting reload would update the status etc.
>
> My problem is that I'm not sure how to do this:
> - I don't want to embed a full web server into the application or
> require any special PC setup.
> - I think I know how to listen on a socket, but not sure how to send
> stuff to to a web browser -- just start with <HTML>? Or like a CGI
> script with the header stuff like text/html? (I don't care if I have
> to write the HTML by hand or can use a toolkit -- not important).
> - Do I need a separate thread to listen and send the HTML? The
> application is currently single threaded. I'm confortable with
> threads, but would prefer to avoid them if possible.
>
> Or is there a better/different way of doing this? Any general advice
> or pointers to some code that already does this would be very much
> appreciated.
>
> Python 2.3, under both Linux & Windows if that makes a difference.
>
> Thanks,
> Brian.
More information about the Python-list
mailing list