I'm having a rough time trying to create threads inside a tkinter GUI. To give some background on what I would like the code to do. This is a GUI that when the log buttons are pressed (e.g. log_1). I'd like to send a continuous command to fire a laser and eventually (after I tackle the current problem) log the subsequent parameters.
However, my issue is that I have 5 lasers needing to be fired at the same time (or very close to) and the more lasers I have in the logging state ON the more the time delay between the lasers firing. I guess this is an issue because my: fire_all() function in conjunction with the window after(100, fire_all) is not a threaded process so having more lasers firing results in more of a time delay between the onset of the command to fire.
My code is below. Does anyone have any suggestions how to implement this properly?
# start the GUI
import tkinter as tk
window = tk.Tk()
def fire_astrum(dev): # N = 100 # no of points in stream array, pulse_count = int(1) # no of desired pulses in pulse train #command is command volatge
#dev,ana_out,ana_in,N,pulse_count,pulse_on,pulse_off,command
#dev = 'Dev1'
ana_out = 'ao0'
ana_in = "ai0:1"# for non consecutive channels use: "Dev0/ai0:Dev0/ai4"
N = 100
pulse_count = 10
pulse_on = 45
pulse_off = 15
command = 3.5
channel = dev +'/' + ana_out
tot = dev + "/" + ana_in
duty = pulse_on/(pulse_on+pulse_off) # duty cycle in %
array_on = int(N*duty) # on values in array
array_off = int(N-array_on) # off values in array
samples = np.append(command*np.ones(array_on),np.zeros(array_off)) # command is command voltage (IPROG), sets up stream array to send to NI box
curr = np.empty((2,pulse_count*len(samples))) # Empty arrays for IMON,VMON
error = np.uint32(np.empty((3,pulse_count*len(samples)))) # # Empty arrays for Digital inputs with pull-up resistors (READY, ERR1 & ERR2)
#sample_clock = '/'+dev+'/ai/SampleClock'
arm_laser(dev,'on') # Sends 5V to Astrum to arm laser diode
time.sleep(0.3) # wait for 300ms before data stream
with nidaqmx.Task() as writeTask, nidaqmx.Task() as readcurr, nidaqmx.Task(): # as digitalread:#, nidaqmx.Task() as readvolt:
writeTask.ao_channels.add_ao_voltage_chan(channel,units = Volts)
writeTask.ao_channels.all.ao_max = 4 # Hard clamp Limit to 4V to protect laser diode
writeTask.ao_channels.all.ao_min = 0 # Set baseline to 0V (ground loop protection)
readcurr.ai_channels.add_ai_voltage_chan(tot, units = Volts,terminal_config = TerminalConfiguration.RSE) # define differntial NRE connfig of channels
#print(read_curr.ai_term_cfg)
#digitalread.di_channels.add_di_chan(dev + '/port0/line0:2',line_grouping=LineGrouping.CHAN_PER_LINE)
#writeTask.triggers.start_trigger.cfg_dig_edge_start_trig('/' + dev +'/' + terminal) #Setting the trigger on the analog input
writeTask.timing.cfg_samp_clk_timing(rate=(array_on/N)*1/pulse_on*1000*N,sample_mode= nidaqmx.constants.AcquisitionType.FINITE , samps_per_chan= pulse_count*len(samples))
readcurr.timing.cfg_samp_clk_timing(rate=(array_on/N)*1/pulse_on*1000*N, sample_mode= nidaqmx.constants.AcquisitionType.FINITE , samps_per_chan= pulse_count*len(samples))
#digitalread.timing.cfg_samp_clk_timing(rate=(array_on/N)*1/pulse_on*1000*N, sample_mode= nidaqmx.constants.AcquisitionType.FINITE , samps_per_chan= pulse_count*len(samples))
reader_curr = AnalogMultiChannelReader(readcurr.in_stream)
#digital = DigitalMultiChannelReader(digitalread.in_stream)
writer = AnalogSingleChannelWriter(writeTask.out_stream, auto_start=True)
writer.write_many_sample(samples)
time_vec = np.linspace(0, (pulse_on+pulse_off)*pulse_count, num=len(curr[0]), endpoint=True)
reader_curr.read_many_sample(data = curr,number_of_samples_per_channel = pulse_count*len(samples), timeout = nidaqmx.constants.WAIT_INFINITELY)# read from DAQ
#digital.read_many_sample_port_uint32(data = error,number_of_samples_per_channel = pulse_count*len(samples), timeout = nidaqmx.constants.WAIT_INFINITELY)# read from DAQ
curr = np.around(curr, 6) # Round all values to 6 decimals to avoid overflow
error = np.around(error, 6) # Round all values to 6 decimals to avoid overflow
print("The channels linked to WriteTask are: ")
for i in writeTask.ao_channels:
print(i)
print("The channels linked to ReadTask are: ")
for j in readcurr.ai_channels:
print(j)
################# Astrum parameters #########################
imon = curr[0]
vmon = curr[1]
ready = error[0]
err1 = error[1]
err2 = error[2]
trigger = np.tile(samples,pulse_count)
###############################################################
arm_laser(dev,'off')
###############################################################
V = np.max(vmon)
I = np.max(imon)
#for j in range(9):
laser_1[6].config(text = V)
laser_1[7].config(text = I)
def log1():
if log_1.config('text')[-1] =='ON':
log_1.config(text='OFF', bg="red",activebackground="red",textvariable=0)
print(log_1.config('textvariable')[-1])
else:
log_1.config(text='ON', bg="green",activebackground="green",textvariable=1)
print(log_1.config('textvariable')[-1])
def log2():
if log_2.config('text')[-1] =='ON':
log_2.config(text='OFF', bg="red",activebackground="red",textvariable=0)
print(log_2.config('textvariable')[-1])
else:
log_2.config(text='ON', bg="green",activebackground="green",textvariable=1)
print(log_2.config('textvariable')[-1])
def log3():
if log_3.config('text')[-1] =='ON':
log_3.config(text='OFF', bg="red",activebackground="red",textvariable=0)
print(log_3.config('textvariable')[-1])
else:
log_3.config(text='ON', bg="green",activebackground="green",textvariable=1)
print(log_3.config('textvariable')[-1])
def log4():
if log_4.config('text')[-1] =='ON':
log_4.config(text='OFF', bg="red",activebackground="red",textvariable=0)
print(log_4.config('textvariable')[-1])
else:
log_4.config(text='ON', bg="green",activebackground="green",textvariable=1)
print(log_4.config('textvariable')[-1])
def log5():
if log_5.config('text')[-1] =='ON':
log_5.config(text='OFF', bg="red",activebackground="red",textvariable=0)
print(log_5.config('textvariable')[-1])
else:
log_5.config(text='ON', bg="green",activebackground="green",textvariable=1)
print(log_5.config('textvariable')[-1])
##### logging button lasers ###########
log_1 = tk.Button(top_frame, width=8 , bd=4, text = 'OFF', command = log1, bg = "red")
log_1.grid(row = 2, column = 8)
log_2 = tk.Button(top_frame, width=8 , bd=4, text = 'OFF', command = log2, bg = "red") #, onvalue=1, offvalue=0)
log_2.grid(row = 3, column = 8)
log_3 = tk.Button(top_frame, width=8 , bd=4, text = 'OFF', command = log3, bg = "red") #, onvalue=1, offvalue=0)
log_3.grid(row = 4, column = 8)
log_4 = tk.Button(top_frame, width=8 , bd=4, text = 'OFF', command = log4, bg = "red") #, onvalue=1, offvalue=0)
log_4.grid(row = 5, column = 8)
log_5 = tk.Button(top_frame, width=8 , bd=4, text = 'OFF', command = log5, bg = "red") #, onvalue=1, offvalue=0)
log_5.grid(row = 6, column = 8)
def fire_all():
#global a
a = str(log_1.config('textvariable')[-1])
b = str(log_2.config('textvariable')[-1])
c = str(log_3.config('textvariable')[-1])
d = str(log_4.config('textvariable')[-1])
e = str(log_5.config('textvariable')[-1])
if a == '1':
fire_astrum('Dev1')
if b == '1':
fire_astrum('Dev2')
if c == '1':
fire_astrum('Dev3')
if d == '1':
fire_astrum('Dev4')
if e == '1':
fire_astrum('Dev5')
# call this function again in 100 milliseconds
window.after(100, fire_all)
window.after(100, fire_all)
window.mainloop()
you can probably launch fire_astrum in a thread from fire_all.
maybe like this:
import threading
...
if a == '1':
threading.Thread(target=lambda dev='Dev1': fire_astrum(dev)).start()
...
It is unclear how long fire_astrum takes to complete; it also sleeps for 300ms; there is no telling the outcome when fired every 100ms, you'll have to try.
I am creating a program which opens a world map in a window using Zelle's graphics.py. It has one function which draws dots on the map, and another function which undraws those dots after they are on the screen for 1 second (which are stored in a list after being drawn). I want these functions to work concurrently, but when the addDots() function is called in a thread it won't draw the dot in the window, it just stalls. Here is the module which I run:
import thread
import threading
import time
import random
import sys
sys.path.append('..')
from Display import map
import tester
import datetime
dots = list(())
def deleteDots():
while 1==1:
tF = datetime.datetime.now()
a = 0
for i in range(len(dots)):
tD = tF - dots[i-a][2]
tD = int(str(tD)[5:7])
if tD >= 1:
map.deletePoint(dots[i-a][0],dots[i-a][1])
dots.pop(i-a)
a = a+1
def addDots():
oldResponseCount = tester.getResponseCount()
oldResponseCount = int(str(oldResponseCount))
while 1==1:
print(oldResponseCount)
newResponseCount = tester.getResponseCount()
newResponseCount = int(str(newResponseCount))
print(newResponseCount)
if(newResponseCount != oldResponseCount):
difference = newResponseCount - oldResponseCount
for i in range(difference):
lat = random.randint(-90,90)
long = random.randint(-180,180)
map.drawPoint(lat,long)
tI = datetime.datetime.now()
dots.append([lat,long,tI])
oldResponseCount = newResponseCount
if __name__ == '__main__':
threading.Thread(target=addDots).start()
threading.Thread(target=deleteDots).start()
And here is the map module which draws the map on a graphics window and contains the functions to plot and delete a point:
from graphics import *
import math
import images
size = 0.6
Circles = list(())
win = GraphWin("My Window", 1920*size, 1080*size)
win.setBackground('blue')
images.test(size)
myImage = Image(Point(960*size,540*size), "../Display/temp.gif")
myImage.draw(win)
import time
def drawPoint(lat,long):
x = int(long*5.3+960)*size
y = int(lat*(-5.92)+540)*size
pt = Point(x,y)
cir = Circle(pt,5)
cir.setFill(color_rgb(255,0,0))
Circles.append([cir,x,y])
cir.draw(win)
def deletePoint(lat,long):
x = int(long*5.3+960)*size
y = int(lat*(-5.92)+540)*size
for c in Circles:
if c[1]==x and c[2]==y:
c[0].undraw()
How should I go about doing this?
There are a couple of issues that have to be addressed. First, any graphics.py commands that invoke tkinter (i.e. commands that cause something to be drawn/undrawn) must be issued by the primary (main) thread. So we need the secondary threads to communicate drawing requests to the primary thread.
Second, you have both your secondary threads modifying the Circles and dots lists -- you need to syncronize (lock) access to these lists so that only one thread at a time can modify or iterate them.
Below is my rework of your code as an example. I've eliminated map and tester routines as I'm just putting dots up on a window with one thread and deleting them after they are a second old from another thread:
from threading import Thread, Lock
from queue import Queue # use for thread-safe communications
from random import randint
import time
from graphics import *
def drawPoint(lat, long):
x = int(long * 5.3 + 960)
y = int(lat * -5.92 + 540)
point = Point(x, y)
circle = Circle(point, 5)
circle.setFill(color_rgb(255, 0, 0))
circles_lock.acquire()
circles.append(circle)
circles_lock.release()
actions.put((circle.draw, win))
def deletePoint(lat, long):
global circles
x = int(long * 5.3 + 960)
y = int(lat * -5.92 + 540)
keep_circles = []
circles_lock.acquire()
for circle in circles:
center = circle.getCenter()
if center.getX() == x and center.getY() == y:
actions.put((circle.undraw,))
else:
keep_circles.append(circle)
circles = keep_circles
circles_lock.release()
def deleteDots():
global dots
while True:
keep_dots = []
dots_lock.acquire()
now = time.time()
for dot in dots:
lat, long, then = dot
if now - then >= 1.0:
deletePoint(lat, long)
else:
keep_dots.append(dot)
dots = keep_dots
dots_lock.release()
time.sleep(0.5)
def addDots():
while True:
lat = randint(-90, 90)
long = randint(-180, 180)
drawPoint(lat, long)
dots_lock.acquire()
dots.append((lat, long, time.time()))
dots_lock.release()
time.sleep(0.25)
win = GraphWin("My Window", 1920, 1080)
circles = []
circles_lock = Lock()
dots = []
dots_lock = Lock()
actions = Queue()
Thread(target=addDots, daemon=True).start()
Thread(target=deleteDots, daemon=True).start()
while True:
if not actions.empty():
action, *arguments = actions.get()
action(*arguments)
time.sleep(0.125)
Am loading a listbox in TKinter. The string is a collection of fields from a matrix. 4 fields, 1 date, 1 float, and 2 text.
However, because of proportional fonts, I cannot get the text in each line to line up into columns.
Here is my current code
from tkinter import *
from tkinter import ttk
from datetime import *
import locale
# for currency display, set the locale
locale.setlocale( locale.LC_ALL, '' )
class main_gui(Frame):
def __init__(self): #Intialize key variables and call the main screen.
self.Ledgerdata = [ ]
print ("main_gui __init__")
self.load_data()
self.main_screen()
def load_data(self):
# stub for testing - to be rewritten to read external file and load into Ledgerdata.
entry = []
entry.append("4/17/2016, 24, 1, Doctor")
entry.append("4/18/2016, 32, 1, Mechanic whose name is bill")
entry.append("4/19/2016, 45, 1, Grocery")
entry.append("4/11/2016, 19, 1, Puppet")
entry.append("4/17/2016, 119.50, 1, Johns Computer House")
entry.append("4/11/2016, 1250, 1, Sidney Barthalmew")
for entry_index in range(len(entry)):
entry[entry_index] = entry[entry_index].split(",")
date_conversion = entry[entry_index][0].split("/") # Convert date from text string
entry[entry_index][0] = date(int(date_conversion[2]), # to datetime variable
int(date_conversion[0]),
int(date_conversion[1]))
self.Ledgerdata.append((entry[entry_index][0], # Add transaction to Ledgerdata
float(entry[entry_index][1]),
int(entry[entry_index][2]),
entry[entry_index][3]))
#====== Data from file is read and loaded into Ledgerdata
#main_screen - main display screen for Ledgerdata
# Will show list of transactions, total for each category,
# with buttons to Add, Edit, Delete, Create Summary and Exit
def main_screen(self):
root = Tk() # reference to the GUI toolkit
main_window = ttk.Frame(root)
main_window.grid(row=0,column=0) # overall Frame
display_frame = ttk.Frame(main_window) # list display frame
display_frame.grid(column=0, row = 0)
option_frame = ttk.Frame(main_window) # selection frame - to be written.
option_frame.grid(column=0, row=15, columnspan=8)
summary_frame = ttk.Frame(main_window)
summary_frame.grid(column = 10, row = 0)
summary_frame.columnconfigure(0, minsize=50)
summary_frame.columnconfigure(1, minsize=250)
#Start display frame
list_header = Label(display_frame, width = 100, # Header line for List
font = 12,anchor = W, # NEEDS CLEANUP
text = " Date Amount Description Code")
list_header.grid(row=1)
list_of_transactions = Listbox(display_frame,width =100, height = 20, font = 12) #Generate listbox widget
list_of_transactions.grid(row=2, column = 0)
# Loop for number of transactions in Ledgerdata. Loop adds formatted transaction info to list_of_transactions
# liststring object for display
for x in range(len(self.Ledgerdata)):
output_string = '{0:10} {1:>8} {2:<50} {3:<40}'.format(
str(self.Ledgerdata[x][0].strftime("%m/%d/%y"))
,locale.currency(self.Ledgerdata[x][1], grouping=True)
,(self.Ledgerdata[x][3])
,(self.Ledgerdata[x][2]))
list_of_transactions.insert(x,output_string)
list_of_transactions.grid(row=x, column = 0)
#main_window.pack()
mainloop()
main_gui()
Anyone know how to get elements in strings to text to maintain the same width???
Thanks as always,
Darrell
EDITTED - Per request, added entire code segment that can be run. I had to take this out of my main program, eliminate several parts that are not relevant to the problem at hand, and make some changes that do not affect the problem at hand. So this is just a standalone testbed.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I wrote a simple console app to upload and download files from an FTP server using the ftplib.
I would like the app to show some visualization of its download/upload progress for the user; each time a data chunk is downloaded, I would like it to provide a progress update, even if it's just a numeric representation like a percentage.
Importantly, I want to avoid erasing all the text that's been printed to the console in previous lines (i.e. I don't want to "clear" the entire terminal while printing the updated progress).
This seems a fairly common task – how can I go about making a progress bar or similar visualization that outputs to my console while preserving prior program output?
Python 3
A Simple, Customizable Progress Bar
Here's an aggregate of many of the answers below that I use regularly (no imports required).
Note: All code in this answer was created for Python 3; see end of answer to use this code with Python 2.
# Print iterations progress
def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█', printEnd = "\r"):
"""
Call in a loop to create terminal progress bar
#params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end = printEnd)
# Print New Line on Complete
if iteration == total:
print()
Sample Usage
import time
# A List of Items
items = list(range(0, 57))
l = len(items)
# Initial call to print 0% progress
printProgressBar(0, l, prefix = 'Progress:', suffix = 'Complete', length = 50)
for i, item in enumerate(items):
# Do stuff...
time.sleep(0.1)
# Update Progress Bar
printProgressBar(i + 1, l, prefix = 'Progress:', suffix = 'Complete', length = 50)
Sample Output
Progress: |█████████████████████████████████████████████-----| 90.0% Complete
Update
There was discussion in the comments regarding an option that allows the progress bar to adjust dynamically to the terminal window width. While I don't recommend this, here's a gist that implements this feature (and notes the caveats).
Single-Call Version of The Above
A comment below referenced a nice answer posted in response to a similar question. I liked the ease of use it demonstrated and wrote a similar one, but opted to leave out the import of the sys module while adding in some of the features of the original printProgressBar function above.
Some benefits of this approach over the original function above include the elimination of an initial call to the function to print the progress bar at 0% and the use of enumerate becoming optional (i.e. it is no longer explicitly required to make the function work).
def progressBar(iterable, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█', printEnd = "\r"):
"""
Call in a loop to create terminal progress bar
#params:
iterable - Required : iterable object (Iterable)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
total = len(iterable)
# Progress Bar Printing Function
def printProgressBar (iteration):
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end = printEnd)
# Initial Call
printProgressBar(0)
# Update Progress Bar
for i, item in enumerate(iterable):
yield item
printProgressBar(i + 1)
# Print New Line on Complete
print()
Sample Usage
import time
# A List of Items
items = list(range(0, 57))
# A Nicer, Single-Call Usage
for item in progressBar(items, prefix = 'Progress:', suffix = 'Complete', length = 50):
# Do stuff...
time.sleep(0.1)
Sample Output
Progress: |█████████████████████████████████████████████-----| 90.0% Complete
Python 2
To use the above functions in Python 2, set the encoding to UTF-8 at the top of your script:
# -*- coding: utf-8 -*-
And replace the Python 3 string formatting in this line:
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end = printEnd)
With Python 2 string formatting:
print('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end = printEnd)
Writing '\r' will move the cursor back to the beginning of the line.
This displays a percentage counter:
import time
import sys
for i in range(100):
time.sleep(1)
sys.stdout.write("\r%d%%" % i)
sys.stdout.flush()
tqdm: add a progress meter to your loops in a second:
>>> import time
>>> from tqdm import tqdm
>>> for i in tqdm(range(100)):
... time.sleep(1)
...
|###-------| 35/100 35% [elapsed: 00:35 left: 01:05, 1.00 iters/sec]
Write a \r to the console. That is a "carriage return" which causes all text after it to be echoed at the beginning of the line. Something like:
def update_progress(progress):
print '\r[{0}] {1}%'.format('#'*(progress/10), progress)
which will give you something like: [ ########## ] 100%
It is less than 10 lines of code.
The gist here: https://gist.github.com/vladignatyev/06860ec2040cb497f0f3
import sys
def progress(count, total, suffix=''):
bar_len = 60
filled_len = int(round(bar_len * count / float(total)))
percents = round(100.0 * count / float(total), 1)
bar = '=' * filled_len + '-' * (bar_len - filled_len)
sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percents, '%', suffix))
sys.stdout.flush() # As suggested by Rom Ruben
Try the click library written by the Mozart of Python, Armin Ronacher.
$ pip install click # both 2 and 3 compatible
To create a simple progress bar:
import click
with click.progressbar(range(1000000)) as bar:
for i in bar:
pass
This is what it looks like:
# [###-------------------------------] 9% 00:01:14
Customize to your hearts content:
import click, sys
with click.progressbar(range(100000), file=sys.stderr, show_pos=True, width=70, bar_template='(_(_)=%(bar)sD(_(_| %(info)s', fill_char='=', empty_char=' ') as bar:
for i in bar:
pass
Custom look:
(_(_)===================================D(_(_| 100000/100000 00:00:02
There are even more options, see the API docs:
click.progressbar(iterable=None, length=None, label=None, show_eta=True, show_percent=None, show_pos=False, item_show_func=None, fill_char='#', empty_char='-', bar_template='%(label)s [%(bar)s] %(info)s', info_sep=' ', width=36, file=None, color=None)
I realize I'm late to the game, but here's a slightly Yum-style (Red Hat) one I wrote (not going for 100% accuracy here, but if you're using a progress bar for that level of accuracy, then you're WRONG anyway):
import sys
def cli_progress_test(end_val, bar_length=20):
for i in xrange(0, end_val):
percent = float(i) / end_val
hashes = '#' * int(round(percent * bar_length))
spaces = ' ' * (bar_length - len(hashes))
sys.stdout.write("\rPercent: [{0}] {1}%".format(hashes + spaces, int(round(percent * 100))))
sys.stdout.flush()
Should produce something looking like this:
Percent: [############## ] 69%
... where the brackets stay stationary and only the hashes increase.
This might work better as a decorator. For another day...
Check this library: clint
it has a lot of features including a progress bar:
from time import sleep
from random import random
from clint.textui import progress
if __name__ == '__main__':
for i in progress.bar(range(100)):
sleep(random() * 0.2)
for i in progress.dots(range(100)):
sleep(random() * 0.2)
this link provides a quick overview of its features
Here's a nice example of a progressbar written in Python: http://nadiana.com/animated-terminal-progress-bar-in-python
But if you want to write it yourself. You could use the curses module to make things easier :)
[edit]
Perhaps easier is not the word for curses. But if you want to create a full-blown cui than curses takes care of a lot of stuff for you.
[edit]
Since the old link is dead I have put up my own version of a Python Progressbar, get it here: https://github.com/WoLpH/python-progressbar
import time,sys
for i in range(100+1):
time.sleep(0.1)
sys.stdout.write(('='*i)+(''*(100-i))+("\r [ %d"%i+"% ] "))
sys.stdout.flush()
output
[ 29% ] ===================
Install tqdm.(pip install tqdm)
and use it as follows:
import time
from tqdm import tqdm
for i in tqdm(range(1000)):
time.sleep(0.01)
That's a 10 seconds progress bar that'll output something like this:
47%|██████████████████▊ | 470/1000 [00:04<00:05, 98.61it/s]
and, just to add to the pile, here's an object you can use:
Add the following to a new file progressbar.py
import sys
class ProgressBar(object):
CHAR_ON = '='
CHAR_OFF = ' '
def __init__(self, end=100, length=65):
self._end = end
self._length = length
self._chars = None
self._value = 0
#property
def value(self):
return self._value
#value.setter
def value(self, value):
self._value = max(0, min(value, self._end))
if self._chars != (c := int(self._length * (self._value / self._end))):
self._chars = c
sys.stdout.write("\r {:3n}% [{}{}]".format(
int((self._value / self._end) * 100.0),
self.CHAR_ON * int(self._chars),
self.CHAR_OFF * int(self._length - self._chars),
))
sys.stdout.flush()
def __enter__(self):
self.value = 0
return self
def __exit__(self, *args, **kwargs):
sys.stdout.write('\n')
Can be included in your program with:
import time
from progressbar import ProgressBar
count = 150
print("starting things:")
with ProgressBar(count) as bar:
for i in range(count + 1):
bar.value += 1
time.sleep(0.01)
print("done")
Results in:
starting things:
100% [=================================================================]
done
This may be "over the top", but is handy when used frequently.
Run this at the Python command line (not in any IDE or development environment):
>>> import threading
>>> for i in range(50+1):
... threading._sleep(0.5)
... print "\r%3d" % i, ('='*i)+('-'*(50-i)),
Works fine on my Windows system.
Try to install this package: pip install progressbar2 :
import time
import progressbar
for i in progressbar.progressbar(range(100)):
time.sleep(0.02)
progresssbar github: https://github.com/WoLpH/python-progressbar
http://code.activestate.com/recipes/168639-progress-bar-class/ (2002)
http://code.activestate.com/recipes/299207-console-text-progress-indicator-class/ (2004)
http://pypi.python.org/pypi/progressbar (2006)
And a lot of tutorials waiting to be googled.
based on the above answers and other similar questions about CLI progress bar, I think I got a general common answer to all of them. Check it at https://stackoverflow.com/a/15860757/2254146
In summary, the code is this:
import time, sys
# update_progress() : Displays or updates a console progress bar
## Accepts a float between 0 and 1. Any int will be converted to a float.
## A value under 0 represents a 'halt'.
## A value at 1 or bigger represents 100%
def update_progress(progress):
barLength = 10 # Modify this to change the length of the progress bar
status = ""
if isinstance(progress, int):
progress = float(progress)
if not isinstance(progress, float):
progress = 0
status = "error: progress var must be float\r\n"
if progress < 0:
progress = 0
status = "Halt...\r\n"
if progress >= 1:
progress = 1
status = "Done...\r\n"
block = int(round(barLength*progress))
text = "\rPercent: [{0}] {1}% {2}".format( "#"*block + "-"*(barLength-block), progress*100, status)
sys.stdout.write(text)
sys.stdout.flush()
Looks like
Percent: [##########] 99.0%
I am using progress from reddit. I like it because it can print progress for every item in one line, and it shouldn't erase printouts from the program.
Edit: fixed link
A very simple solution is to put this code into your loop:
Put this in the body (i.e. top) of your file:
import sys
Put this in the body of your loop:
sys.stdout.write("-") # prints a dash for each iteration of loop
sys.stdout.flush() # ensures bar is displayed incrementally
I recommend using tqdm - https://pypi.python.org/pypi/tqdm - which makes it simple to turn any iterable or process into a progress bar, and handles all messing about with terminals needed.
From the documentation: "tqdm can easily support callbacks/hooks and manual updates. Here’s an example with urllib"
import urllib
from tqdm import tqdm
def my_hook(t):
"""
Wraps tqdm instance. Don't forget to close() or __exit__()
the tqdm instance once you're done with it (easiest using `with` syntax).
Example
-------
>>> with tqdm(...) as t:
... reporthook = my_hook(t)
... urllib.urlretrieve(..., reporthook=reporthook)
"""
last_b = [0]
def inner(b=1, bsize=1, tsize=None):
"""
b : int, optional
Number of blocks just transferred [default: 1].
bsize : int, optional
Size of each block (in tqdm units) [default: 1].
tsize : int, optional
Total size (in tqdm units). If [default: None] remains unchanged.
"""
if tsize is not None:
t.total = tsize
t.update((b - last_b[0]) * bsize)
last_b[0] = b
return inner
eg_link = 'http://www.doc.ic.ac.uk/~cod11/matryoshka.zip'
with tqdm(unit='B', unit_scale=True, miniters=1,
desc=eg_link.split('/')[-1]) as t: # all optional kwargs
urllib.urlretrieve(eg_link, filename='/dev/null',
reporthook=my_hook(t), data=None)
import sys
def progresssbar():
for i in range(100):
time.sleep(1)
sys.stdout.write("%i\r" % i)
progressbar()
NOTE: if you run this in interactive interepter you get extra numbers printed out
lol i just wrote a whole thingy for this
heres the code keep in mind you cant use unicode when doing block ascii i use cp437
import os
import time
def load(left_side, right_side, length, time):
x = 0
y = ""
print "\r"
while x < length:
space = length - len(y)
space = " " * space
z = left + y + space + right
print "\r", z,
y += "█"
time.sleep(time)
x += 1
cls()
and you call it like so
print "loading something awesome"
load("|", "|", 10, .01)
so it looks like this
loading something awesome
|█████ |
With the great advices above I work out the progress bar.
However I would like to point out some shortcomings
Every time the progress bar is flushed, it will start on a new line
print('\r[{0}]{1}%'.format('#' * progress* 10, progress))
like this:
[] 0%
[#]10%
[##]20%
[###]30%
2.The square bracket ']' and the percent number on the right side shift right as the '###' get longer.
3. An error will occur if the expression 'progress / 10' can not return an integer.
And the following code will fix the problem above.
def update_progress(progress, total):
print('\r[{0:10}]{1:>2}%'.format('#' * int(progress * 10 /total), progress), end='')
For python 3:
def progress_bar(current_value, total):
increments = 50
percentual = ((current_value/ total) * 100)
i = int(percentual // (100 / increments ))
text = "\r[{0: <{1}}] {2}%".format('=' * i, increments, percentual)
print(text, end="\n" if percentual == 100 else "")
function from Greenstick for 2.7:
def printProgressBar (iteration, total, prefix = '', suffix = '',decimals = 1, length = 100, fill = '#'):
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print'\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix),
sys.stdout.flush()
# Print New Line on Complete
if iteration == total:
print()
Code for python terminal progress bar
import sys
import time
max_length = 5
at_length = max_length
empty = "-"
used = "%"
bar = empty * max_length
for i in range(0, max_length):
at_length -= 1
#setting empty and full spots
bar = used * i
bar = bar+empty * at_length
#\r is carriage return(sets cursor position in terminal to start of line)
#\0 character escape
sys.stdout.write("[{}]\0\r".format(bar))
sys.stdout.flush()
#do your stuff here instead of time.sleep
time.sleep(1)
sys.stdout.write("\n")
sys.stdout.flush()
The python module progressbar is a nice choice.
Here is my typical code:
import time
import progressbar
widgets = [
' ', progressbar.Percentage(),
' ', progressbar.SimpleProgress(format='(%(value_s)s of %(max_value_s)s)'),
' ', progressbar.Bar('>', fill='.'),
' ', progressbar.ETA(format_finished='- %(seconds)s -', format='ETA: %(seconds)s', ),
' - ', progressbar.DynamicMessage('loss'),
' - ', progressbar.DynamicMessage('error'),
' '
]
bar = progressbar.ProgressBar(redirect_stdout=True, widgets=widgets)
bar.start(100)
for i in range(100):
time.sleep(0.1)
bar.update(i + 1, loss=i / 100., error=i)
bar.finish()
i wrote a simple progressbar:
def bar(total, current, length=10, prefix="", filler="#", space=" ", oncomp="", border="[]", suffix=""):
if len(border) != 2:
print("parameter 'border' must include exactly 2 symbols!")
return None
print(prefix + border[0] + (filler * int(current / total * length) +
(space * (length - int(current / total * length)))) + border[1], suffix, "\r", end="")
if total == current:
if oncomp:
print(prefix + border[0] + space * int(((length - len(oncomp)) / 2)) +
oncomp + space * int(((length - len(oncomp)) / 2)) + border[1], suffix)
if not oncomp:
print(prefix + border[0] + (filler * int(current / total * length) +
(space * (length - int(current / total * length)))) + border[1], suffix)
as you can see, it have: length of bar, prefix and suffix, filler, space, text in bar on 100%(oncomp) and borders
here an example:
from time import sleep, time
start_time = time()
for i in range(10):
pref = str((i+1) * 10) + "% "
complete_text = "done in %s sec" % str(round(time() - start_time))
sleep(1)
bar(10, i + 1, length=20, prefix=pref, oncomp=complete_text)
out in progress:
30% [###### ]
out on complete:
100% [ done in 9 sec ]
Putting together some of the ideas I found here, and adding estimated time left:
import datetime, sys
start = datetime.datetime.now()
def print_progress_bar (iteration, total):
process_duration_samples = []
average_samples = 5
end = datetime.datetime.now()
process_duration = end - start
if len(process_duration_samples) == 0:
process_duration_samples = [process_duration] * average_samples
process_duration_samples = process_duration_samples[1:average_samples-1] + [process_duration]
average_process_duration = sum(process_duration_samples, datetime.timedelta()) / len(process_duration_samples)
remaining_steps = total - iteration
remaining_time_estimation = remaining_steps * average_process_duration
bars_string = int(float(iteration) / float(total) * 20.)
sys.stdout.write(
"\r[%-20s] %d%% (%s/%s) Estimated time left: %s" % (
'='*bars_string, float(iteration) / float(total) * 100,
iteration,
total,
remaining_time_estimation
)
)
sys.stdout.flush()
if iteration + 1 == total:
print
# Sample usage
for i in range(0,300):
print_progress_bar(i, 300)
Well here is code that works and I tested it before posting:
import sys
def prg(prog, fillchar, emptchar):
fillt = 0
emptt = 20
if prog < 100 and prog > 0:
prog2 = prog/5
fillt = fillt + prog2
emptt = emptt - prog2
sys.stdout.write("\r[" + str(fillchar)*fillt + str(emptchar)*emptt + "]" + str(prog) + "%")
sys.stdout.flush()
elif prog >= 100:
prog = 100
prog2 = prog/5
fillt = fillt + prog2
emptt = emptt - prog2
sys.stdout.write("\r[" + str(fillchar)*fillt + str(emptchar)*emptt + "]" + str(prog) + "%" + "\nDone!")
sys.stdout.flush()
elif prog < 0:
prog = 0
prog2 = prog/5
fillt = fillt + prog2
emptt = emptt - prog2
sys.stdout.write("\r[" + str(fillchar)*fillt + str(emptchar)*emptt + "]" + str(prog) + "%" + "\nHalted!")
sys.stdout.flush()
Pros:
20 character bar (1 character for every 5 (number wise))
Custom fill characters
Custom empty characters
Halt (any number below 0)
Done (100 and any number above 100)
Progress count (0-100 (below and above used for special functions))
Percentage number next to bar, and it's a single line
Cons:
Supports integers only (It can be modified to support them though, by making the division an integer division, so just change prog2 = prog/5 to prog2 = int(prog/5))
Here's my Python 3 solution:
import time
for i in range(100):
time.sleep(1)
s = "{}% Complete".format(i)
print(s,end=len(s) * '\b')
'\b' is a backslash, for each character in your string.
This does not work within the Windows cmd window.
This time I want to know what possible solutions exist to following situation: I let my Laptop read the raw mouse data (Ubuntu OS) with a python script I already posted here. It has a method, which reads the mouse file and extracts the x,y data from it. a while true loop uses this method to put the data into an array. when I use a time counter to stop reading after a while the script puts the data into an excel file.
What I now need is a option to pause the data stream, i.e. to change mouse position without creating data, and then resume it. I would like to have something to stop the reading and to write it into excel.
import struct
import matplotlib.pyplot as plt
import numpy as np
import xlsxwriter
import time
from drawnow import *
workbook = xlsxwriter.Workbook('/path/test.xlsx')
worksheet = workbook.add_worksheet()
file = open( "/dev/input/mouse2", "rb" );
test = [(0,0,0)]
plt.ion()
def makeFig():
plt.plot(test)
#plt.show()
def getMouseEvent():
buf = file.read(3);
button = ord( buf[0] );
bLeft = button & 0x1;
x,y = struct.unpack( "bb", buf[1:] )
_zeit = time.time()-test[-1][-1]
print ("x: %d, y: %d, Zeit: %d\n" % (x, y, _zeit) )
return x,y, _zeit
zeit = time.time()
warte = 0
while warte < 20:
test.append(getMouseEvent())
warte = time.time()-zeit
row = 1
col = 0
worksheet.write(0,0, 'x-richtung')
worksheet.write('C1', 'Zeit')
for x, y , t in (test):
worksheet.write(row, col, x)
worksheet.write(row, col + 1, y)
worksheet.write(row, col + 2, t)
row += 1
chart = workbook.add_chart({'type': 'line'})
chart.add_series({'values': '=Sheet1!$A$1:$A$'+str(len(test))})
worksheet.insert_chart('D2', chart)
workbook.close()
#drawnow(makeFig)
#plt.pause(.00001)
file.close();
It would be awesome to have something like "hit space for pause/Unpause. q for end and save" but I have no clue how to do it. Any idea would be nice :)
Oh and I tried to Plot the Data with matplotlib, which worked but it's something for future improvements ;)
Here is an example with the standard threading module - I actually don't know how responsive it will be. Also if you want to pause or start input based on a global hotkey, rather than from the script this will depend on your desktop environment - I've only used xlib but there should be a python wrapper for it floating around somewhere.
import threading
import struct
data =[]
file = open("/dev/input/mouse0", "rb")
e=threading.Event()
def getMouseEvent():
buf = file.read(3);
#python 2 & 3 compatibility
button = buf[0] if isinstance(buf[0], int) else ord(buf[0])
bLeft = button & 0x1;
bMiddle = ( button & 0x4 ) > 0;
bRight = ( button & 0x2 ) > 0;
x,y = struct.unpack( "bb", buf[1:] );
return "L:%d, M: %d, R: %d, x: %d, y: %d\n" % (bLeft,bMiddle,bRight, x, y)
def mouseCollect():
global e
#this will wait while e is False (without breaking the loop)
#and loop while e is True
while e.wait():
#do something with MouseEvent data, like append to an array, or redirect to pipe etc.
data.append(getMouseEvent())
mouseCollectThread = threading.Thread(target=mouseCollect)
mouseCollectThread.start()
#toggle mouseCollect with any keyboard input
#type "q" or "quit" to quit.
while True:
x = input()
if x.lower() in ['quit', 'q', 'exit']:
mouseCollectThread._stop()
file.close()
break
elif x:
e.clear() if e.isSet() else e.set()
edit: I was missing a () after e.isSet