Im trying to make a underground controlling program with visualisation (for fun of course ;D). I know there are many different loading bar libraries but I couldnt find anything wiht milestones if you know what I mean. Im using Python 3 on windows. I will be thankfull for any help
A loading bar with milestones can be achived with sys:
def updt(total, progress):
barLength, status = 25, ""
progress = float(progress) / float(total)
if progress >= 1.:
progress, status = 1, "\r\n"
block = int(round(barLength * progress))
text = "\r[{}] {:.0f}% {}".format(
"#" * block + "-" * (barLength - block), round(progress * 100, 0),
status)
sys.stdout.write(text)
sys.stdout.flush()
runs = 250
for run_num in range(runs):
time.sleep(.1)
updt(runs, run_num + 1)
time.sleep(1)
Related
I want to use a manual progress bar from tqdm in a Jupyter notebook with a nested loop. To have an overview over all iterations, I use a manual update of the progress bar as follows:
from tqdm.notebook import tqdm
a = range(100)
b = range(5)
pbar = tqdm(total=len(a)*len(b))
for a_ in a:
for b_ in b:
pbar.update(1)
pbar.refresh()
However, when the total number of iterations is reached (i.e., 100%), the color is still blue. But if I use something like for i in trange(100): ... , the progress bar turns green after finishing.
Can someone tell me how to achieve the same behavior for the manual progress bar? Thanks for help!
I think pbar.close() can do it.
from tqdm.notebook import tqdm
a = range(100)
b = range(5)
pbar = tqdm(total=len(a)*len(b))
for a_ in a:
for b_ in b:
pbar.update(1)
pbar.refresh()
pbar.close()
Found this code on stackoverflow:
def progress_function(chunk, file_handling, bytes_remaining):
'''
function to show the progress of the download
'''
global filesize
filesize=chunk.filesize
current = ((filesize - bytes_remaining)/filesize)
percent = ('{0:.1f}').format(current*100)
progress = int(50*current)
status = '█' * progress + '-' * (50 - progress)
You can edit this code to this:
def progress_function(chunk, file_handling, bytes_remaining):
'''
function to show the progress of the download
'''
global filesize
filesize=chunk.filesize
current = ((filesize - bytes_remaining)/filesize)
percent = ('{0:.1f}').format(current*100)
progress = int(50*current)
status = '█' * progress + '-' * (50 - progress)
#change the color of the progress bar to green when the download is complete
if bytes_remaining == 0:
status = '\033[92m' + status + '\033[0m'
sys.stdout.write(' ↳ |{bar}| {percent}%\r'.format(bar=status,
percent=percent))
sys.stdout.flush()
This will turn the progress bar to green when it is done.
I don't know whether it will help you or not. But, check it out anyway :)
I program an app witgh QT interface to read modbus data, I display data on GUI create with PyQT5.
When I launch program, it crash after few second, I haven't message error. I used debugger and nothing wrong thing, program just close.
This is a part of my code where is my problem, I'll explain after
def displayModbus(self):
modbusReturn = modbus.readModbus()
commStatus = modbusReturn[0]
if commStatus:
listRegValue = modbusReturn[1]
#Display data to ui
if str(listRegValue[1]) == "1": #HEATING_ST
self.ui.lnHeatingStatus.setText("On")
else:
self.ui.lnHeatingStatus.setText("Off")
# self.ui..setText(str(listRegValue[2])) #WAIT_TIME_CHG
# self.ui..setText(str(listRegValue[3])) #RUN_TIME_CHG
if str(listRegValue[4]) == "1": #ON_WAIT_TIME_P
self.ui.ledWaitTime.setStyleSheet("QPushButton {background-color:green;}")
else:
self.ui.ledWaitTime.setStyleSheet("QPushButton {background-color:red;}")
if str(listRegValue[5]) == "1": #RUN_CYCLE
self.ui.lnRunCycle.setText("On")
else:
self.ui.lnRunCycle.setText("Off")
# self.ui..setText(str(listRegValue[6])) #LEVEL_RESET
# self.ui..setText(str(listRegValue[7])) #CYL_MAN_CYCLE
# self.ui..setText(str(listRegValue[8])) #CYL_EMPTY
# self.ui..setText(str(listRegValue[9])) #CYL_EMPTY_END
# self.ui..setText(str(listRegValue[10])) #NORASYSTEM_MANUAL
# self.ui..setText(str(listRegValue[11])) #NORASYSTEM_ON
# self.ui..setText(str(listRegValue[12])) #HEATING_ALLOW
if str(listRegValue[13]) == "1": #LOW_AIRFLOW
self.ui.lnLowAirflow.setText("Low")
else:
self.ui.lnLowAirflow.setText("Normal")
# self.ui..setText(str(listRegValue[15])) #HUM_ALLOW
TC1 = listRegValue[16] / 100
self.ui.lnTC1.setText(str(TC1)+" °C") #TC1
TC2 = listRegValue[17] / 100
self.ui.lnTC2.setText(str(TC2)+" °C") #TC2
TC3 = listRegValue[18] / 100
self.ui.lnTC3.setText(str(TC3)+" °C") #TC3
TC4 = listRegValue[19] / 100
self.ui.lnTC4.setText(str(TC4)+" °C") #TC4
self.ui.lnH1.setText(str(listRegValue[20])+" %RH") #HUM1
waitTime = str(listRegValue[22] / 3600)
self.ui.lnWaitTime.setText(str(waitTime+" hr")) #WAIT_TIME_D
runTime = str(listRegValue[23] / 60)
self.ui.lnRunTime.setText(str(runTime+" min")) #RUN_TIME_D
timeRemainM = str(int(listRegValue[24] / 60))
timeRemainS = str(int(listRegValue[24] % 60))
self.ui.lnTimeRemain.setText(str(timeRemainM + " min "+timeRemainS + " sec")) #TIME_REMAIN_D
self.ui.lnLevel.setText(str(listRegValue[25])) #LEVEL_D
self.statusBar().showMessage("Modbus communication OK")
else:
self.statusBar().showMessage("Error modbus communication")
The problem come from this function after many try and error method. If I delete it program doesn't crash BUT if I modify it by print function (example: print(str(listRegValue[4]))) all is good so my problem doesn't come from mGUI assignation odbus module.
my function displayModbus is refresh every 5 second (using QtThreading).
If I keep just one variable to display my app doesn't crash.
I have another app to read and record modbus data using same displayModbus function (without QT5 display) and I haven't problem
I have this problem on 2 computers and I try it on 5 computers (4x windows 10 and 1x Linux)
Python 3.8.6
PyQT==5.15.1
PyQt5-sip==12.8.1
Any idea?
Thanks.
I'm trying to have a progress window which shows the progress, alongside having tasks happening in the background. Everything works as expected, except the window partially loads on to the screen (how much of it does depends on every run). Here is the relevant part of the code:
def loading(): #Displays loading progress while setting up the exam
global load, progress
load = Toplevel()
load.title("Loading")
load.attributes('-topmost', True)
load.overrideredirect(True)
lab = Label(load, text = ("Preparing Your Exam, Please Wait!\nPlease DO NOT Open any Other Window.\n"
+"Doing so may lead to immidiate Termination."))
lab.grid(row = 0, column = 1, padx = 20, pady = 20)
progress=Progressbar(load,orient=HORIZONTAL,length=200,mode='determinate')
progress.grid(row = 1, column = 1, padx = 20, pady = 20)
log = Label(load, image = logo)
log.image = logo
log.grid(row = 0, column = 0, rowspan = 2, padx = 20, pady = 20)
w_req, h_req = load.winfo_width(), load.winfo_height()
w_form = load.winfo_rootx() - load.winfo_x()
w = w_req + w_form*2
h = h_req + (load.winfo_rooty() - load.winfo_y()) + w_form
x = (load.winfo_screenwidth() // 2) - (w // 2)
y = (load.winfo_screenheight() // 2) - (h // 2)
load.geometry(f'{w_req}x{h_req}+{x}+{y}')
Here's what happens after calling loading:
loading()
conv_th = Thread(target = Convert).start()
The Convert function converts and processes images, I'm not sharing that because it might not be relevant.
I far as I think, it might be because it is not getting enough time to load completely, but I couldn't really figure out what could be causing the program to behave this way. Any help will be appreciated!
Update: This behavior is seen even if conv_th = Thread(target = Convert).start() is omitted, implying that there could be a problem within the loading() function.
So, I ended up solving the problem myself. I'm telling the reason that I think is most probable, please correct me if the reason that I give is incorrect or there is another solution for this.
This part of the code,
w_req, h_req = load.winfo_width(), load.winfo_height()
w_form = load.winfo_rootx() - load.winfo_x()
w = w_req + w_form*2
h = h_req + (load.winfo_rooty() - load.winfo_y()) + w_form
x = (load.winfo_screenwidth() // 2) - (w // 2)
y = (load.winfo_screenheight() // 2) - (h // 2)
was being executed too early, before the window could actually load itself up completely, and hence whatever amount of it got loaded before this, was taken as the dimensions and then the values were set.
Adding the command load.update_idletasks() before the above part of the code resolved the problem. Thanks #martineau, your comment was really helpful in figuring this out.
I'm working on my first big project on Python - Inspired by the Chris Kiehl sushi go round tutorial, I'm building a bot that plays a text sim game using gui automation and basic computer vision. I want to use Pillow to take screenshots and gui apis to manipulate the operating system but I can make scripts that do either but not both. I'm not quite sure what I'm doing wrong - any help would be really appreciated - thanks.
Here's an abridged version of what I'm trying to do:
def match_loop():
global home_or_away
box = (x_pad+1,y_pad+1,x_pad+806,y_pad+629)
im2 = ImageGrab.grab(box)
time.sleep(1)
mousePos((x_pad+49, y_pad+149))
leftClick()
time.sleep(1)
if home_or_away == "home":
mousePos((x_pad+175, y_pad+543))
leftClick()
mousePos((x_pad+49, y_pad+149))
else:
mousePos((x_pad+728, y_pad+543))
leftClick()
mousePos((x_pad+49, y_pad+149))
im2.save(os.getcwd() + '\\full_snap_' + str(int(time.time()))+'.png', 'PNG')
while im2.getpixel((33,136)) != (206,203,214) : #while it's the first half
im2 = ImageGrab.grab(box)
im2.save(os.getcwd() + '\\full_snap_' + str(int(time.time()))+'.png', 'PNG')
time.sleep(1)
if im2.getpixel((33,136)) == (206,203,214): #if's the secondhalf
secondhalf()
secondhalf()
The code above runs fine but the code below runs into a syntax error after the mousePos function call, which works in the function above, no matter what I put afterwards:
def subsaudit():
im = ImageGrab.grab(box)
im.save(os.getcwd() + '\\full_snap_' + '.png', 'PNG')
if im.getpixel((124,399)) or im.getpixel((125,399)) or im.getpixel((126,399)) or im.getpixel((127,399)) or im.getpixel((128,399)) or im.getpixel((129,399)) or im.getpixel((130,399)) or im.getpixel((131,399)) or im.getpixel((132,399)) or im.getpixel((133,399)) or im.getpixel((134,399)) or im.getpixel((135,399)) or im.getpixel((136,399)) or im.getpixel((137,399)) != (0,0,66):
subscount = 1
if im.getpixel((124,419)) or im.getpixel((125,419)) or im.getpixel((126,419)) or im.getpixel((127,419)) or im.getpixel((128,419)) or im.getpixel((129,419)) or im.getpixel((130,419)) or im.getpixel((131,419)) or im.getpixel((132,419)) or im.getpixel((133,419)) or im.getpixel((134,419)) or im.getpixel((135,419)) or im.getpixel((136,419)) or im.getpixel((137,419)) != (0,0,66):
subscount = 2
if subscount >= 1 :
mousePos(((x_pad+178, y_pad+399))
time.sleep(1)
leftClick()
box = (x_pad+1,y_pad+1,x_pad+806,y_pad+629)
im = ImageGrab.grab(box)
im.save(os.getcwd() + '\\full_snap_' + '.png', 'PNG')
I'm trying to develop a multithread function in python 3.6 and sometime my code freeze. from my tests I think that the problem come from os.write() or os.read(), but I don't know why.
here is my code (I don't think that partialTransform() cause the freeze but I put it to understand the code):
def naiveTransform(netData,**kwargs):
#parralelisable part
def partialTransform(debut, fin) :
for i in range(debut, fin) :
j = 0
#calcul of all the distances :
while j < nbrPoint :
distance[j] = euclidianDistance(netData[i], netData[j])
j += 1
#construction of the graph :
j = 0
del distance[i]
while j < k :
nearest = min(distance, key=distance.get)
del distance[nearest] #if k > 1 we don't want to get always the same point.
graph.append([i, nearest])
j += 1
return graph
k = kwargs.get('k', 1) # valeur par défault à definir.
nbrCore = kwargs.get('Core', 1)
nbrPoint = len(netData)
nbrPointCore = nbrPoint//nbrCore
distance = dict()
graph = []
#pipes
r = [-1]*nbrCore
w = [-1]*nbrCore
pid = [-1]*nbrCore
for i in range(nbrCore):
r[i], w[i] = os.pipe()
try:
pid[i] = os.fork()
except OSError:
exit("Could not create a child process\n")
if pid[i] == 0:
if i < nbrCore-1 :
g = partialTransform(i*nbrPointCore, (i+1)*nbrPointCore)
else :
g = partialTransform(i*nbrPointCore, nbrPoint) #to be sure that there is not a forgoten point.
print("write in " + str(i))
import sys
print(sys.getsizeof(g))
os.write(w[i], pickle.dumps(g))
print("exit")
exit()
for i in range(nbrCore):
print("waiting " + str(i))
finished = os.waitpid(pid[i], 0)
print("received")
graph += pickle.loads(os.read(r[i], 250000000))
return graph
When the argument k is superior or equal to 5 the code freeze after the
print(sys.getsizeof(g))
For my example case when k = 4 the size is of 33928 and for k = 5 the size is of 43040 so I don't think that it's the problem ?
The number of core used don't seem to have any influence on the freeze.
I'm still a beginner in python so it may be something obvious but I didn't find any similar problem on internet. Do you have any idea of what could cause theses freeze ?
Pipes have limited size buffers and the child will block writing the pipe until the parent reads it. But the parent is waiting for the child to exit, so you hang. You can avoid the buffer limit by writing the object to a temporary file instead. The data will be in the operation system file cache when the parent reads so it will still be fast.
There is a trick in all this. The parent needs to convince libc to re-examine the file after the child writes it or the read will just be satisfied by its 0 length internal cache. You can do that with a seek.
import tempfile
def naiveTransform(netData,**kwargs):
// *** code removed for example ***
# files
tmp = [tempfile.TemporaryFile() for _ in range(nbrCore)]
pid = [-1]*nbrCore
for i in range(nbrCore):
try:
pid[i] = os.fork()
except OSError:
exit("Could not create a child process\n")
if pid[i] == 0:
if i < nbrCore-1 :
g = partialTransform(i*nbrPointCore, (i+1)*nbrPointCore)
else :
g = partialTransform(i*nbrPointCore, nbrPoint) #to be sure that there is not a forgoten point.
print("write in " + str(i))
import sys
print(sys.getsizeof(g))
pickle.dump(g, tmp[i])
tmp[i].close()
print("exit")
exit()
for i in range(nbrCore):
print("waiting " + str(i))
finished = os.waitpid(pid[i], 0)
print("received")
# seek to get updated file content
tmp[i].seek(0,2)
tmp[i].seek(0)
graph += pickle.load(tmp[i])
return graph