Используйте декоратор @app.route()
для определения маршрута:
@app.route("/hello")
def hello():
return "This is a new route!"
Вы можете передавать параметры через URL:
@app.route("/user/<username>")
def show_user(username):
return f"User: {username}"
Flask использует систему шаблонов Jinja2, которая поддерживает вставку Python-кода в HTML.
Файл templates/index.html
:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Hello, !</h1>
</body>
</html>
Подробнее про html теги можно узнать здесь.
Функция для рендеринга шаблона:
from flask import render_template
@app.route("/")
def home():
return render_template("index.html", title="My App", name="Flask")