Python Forum
is there an easy way to add a progress bar/counter to an existing script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is there an easy way to add a progress bar/counter to an existing script
#1
I have a script that is complete, working well
but I'd like to add a progress bar or a step couter of some sort without having to redo teh entire script

I'm having a problem finding a progress bar I can do this with

basically I'd like to create a definition or a loop and add an indicator every so often inside the script to move it forward as the script continues

I know I can already just use print('message') every so often... but I'd like to make it appear either in a tkinter window or in the cmd window that appears without having it show up as 20 lines printed


Thanks if you can point me in the right direction
Reply
#2
tkinter
https://riptutorial.com/tkinter/example/...rogressbar

console/terminal
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()
Whatever your current code is, you would increment the progressbar at the point at which it needs. I am not sure if your downloading, loading resources, etc.

This would be an example of downloading using a percentage progressbar
Recommended Tutorials:
Reply
#3
I'm not downloading or loading anything


I just want a bar that increase as it steps thru the script

my script is set up like this

step 1
do some of the script
step 2
do more of the script
step 3
do more of the script


I just want it to do this

step 1
do some of the script
Increment progress bar
step 2
do more of the script
Increment progress bar
step 3
do more of the script
Increment progress bar

what I dont understand is in your example
you didnt show me how to increment that progress bar as the script goes on

what command do I type to make it increase by a value
Reply
#4
If you want to manually insert the value throughout your script

import sys
import time


def progress(count, suffix='', total=100, complete='=', incomplete='-', bar_len=60):
    filled_len = int(round(bar_len * count / float(total)))

    percents = round(100.0 * count / float(total), 1)
    bar = complete * filled_len + incomplete * (bar_len - filled_len)

    sys.stdout.write(f'[{bar}] {percents}% ...{suffix}\r')
    sys.stdout.flush()  
    
def task():
    time.sleep(1)
    
def script():
    progress(0,'running task 1')
    task()
    progress(20,'running task 2')
    task()
    progress(40,'running task 3')
    task()
    progress(60,'running task 4')
    task()
    progress(80,'running task 5')
    task()
    progress(100, '   complete   ')
    
    
script()
Recommended Tutorials:
Reply
#5
can I break these up into separate defeinition?

def script():
    progress(0,'running task 1')
    task()

def script2():
    progress(20,'running task 2')
    task()

def script3():
    progress(40,'running task 3')
    task()
and so forth and so on?

and then I just place
script()
do more stuff
script2()
do more stuff
script3()
do more stuff...
and so forth and so on?
Reply
#6
You dont need script() or task() functions at all. It was just an illustration of how to use it. Script being your entire program, and task being any function within your program that does something as the purpose of the program's existance

Put progress(0) in the beginning of your script, and progress(100) at the end of it. Then divide 100 by your programs' number of tasks and put progress(THAT_NUM) after task 1, program(THAT_NUM*2) after task 2, program(THAT_NUM*3) after task 3, etc.

Quote:
    progress(0,'running task 1')
    task()
    progress(20,'running task 2')
    task()
    progress(40,'running task 3')
    task()
    progress(60,'running task 4')
    task()
    progress(80,'running task 5')
    task()
    progress(100, '   complete   ')
In this case there are 5 tasks throughout the entire program progressing the bar 20% each time. 100/5=20 100/number of tasks = percentage to increase after each task
Recommended Tutorials:
Reply
#7
tqdm have i used before,it's easy to drop into exiting code.
tqdm Wrote:tqdm works on any platform (Linux, Windows, Mac, FreeBSD, NetBSD, Solaris/SunOS),
in any console or in a GUI, and is also friendly with IPython/Jupyter notebooks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python beginner that needs an expression added to existing script markham 1 703 Sep-04-2023, 05:24 AM
Last Post: Pedroski55
  Progress bar bnadir55 1 1,825 Apr-11-2022, 01:52 PM
Last Post: deanhystad
  Progress Indicator for Xmodem 0.4.6 KenHorse 1 1,977 Jan-30-2021, 07:12 PM
Last Post: bowlofred
  Need help creating complex loop around existing script CephloRhod 5 2,772 Apr-16-2020, 01:23 PM
Last Post: deanhystad
  How can I add a progress bar for my software? aquerci 8 3,764 Nov-16-2019, 04:20 PM
Last Post: aquerci
  wget progress bar anasrocks 1 4,726 Jun-06-2019, 03:12 PM
Last Post: heiner55
  Progress Finished Question malonn 32 17,523 May-23-2018, 02:43 AM
Last Post: malonn
  Seeking feedback on my script-in-progress league55 2 2,651 Feb-12-2018, 03:03 PM
Last Post: league55
  how to progress with py jakegold98 1 2,630 Dec-05-2017, 02:58 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020