Eksamens/flsk.py

125 lines
3.4 KiB
Python
Raw 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
app = Flask(__name__)
@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":
# Get form data
name = request.form.get("name")
email = request.form.get("email")
message = request.form.get("message")
# Here you can process the message, send emails, save to database, etc.
# let's just print the data to the console
print(f"Name: {name}, Email: {email}, Message: {message}")
return render_template("thank_you.html", name=name)
return render_template("contact.html")
@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', name=username))
else:
# Если поля пустые, оставляем пользователя на странице "login"
return render_template("login.html")
return render_template("login.html")
@app.route("/index")
def index():
# Здесь можно добавить логику для страницы "index"
return render_template("index.html")
if __name__ == "__main__":
app.run()
@app.route('/')
def button_page():
return render_template('login.html')
@app.route('/index', methods=['GET', 'POST'])
def display_text():
if request.method == 'POST':
return render_template('index.html', show_text=True)
else:
return render_template('index.html', show_text=False)
if __name__ == '__main__':
app.run(debug=True)
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}"
if __name__ == "__main__":
latvia_time = get_latvia_time()
print(f"Текущее время в Латвии: {latvia_time}")
def get_weather_in_riga():
api_key = "e53814d3c7b076180e8d0273015b2804"
city_id = 456173 # ID города Рига
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()