Skip to content

Commit 07dab2d

Browse files
committed
Added lesson 23
1 parent 607109d commit 07dab2d

9 files changed

Lines changed: 170 additions & 1 deletion

File tree

lesson23

Lines changed: 0 additions & 1 deletion
This file was deleted.

lesson23/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.venv
2+
.env
3+
__pycache__

lesson23/requirements.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
blinker==1.6.2
2+
certifi==2023.5.7
3+
charset-normalizer==3.2.0
4+
click==8.1.5
5+
colorama==0.4.6
6+
Flask==2.3.2
7+
idna==3.4
8+
itsdangerous==2.1.2
9+
Jinja2==3.1.2
10+
MarkupSafe==2.1.3
11+
python-dotenv==1.0.0
12+
requests==2.31.0
13+
urllib3==2.0.3
14+
waitress==2.1.2
15+
Werkzeug==2.3.6

lesson23/server.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from flask import Flask, render_template, request
2+
from weather import get_current_weather
3+
from waitress import serve
4+
5+
app = Flask(__name__)
6+
7+
8+
@app.route('/')
9+
@app.route('/index')
10+
def index():
11+
return render_template('index.html')
12+
13+
14+
@app.route('/weather')
15+
def get_weather():
16+
city = request.args.get('city')
17+
18+
# Check for empty strings or string with only spaces
19+
if not bool(city.strip()):
20+
city = "Kansas City"
21+
22+
weather_data = get_current_weather(city)
23+
24+
# City is not found by API
25+
if not weather_data['cod'] == 200:
26+
return render_template('city-not-found.html')
27+
28+
return render_template(
29+
"weather.html",
30+
title=weather_data["name"],
31+
status=weather_data["weather"][0]["description"].capitalize(),
32+
temp=f"{weather_data['main']['temp']:.1f}",
33+
feels_like=f"{weather_data['main']['feels_like']:.1f}"
34+
)
35+
36+
37+
if __name__ == "__main__":
38+
serve(app, host="0.0.0.0", port=8000)

lesson23/static/styles/style.css

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
* {
2+
margin: 0;
3+
padding: 0;
4+
box-sizing: border-box;
5+
}
6+
7+
body {
8+
padding: 2rem;
9+
background-color: #333;
10+
color: whitesmoke;
11+
min-height: 100vh;
12+
display: flex;
13+
flex-direction: column;
14+
align-items: center;
15+
gap: 2rem;
16+
font-size: 2rem;
17+
}
18+
19+
input, button {
20+
font-size: 2rem;
21+
padding: 1rem;
22+
border-radius: 10px;
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>City Not Found</title>
8+
<link href="{{ url_for('static', filename='styles/style.css') }}" rel="stylesheet" />
9+
</head>
10+
11+
<body>
12+
<h1>City Not Found</h1>
13+
<h2>Try Again?</h2>
14+
<form action="/weather">
15+
<input type="text" name="city" id="city" placeholder="Enter a City" />
16+
<button type="submit">Submit</button>
17+
</form>
18+
</body>
19+
20+
</html>

lesson23/templates/index.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Get Weather Conditions</title>
8+
<link href="{{ url_for('static', filename='styles/style.css') }}" rel="stylesheet" />
9+
</head>
10+
11+
<body>
12+
<h1>Get Weather Conditions</h1>
13+
<form action="/weather">
14+
<input type="text" name="city" id="city" placeholder="Enter a City" />
15+
<button type="submit">Submit</button>
16+
</form>
17+
</body>
18+
19+
</html>

lesson23/templates/weather.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>{{ title }} Weather</title>
8+
<link href="{{ url_for('static', filename='styles/style.css') }}" rel="stylesheet" />
9+
</head>
10+
11+
<body>
12+
<h1>{{ title }} Weather</h1>
13+
<p>{{ status }} and {{ temp }} &deg;</p>
14+
<p>Feels like {{ feels_like }} &deg;</p>
15+
16+
<form action="/weather">
17+
<input type="text" name="city" id="city" placeholder="Enter a City" />
18+
<button type="submit">Submit</button>
19+
</form>
20+
</body>
21+
22+
</html>

lesson23/weather.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from dotenv import load_dotenv
2+
from pprint import pprint
3+
import requests
4+
import os
5+
6+
load_dotenv()
7+
8+
9+
def get_current_weather(city="Kansas City"):
10+
11+
request_url = f'http://api.openweathermap.org/data/2.5/weather?appid={os.getenv("API_KEY")}&q={city}&units=imperial'
12+
13+
weather_data = requests.get(request_url).json()
14+
15+
return weather_data
16+
17+
18+
if __name__ == "__main__":
19+
print('\n*** Get Current Weather Conditions ***\n')
20+
21+
city = input("\nPlease enter a city name: ")
22+
23+
# Check for empty strings or string with only spaces
24+
if not bool(city.strip()):
25+
city = "Kansas City"
26+
27+
weather_data = get_current_weather(city)
28+
29+
print("\n")
30+
pprint(weather_data)

0 commit comments

Comments
 (0)