Update flsk.py

main
Mihaeļs Mejerovs 2024-05-22 17:06:01 +00:00
parent ac871f29a0
commit c8fe330491
1 changed files with 125 additions and 118 deletions

243
flsk.py
View File

@ -1,118 +1,125 @@
import os
from flask import Flask, render_template, request, send_from_directory, redirect, url_for
import uuid
import csv
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":
# Get form data
username = request.form.get("username")
password = request.form.get("password")
# Here you would typically validate the user's credentials
# For this example, let's assume the user is valid
# Redirect the user to their account page
return redirect(url_for('account', name=username))
return render_template("login.html")
@app.route("/account/<name>")
def account(name):
# Render the account page with the user's name
return render_template("account.html", name=name)
if __name__ == "__main__":
app.run(debug=True)
@app.route('/')
def button_page():
return render_template('account.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)
#CSV_FILE_PATH = 'data.csv'
#@app.route('/')
#def login1():
# return render_template('login.html')
#@app.route('/login', methods=['POST'])
#def submit():
#username = request.form['username']
#password = request.form['password']
# Проверка существования файла, если нет - создаем с заголовками
#if not os.path.exists(CSV_FILE_PATH):
#with open(CSV_FILE_PATH, mode='w', newline='') as file:
# writer = csv.writer(file)
# writer.writerow(['username', 'password'])
# Запись данных в CSV файл
#with open(CSV_FILE_PATH, mode='a', newline='') as file:
# writer = csv.writer(file)
# writer.writerow([username, password])
# return redirect(url_for('login1'))
#if __name__ == '__main__':
# app.run(debug=True)
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 = "ВАШ_КЛЮЧ"
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()