Eksamens/flsk.py

105 lines
2.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from flask import Flask, render_template, request, send_from_directory, redirect, url_for
from markupsafe import escape
app = Flask(__name__)
@app.route('/index', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
print("sdfsdfsdfsdfs")
return render_template('index.html', show_text=True)
else:
return render_template('index.html', show_text=False)
@app.route("/")
def home():
return render_template("index.html")
@app.route("/dwnld")
def dwnld_page():
return render_template("download.html")
@app.route("/download")
def download():
return send_from_directory('game', 'Game-master.zip')
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/contact", methods=["GET", "POST"])
def contact():
if request.method == "POST":
name = request.form.get("name")
email = request.form.get("email")
message = request.form.get("message")
return redirect(url_for('thank_you', name=name, email=email, message=message))
return render_template("contact.html")
@app.route("/thank_you/<name>/<email>/<message>")
def thank_you(name,email,message):
return render_template("thank_you.html", name=name, email=email, message=message)
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "POST":
username = request.form.get("username")
password = request.form.get("password")
if username and password:
return redirect(url_for('index'),code=307)
else:
return render_template("login.html")
return render_template("login.html")
@app.route('/')
def button_page():
return render_template('login.html')
def get_latvia_time():
try:
url = "http://worldtimeapi.org/api/timezone/Europe/Riga"
response = requests.get(url)
data = response.json()
latvia_time = data["datetime"]
return latvia_time
except Exception as e:
return f"Ошибка при получении времени: {e}"
def get_weather_in_riga():
api_key = "e53814d3c7b076180e8d0273015b2804"
city_id = 456173
url = f"https://api.openweathermap.org/data/2.5/weather?id={city_id}&appid={api_key}&units=metric"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
weather_description = data["weather"][0]["description"]
temperature = data["main"]["temp"]
humidity = data["main"]["humidity"]
return f"Сейчас в Риге {weather_description}. Температура: {temperature}°C, Влажность: {humidity}%"
else:
return "Не удалось получить данные о погоде."
@app.route("/")
def weather_route():
return get_weather_in_riga()
if __name__ == "__main__":
app.run(debug=True)