Sorry guys for the very newbish question.
I just created a small program using PySimpleGUI and BeautifulSoup which reads live stocks from the TMXMoney website. I know it's quite bad but I am very new to Python (this is my first program actually).
Unfortunately, my program is running a bit slow. When I CMD + Tab to a browser and back to my app, it takes a few seconds for that to go through.
I know I probably have some piece of code that is bottlenecking the whole program, so I was wondering if anybody could help me out in identifying what that bottleneck is?
Here is the code:
import PySimpleGUI as sg
import time
import bs4
import requests
def parsePrice(sym):
r = requests.get("https://web.tmxmoney.com/quote.php?qm_symbol=" + sym) #Canadian stocks source (TSX)
soup = bs4.BeautifulSoup(r.text,"html.parser")
try:
price = soup.find("div", {"class":"labs-symbol"}).find("span", {"class":"price"}).find("span").text
return price
except:
return "Cannot find"
sg.theme('DarkBrown1')
layout = [[sg.Input(key="i1",font=('Helvetica', 20),size = (10,0)), sg.Text("0.00",size=(6,0), font=('Helvetica', 20), key="o1")],
[sg.Input(key="i2",font=('Helvetica', 20),size = (10,0)), sg.Text("0.00",size=(6,0),font=('Helvetica', 20), key="o2")],
[sg.Input(key="i3",font=('Helvetica', 20),size = (10,0)), sg.Text("0.00",size=(6,0),font=('Helvetica', 20), key="o3")],
[sg.Input(key="i4",font=('Helvetica', 20),size = (10,0)), sg.Text("0.00",size=(6,0),font=('Helvetica', 20), key="o4")],
[sg.Input(key="i5",font=('Helvetica', 20),size = (10,0)), sg.Text("0.00",size=(6,0),font=('Helvetica', 20), key="o5")],
[sg.Input(key="i6",font=('Helvetica', 20),size = (10,0)), sg.Text("0.00",size=(6,0),font=('Helvetica', 20), key="o6")]]
window = sg.Window("TSX Stocks", layout)
while True: # Event Loop
event, values = window.read(timeout=2000)
if event in (None, 'Quit'): # if user closed the window using X or clicked Quit button
break
if values['i1'] is not "":
window['o1'].update(parsePrice(values['i1']))
if values['i2'] is not "":
window['o2'].update(parsePrice(values['i2']))
if values['i3'] is not "":
window['o3'].update(parsePrice(values['i3']))
if values['i4'] is not "":
window['o4'].update(parsePrice(values['i4']))
if values['i5'] is not "":
window['o5'].update(parsePrice(values['i5']))
if values['i6'] is not "":
window['o6'].update(parsePrice(values['i6']))
time.sleep(1)
window.close()