Posts: 7
Threads: 3
Joined: Jun 2019
Jun-09-2019, 01:36 AM
(This post was last modified: Jun-09-2019, 01:40 AM by Soundtechscott.)
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
Posts: 5,151
Threads: 396
Joined: Sep 2016
Jun-09-2019, 02:09 AM
(This post was last modified: Jun-09-2019, 02:09 AM by metulburr.)
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
from __future__ import division, absolute_import, print_function, unicode_literals
import sys
import os
if sys.version_info >= (3,):
import urllib.request as urllib2
import urllib.parse as urlparse
else:
import urllib2
import urlparse
def download_file(url, desc=None):
u = urllib2.urlopen(url)
scheme, netloc, path, query, fragment = urlparse.urlsplit(url)
filename = os.path.basename(path)
if not filename:
filename = 'downloaded.file'
if desc:
filename = os.path.join(desc, filename)
with open(filename, 'wb') as f:
meta = u.info()
meta_func = meta.getheaders if hasattr(meta, 'getheaders') else meta.get_all
meta_length = meta_func("Content-Length")
file_size = None
if meta_length:
file_size = int(meta_length[0])
print("Downloading: {0} Bytes: {1}".format(filename, file_size))
file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
status = "{0:16}".format(file_size_dl)
if file_size:
status += " [{0:6.2f}%]".format(file_size_dl * 100 / file_size)
status += chr(13)
print(status, end="")
print()
return filename
url = 'https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tar.xz'
url = 'ftp://speedtest:[email protected]/test100Mb.db'
download_file(url)
Recommended Tutorials:
Posts: 7
Threads: 3
Joined: Jun 2019
Jun-09-2019, 02:54 AM
(This post was last modified: Jun-09-2019, 03:04 AM by Soundtechscott.)
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
Posts: 5,151
Threads: 396
Joined: Sep 2016
Jun-09-2019, 04:50 AM
(This post was last modified: Jun-09-2019, 04:50 AM by metulburr.)
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:
Posts: 7
Threads: 3
Joined: Jun 2019
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?
Posts: 5,151
Threads: 396
Joined: Sep 2016
Jun-09-2019, 05:20 AM
(This post was last modified: Jun-09-2019, 05:20 AM by metulburr.)
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:
Posts: 7,313
Threads: 123
Joined: Sep 2016
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.
|