Python Forum

Full Version: console progressbars
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Just an example of a few different types of progress bars

import time, sys

if sys.version[0] == '2':
    range = xrange
    
def flush():
    if sys.version[0] == '2':
        sys.stdout.flush()


def progressbar_num():
    for num in range(101):
        time.sleep(.1)
        sys.stdout.write("\r{}%".format(num))    # or print >> sys.stdout, "\r%d%%" %i,
        flush()
    print('')

def progressbar_disp():
    display_char = '#'
    for num in range(101):
        time.sleep(.1)
        sys.stdout.write("\r[{0}] {1}%".format(int(num/3)*display_char, num))
        flush()
    print('')

def progressbar_disp_full():
    display_char = '#'
    incomplete_char = ' '
    for num in range(101):
        spacer = int(33-int(num/3)) * incomplete_char
        filler = int(num/3)*display_char
        time.sleep(.1)
        sys.stdout.write("\r[{0}{1}] {2}%".format(filler, spacer, num))
        flush()
    print('')
    
progressbar_num()
progressbar_disp()
progressbar_disp_full()
how are they used?

how about print() instead of print('')?
this code below is for version python v2?


>>if sys.version[0] == '2':
(Nov-30-2017, 07:54 AM)issac_n Wrote: [ -> ]this code below is for version python v2? >>if sys.version[0] == '2':
yes, this is to check if python version is 2 and if it is - use xrange (it overwrites range with xrange)
if bytes == str: range = xrange
possible make it more simple?
Progress_bar = ttk.Progressbar(master1,orient='horizontal',length=286, mode = 'determinate')
Progress_bar.start(30)
Progress_bar.XXX# Full green bar
Progress_bar.stop()
i do have question here, how to make it FULL GREEN BAR?
Progress_bar.XXX# Full green bar
i think tkinter to change colors was a littler trickier
import Tkinter as tk
import ttk as ttk
root = tk.Tk()
frame = tk.Frame(root)
frame.grid()
s = ttk.Style()
s.theme_use('clam')
s.configure("red.Horizontal.TProgressbar", foreground='red', background='red')
ttk.Progressbar(frame, style="red.Horizontal.TProgressbar", orient="horizontal", length=600, mode="determinate", maximum=4, value=1).grid(row=1, column=1)
frame.pack()
(Dec-04-2017, 03:02 AM)issac_n Wrote: [ -> ]possible make it more simple?
Progress_bar = ttk.Progressbar(master1,orient='horizontal',length=286, mode = 'determinate')
Progress_bar.start(30)
Progress_bar.XXX# Full green bar
Progress_bar.stop()
i do have question here, how to make it FULL GREEN BAR?
Progress_bar.XXX# Full green bar

I found it ..
Progress_bar["value"]=100 
A little simple pure text based progress bar.

In my case the output pane will not do a true carriage return(\r), but will do an additional line feed (\r => \r\n).
(The picked Pbar characters are related to the proportional font my output pane is using)

+(Updated to second version. Behaving overall as intended.)
+(Updated to third version. Behaving finally as intended. +(minor bug fix).)
(Might not truly classify as simple anymore. Well, code wise I guess. ... Was fun little personal project.)

class Pbar():

	def __init__(self, loops, title='', barlen=80, chr1='~', chr2='#'):

		if isinstance(loops, int): pass
		elif isinstance(loops, float): loops = int(loops)
		else: loops = len(loops)

		if not isinstance(barlen, int):
			barlen = int(barlen)

		self._loops = loops if loops > 1 else 1
		self._title = str(title) if str(title) else ''
		self._barlen = barlen if barlen > 11 else 11
		## True minimale bar length is "ciel(set-value/2)". Or 6 when set to 11 or 12 for example.

		if self._loops < self._barlen:
			self._step = self._barlen//self._loops
			self._barlen = self._loops * self._step
		else:
			self._step = 1

		self._loops -= 0

		if self._title:
			self._title = '[ ' + self._title + ' ]'

		self._chr1 = chr1[:1]
		self._chr2 = chr2[:1]

		self._count = 0
		self._progress = 0
		self._done = False

		self._pre_bar()


	def _pre_bar(self):

		if self._title:
			## General title centering for proportional fonts.
			## For monospaced fonts "offset*1" should do the trick.
			offset = self._barlen//2 - len(self._title)//2
			print(''.rjust(offset*3, ' '), self._title)

		print(''.rjust(self._barlen, self._chr1))


	def loop(self):

		if self._done: return

		self._count += 1

		new_progress = int(round(self._count / self._loops * self._barlen))

		if new_progress > self._progress:
			self._progress = new_progress
			print(''.rjust(self._step, self._chr2), end='')


	def done(self):

		self._done = True

		print()
		print()



def pbar_example_run():

	import time
	import random

	for x in range(6):

		barlen = int(random.random() * 15) * x
		loop = int(random.random() * 23) * x + 1

		title = 'Test run: Loop ' + str(loop) + ' over Bar ' + str(barlen) + '.'
		_time = 1/loop if loop > 0 else 1

		pbar = Pbar(loop, title=title , barlen=barlen)
		for i in range(loop):
			time.sleep(_time)
			pbar.loop()
		pbar.done()



if (__name__ == "__main__"):
    pbar_example_run()
Output:
[ Test run: Loop 111 over Bar 6. ] ~~~~~~~~~~~ ########### [ Test run: Loop 6 over Bar 111. ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ############################################################################################################
(Python 3.8.5)