loading message thingy

R

Robbie

Even though this is not very important it would be nice to get working.

Basically all I want to do is continuously write to the same line.
So I could have a little rotating text thing showing that the script is
working or display data ie, kb downloaded so far...

ATM I am just calling a clear screen every pass with a little nap, nasty.

Something along these lines:
for i in range(1000):
print "Loading... %s" % file_size
os.system("clear")
sleep(0.5)
I also tried something with \r but that never quite worked...
Also it would be nice to be able to change more than one line so I could
have 4 threads open downloading something with 4 lines each stating the
current status of each thread...
 
L

Larry Bates

Hope this code might help or at least point
you in the correct direction.

Regards,
Larry Bates
Syscon, Inc.
Title: Console (text) progress indicator class



Description:

I was finding that I needed a progress indicator for Linux and Windows
console applications that could be used to show the user that work was
progressing and how much of the total work that had been completed. I
finally broke down and wrote this class that seems to do exactly what I
wanted. Since I continue to see questions about how to write such a class on
Comp.Lang.Python, I thought I'd donate it to this Cookbook archive.

Source:

Note: Your final code will have syntax highlighting

# CLASS NAME: DLLInterface
#
# Author: Larry Bates ([email protected])
#
# Written: 12/09/2002
#
# Released under: GNU GENERAL PUBLIC LICENSE
#
#
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)





Discussion:

Programs that run (FTP downloads, database insertions, etc.) sometimes
require visual feedback to the user as to the progress of the task that is
running. This progress bar class provides an easy to implement way of
showing the percentage of completion that is updated periodically by the
program.
 
E

Eddie Corns

Robbie said:
Even though this is not very important it would be nice to get working.
Basically all I want to do is continuously write to the same line.
So I could have a little rotating text thing showing that the script is
working or display data ie, kb downloaded so far...
ATM I am just calling a clear screen every pass with a little nap, nasty.
Something along these lines:
for i in range(1000):
print "Loading... %s" % file_size
os.system("clear")
sleep(0.5)
I also tried something with \r but that never quite worked...
Also it would be nice to be able to change more than one line so I could
have 4 threads open downloading something with 4 lines each stating the
current status of each thread...
In the old days we all used to know all the ANSI control sequences off by
heart. Nowadays you have it easy with curses and the like. Not sure if
curses can be used to just use as many lines as you need rather than the whole
screen. Perhaps it's over the top but you 0nly need to do it once.

The \r trick is the most common though, maybe you should try to get that
working. Then squeeze all statuses onto 1 line eg:

File 1 File 2 File 3 File 4
23% 45% 100% 0%\r 24% etc.

Eddie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top