42
loading...
This website collects cookies to deliver better user experience
rumps
requests
py2app
import rumps
def hello(sender):
print(f"Hello from {sender.title}")
app = rumps.App("Hello World")
app.menu = [rumps.MenuItem("Weird Menu Item",callback=hello)]
app.run()
hello()
function is executed when the menu item is clicked.>>> Hello from Weird Menu Item
rumps.clicked
decorator:@rumps.clicked("Weird Menu Item")
@rumps.clicked("Saner Menu Item")
def hello(sender):
print(f"Hello from {sender.title}")
app = rumps.App("Hello World")
app.run()
GET https://finnhub.io/api/v1/quote?symbol=AAPL&token=YOUR_API_KEY
{
"c":119.49,
"h":119.6717,
"l":117.87,
"o":119.44,
"pc":119.21,
"t":1605436627
}
import rumps
class StockApp(rumps.App):
def __init__(self):
super(StockApp, self).__init__(name="Stock")
@rumps.clicked("MSFT")
@rumps.clicked("AAPL")
def getStock(self, sender):
self.title = f"{sender.title}"
if __name__ == '__main__':
StockApp().run()
import requests
import rumps
class StockApp(rumps.App):
def __init__(self):
super(StockApp, self).__init__(name="Stock")
@rumps.clicked("MSFT")
@rumps.clicked("AAPL")
def getStock(self, sender):
response = requests.get(f"https://finnhub.io/api/v1/quote?symbol={sender.title}&token=YOUR_API_KEY")
stock_data = response.json()['c']
self.title = f"{sender.title}:{stock_data}"
if __name__ == '__main__':
StockApp().run()
getStock()
method updates the title when you select a stock symbol from the menu.Timer
class, and you can decorate a function with rumps.timer()
to set a timer on the function.@rumps.timer(1)
def do_something(self, sender):
# this function is executed every 1 second
@rumps.clicked("AAPL")
@rumps.clicked("MSFT")
def changeStock(self, sender):
self.stock = sender.title
@rumps.timer(5)
def updateStockPrice(self, sender):
# fetch stock quote and update title
import threading
@rumps.timer(5)
def updateStockPrice(self, sender):
thread = threading.Thread(target=self.getStock)
thread.start()
def getStock(self):
# code to send API request
import threading
import requests
import rumps
class StockApp(rumps.App):
def __init__(self):
super(StockApp, self).__init__(name="Stock")
self.stock = "AAPL"
self.icon = "icon.png"
self.API_KEY = "YOUR_API_KEY"
@rumps.clicked("Search...")
@rumps.clicked("MSFT")
@rumps.clicked("TSLA")
@rumps.clicked("NFLX")
@rumps.clicked("FB")
@rumps.clicked("AAPL")
def changeStock(self, sender):
if sender.title!="Search...":
self.title = f" 🔍 {sender.title}"
self.stock = sender.title
else:
# Launches a rumps window for user input
window = rumps.Window(f"Current: {self.stock}","Search another stock")
window.icon = self.icon
response = window.run()
self.stock = response.text
@rumps.timer(5)
def updateStockPrice(self, sender):
thread = threading.Thread(target=self.getStock)
thread.start()
def getStock(self):
response = requests.get(f"https://finnhub.io/api/v1/quote?symbol={self.stock}&token={self.API_KEY}")
if response.status_code!=200:
self.title = "API Error."
return
stock_data = response.json()
current_price = stock_data['c']
previous_close = stock_data['pc']
change = current_price-previous_close
try:
changePercentage = abs(round(100*(change/previous_close), 2))
if change<0:
marker = "🔻"
else:
marker = "🔺"
self.title = f" {self.stock}: {str(response.json()['c'])} {marker}{changePercentage}%"
# Finnhub returns 0 for non-existent symbols
except ZeroDivisionError:
self.title = "Invalid symbol, set to AAPL"
self.stock = "AAPL"
if __name__ == '__main__':
StockApp().run()
self.API_KEY
.StockApp
|__ app.py
|__ setup.py
|__ icon.png
|__ icon.icns
from setuptools import setup
APP = ['app.py']
DATA_FILES = ['icon.png']
OPTIONS = {
'argv_emulation': True,
'iconfile': 'icon.icns',
'plist': {
'CFBundleShortVersionString': '1.0.0',
'LSUIElement': True,
},
'packages': ['rumps','requests']
}
setup(
app=APP,
name='Stock',
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'], install_requires=['rumps','requests']
)
python setup.py py2app
dist
folder. Open the app, and see it in action!open Stock.App/Contents/MacOS/Stock