Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 18 additions & 25 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,36 @@
from flask import Flask, render_template, request
from weather import get_current_weather
from waitress import serve # production server

from waitress import serve

app = Flask(__name__)


@app.route("/")
@app.route("/index")
def index():
# return "Hello, World!"
return render_template("index.html")


@app.route("/weather")
def weather():
city = request.args.get("city")
city = request.args.get("city", "").strip()

# check for empty strings and string with only spaces
if not bool(city.strip()):
if not city:
city = "Locquirec"

# Retrieve weather data from the API
weather_data = get_current_weather(city)

# City is not found by the 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}",
)

try:
weather_data = get_current_weather(city)
if "cod" not in weather_data or weather_data["cod"] != 200:
return render_template("city-not-found.html", city=city)

return render_template(
"weather.html",
title=weather_data.get("name", "Unknown City"),
status=weather_data.get("weather", [{}])[0].get("description", "No data").capitalize(),
temp=f"{weather_data['main'].get('temp', 'N/A'):.1f}",
feels_like=f"{weather_data['main'].get('feels_like', 'N/A'):.1f}",
)
except Exception as e:
return f"Error: {str(e)}", 500

if __name__ == "__main__":
# app.run(host="0.0.0.0", port=8000) # development server
print("\n ** Starting the server ** \n")
serve(app, host="0.0.0.0", port=8000) # using waitress for production server
serve(app, host="0.0.0.0", port=8000)