Skip to content

Instantly share code, notes, and snippets.

@aitoehigie
Last active July 20, 2020 01:42
Show Gist options
  • Save aitoehigie/3fcb53431ba2f7b2648541e87742a030 to your computer and use it in GitHub Desktop.
Save aitoehigie/3fcb53431ba2f7b2648541e87742a030 to your computer and use it in GitHub Desktop.
"""
A simple weather app.
"""
from pprint import pprint
import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW, LEFT, RIGHT
import requests
TOKEN="37946c6bcf8f62c02f22594c77f8a311"
BASE_URL="http://api.weatherstack.com/"
class weatherman(toga.App):
def startup(self):
"""
Construct and show the Toga application.
Usually, you would add your application to a main content box.
We then create a main window (with a name matching the app), and
show the main window.
"""
main_box = toga.Box(style=Pack(direction=COLUMN))
location_label = toga.Label("Location", style=Pack(padding=(0, 5)))
self.location_input = toga.TextInput(style=Pack(flex=1))
location_box = toga.Box(style=Pack(direction=ROW, padding=5))
location_box.add(location_label)
location_box.add(self.location_input)
weather_box_label = toga.Label("Weather Results", style=Pack(padding=(0, 5)))
self.weather_box_input = toga.TextInput(readonly=True, style=Pack(flex=1))
weather_box = toga.Box(style=Pack(direction=ROW, padding=5))
weather_box.add(weather_box_label)
weather_box.add(self.weather_box_input)
button = toga.Button("Fetch weather", on_press=self.weather, style=Pack(padding=5))
main_box.add(location_box)
main_box.add(button)
main_box.add(weather_box)
self.main_window = toga.MainWindow(title=self.formal_name)
self.main_window.content = main_box
self.main_window.show()
def weather(self, widget):
params = dict(access_key=TOKEN, query=self.location_input.value)
resp = requests.get(BASE_URL + "current", params=params).json()
try:
self.weather_box_input.value = f'The weather report for {resp["request"]["query"]} \
{resp["current"]["temperature"]} \
{resp["current"]["weather_descriptions"][0]} \
{resp["current"]["feelslike"]}'
except ValueError:
self.weather_box_input.value = "Location unknown!"
def main():
return weatherman()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment