-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
83 lines (67 loc) · 3.13 KB
/
app.py
File metadata and controls
83 lines (67 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import requests
from flask import Flask, render_template
from datetime import datetime
import pytz
from icon_mapping import icon_mapping
from directions import degrees_to_direction
app = Flask(__name__)
@app.route('/')
def home():
location = "Tampa, Florida" #hard code location for now
# Make the API request
url = "https://api.openweathermap.org/data/2.5/onecall"
params = {
"lat": 27.9506, # Latitude of Tampa, Florida
"lon": -82.4572, # Longitude of Tampa, Florida
"appid": "fbf5ddfd48f2e22c88896a1be5ac73e7",
"units": "imperial" # Get temperature in Fahrenheit
}
response = requests.get(url, params=params)
data = response.json()
# Get the current date and time in the returned timezone
tz = pytz.timezone(data['timezone'])
now = datetime.now(tz)
current_time = now.strftime("%B %d, %Y %I:%M %p")
# Determine if day or night for the current weather icon
condition_id = data["current"]["weather"][0]["id"]
if 800 > condition_id > 602:
time_of_day = ".svg" # Nessasary since some of the weather icons do not have day/night versions
elif 6 <= now.hour < 19: # Day time
time_of_day = "-day.svg"
else: # Night time
time_of_day = "-night.svg"
# Get the corresponding icon filename from your mapping
icon_filename = icon_mapping.get(condition_id) + time_of_day
# Extract needed values from data to be returned to the home.html template
temperature = round(data["current"]["temp"])
feels_like = round(data["current"]["feels_like"])
condition = data["current"]["weather"][0]["description"]
conditions = condition.title()
temp_high = round(data["daily"][0]["temp"]["max"])
temp_low = round(data["daily"][0]["temp"]["min"])
humidity = round(data["current"]["humidity"])
dew_point = round(data["current"]["dew_point"])
rain_chance = round(data["daily"][0]["pop"]*100)
wind_speed = round(data["current"]["wind_speed"])
wind_direction_deg = data["current"]["wind_deg"]
wind_direction = degrees_to_direction(wind_direction_deg)
wind_gust = round(data["current"].get("wind_gust", 0))
visability = (data["current"]["visibility"]) / 1000
return render_template('home.html', temperature = temperature, time = current_time, location = location, feels_like = feels_like,
condition = conditions, icon_filename = icon_filename, temp_high = temp_high, temp_low = temp_low, humidity = humidity,
dew_point = dew_point, rain_chance = rain_chance, wind_speed = wind_speed, wind_direction = wind_direction, wind_gust = wind_gust,
visability = visability)
@app.route('/forecast')
def forecast():
return render_template('forecast.html')
@app.route('/weather-history')
def weather_history():
return render_template('weather_history.html')
@app.route('/radar')
def radar():
return render_template('radar.html')
@app.route('/about')
def about():
return render_template('about.html')
if __name__ == '__main__':
app.run(debug=True)