-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1dc1c53
commit 574bfef
Showing
9 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.venv | ||
.env | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
blinker==1.7.0 | ||
certifi==2023.11.17 | ||
charset-normalizer==3.3.2 | ||
click==8.1.7 | ||
Flask==3.0.0 | ||
idna==3.6 | ||
itsdangerous==2.1.2 | ||
Jinja2==3.1.2 | ||
MarkupSafe==2.1.3 | ||
python-dotenv==1.0.0 | ||
requests==2.31.0 | ||
urllib3==2.1.0 | ||
waitress==2.1.2 | ||
Werkzeug==3.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from flask import Flask, render_template, request | ||
from weather import get_current_weather | ||
from waitress import serve | ||
|
||
|
||
app = Flask(__name__) | ||
|
||
@app.route('/') | ||
@app.route('/index') | ||
def index(): | ||
return render_template('index.html') | ||
|
||
@app.route('/weather') | ||
def get_weather(): | ||
city = request.args.get("city") | ||
|
||
# Check for empth strings or strings only with speces | ||
if not bool(city.strip()): | ||
city = "Kansas City" | ||
|
||
weather_data = get_current_weather(city) | ||
|
||
# The network is shut down. | ||
if weather_data['cod'] == 300: | ||
return render_template('network-shut-down.html') | ||
|
||
# The city is not found by API | ||
if not weather_data['cod'] == 200: | ||
return render_template('city-not-found.html') | ||
|
||
return render_template( | ||
'weather.html', | ||
title=weather_data['name'], | ||
status=weather_data['weather'][0]['description'].capitalize(), | ||
temp=f"{weather_data['main']['temp']:.1f}", | ||
feels_like=f"{weather_data['main']['feels_like']:.1f}" | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
serve(app, host='0.0.0.0', port=8000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
* { | ||
margin: 0; | ||
padding: 0; | ||
box-sizing: border-box; | ||
} | ||
|
||
body { | ||
padding: 2rem; | ||
background-color: #333; | ||
color: whitesmoke; | ||
min-height: 100vh; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
gap: 2rem; | ||
font-size: 2rem; | ||
} | ||
|
||
input, button { | ||
font-size: 2rem; | ||
padding: 1rem; | ||
border-radius: 10px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>City Not Found</title> | ||
<link href="{{ url_for('static', filename='styles/style.css') }}" rel="stylesheet" /> | ||
</head> | ||
<body> | ||
<h1>City Not Found</h1> | ||
<h2>Try Again?</h2> | ||
<form action="/weather"> | ||
<input type="text" name="city" id="city" placeholder="Enter a City" /> | ||
<button type="submit">Submit</button> | ||
</form> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Get weather Conditions</title> | ||
<link href="{{ url_for('static', filename='styles/style.css') }}" rel="stylesheet" /> | ||
</head> | ||
<body> | ||
<h1>Get weather Conditions</h1> | ||
<form action="/weather"> | ||
<input type="text" name="city" id="city" placeholder="Enter a City" /> | ||
<button type="submit">Submit</button> | ||
</form> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Network Shut Down</title> | ||
<link href="{{ url_for('static', filename='styles/style.css') }}" rel="stylesheet" /> | ||
</head> | ||
<body> | ||
<h1>Network Shut Down</h1> | ||
<h2>Try Again?</h2> | ||
<form action="/weather"> | ||
<input type="text" name="city" id="city" placeholder="Enter a City" /> | ||
<button type="submit">Submit</button> | ||
</form> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>{{ title }} Weather</title> | ||
<link href="{{ url_for('static', filename='styles/style.css') }}" rel="stylesheet" /> | ||
</head> | ||
<body> | ||
<h1>{{ title }} Weather</h1> | ||
<p>{{ status }} and {{ temp }} °</p> | ||
<p>Feels like {{ feels_like }} °</p> | ||
|
||
<form action="/weather"> | ||
<input type="text" name="city" id="city" placeholder="Enter a City" /> | ||
<button type="submit">Submit</button> | ||
</form> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from dotenv import load_dotenv | ||
from pprint import pprint | ||
import requests | ||
import os | ||
|
||
load_dotenv() | ||
|
||
def get_current_weather(city="New York"): | ||
|
||
request_url=f"https://api.openweathermap.org/data/2.5/weather?appid={os.getenv('API_KEY')}&q={city}&units=metric" | ||
|
||
try: | ||
weather_data = requests.get(request_url).json() | ||
except: | ||
weather_data = { | ||
'cod': 300, | ||
'message': 'The network is shut down.' | ||
} | ||
|
||
return weather_data | ||
|
||
if __name__ == "__main__": | ||
print("\n*** Get Current Weather Conditions ***\n") | ||
city = input("\nPlease enter a city name: \n") | ||
|
||
# Check for empth strings or strings only with speces | ||
if not bool(city.strip()): | ||
city = "Kansas City" | ||
|
||
weather_data = get_current_weather(city) | ||
pprint(weather_data) | ||
|
||
print("\n*** Thanks For Your Request ***\n") |