ДокументацияPython фреймворки
От Flask до Некси
В Nexy представление Flask становится файлом в папке src/routes/: файлом .py для JSON, файлом .nexy для HTML, при этом Jinja2 сохраняется, а декораторы, чертежи и request глобальные исчезают.
В каждом разделе ниже показана привычка Flask, затем то же самое в Nexy.
Маршрут
from flask import Flask app = Flask(__name__) @app.get("/api/hello")def hello(): return {"message": "Hello"}Путь к файлу - это URL-адрес, а имя функции - это метод:
async def GET(): return {"message": "Hello"}URL-переменные
@app.get("/api/users/<int:id>")def get_user(id): return {"id": id}Конвертер становится аннотацией типа, а переменная - сегментом [id]:
async def GET(id: int): return {"id": id}<path:rest> - это универсальный сегмент, [...rest].py.
Строки запроса
from flask import request @app.get("/api/search")def search(): q = request.args.get("q", "") page = request.args.get("page", 1, type=int) return {"q": q, "page": page}Глобального request нет: параметр запроса - это параметр функции, преобразованный и проверенный:
async def GET(q: str = "", page: int = 1): return {"q": q, "page": page}Тело JSON
@app.post("/api/items")def create_item(): data = request.get_json() if "name" not in data: return {"error": "name is required"}, 400 return {"created": data["name"]}, 201Модель Pydantic проверяет тело, а плохая отвечает на 422 самостоятельно:
from pydantic import BaseModel from nexy.http import JSONResponse class NewItem(BaseModel): name: str async def POST(item: NewItem): return JSONResponse({"created": item.name}, status_code=201)Несколько методов на одном URL
@app.route("/api/notes/<int:id>", methods=["GET", "DELETE"])def note(id): if request.method == "DELETE": return {"deleted": id} return {"id": id}Одна функция на метод в одном файле:
async def GET(id: int): return {"id": id} async def DELETE(id: int): return {"deleted": id}Шаблоны
from flask import render_template @app.get("/users")def users(): return render_template("users.html", users=[{"name": "Ada"}, {"name": "Alan"}])Представление и его шаблон - это один файл .nexy: Python над строкой ---, тот же Jinja2 снизу:
users = [{"name": "Ada"}, {"name": "Alan"}]<ul> {% for user in users %} <li>{{ user.name }}</li> {% endfor %}</ul>Базовый шаблон
<!DOCTYPE html><html> <body>{% block content %}{% endblock %}</body></html>layout.nexy оборачивает каждую страницу своей папки и ниже; страницы не {% extends %} это:
children: prop[NexyElement]<!DOCTYPE html><html> <body>{{ children | safe }}</body></html>Макросы и включает в себя
{% macro card(title) %}<article><h2>{{ title }}</h2>{{ caller() }}</article>{% endmacro %}Компонент представляет собой файл .nexy с реквизитами, импортированный во вступительной части:
title: prop[str]<article> <h2>{{ title }}</h2> <Slot /></article>from "@/components/Card.nexy" import Card<Card title="Latest"> <p>Everything published this week.</p></Card>@ - это src/: nx new записывает useAliases = {"@": "src"} в nexyconfig.py.
Чертежи
from flask import Blueprint admin = Blueprint("admin", __name__, url_prefix="/admin") @admin.get("/stats")def stats(): return {"users": 3} app.register_blueprint(admin)Папка - это префикс, и ничего не прописано:
async def GET(): return {"users": 3}before_request
from flask import abort @admin.before_requestdef require_admin(): if request.headers.get("X-Role") != "admin": abort(403)dependencies.py выполняется перед каждым маршрутом в своей папке и ниже:
from nexy.http import Header, HTTPException def require_admin(x_role: str = Header("")) -> None: if x_role != "admin": raise HTTPException(status_code=403, detail="Admins only") dependencies = [require_admin]after_request
@app.after_requestdef add_header(response): response.headers["X-App"] = "demo" return responsemiddleware.py видит запрос перед маршрутом и ответ после него:
from nexy.http import Request async def middleware(request: Request, call_next): response = await call_next(request) response.headers["x-app"] = "demo" return responseФормы и редиректы
from flask import redirect, url_for @app.post("/notes")def add_note(): NOTES.append(request.form["title"]) return redirect(url_for("notes"))Действие - это функция Python, в которую отправляется форма. Nexy задает ему путь, считывает поля формы как параметры и отправляет посетителя обратно на страницу с помощью 303:
from nexy.decorators import Action NOTES: list[str] = [] @Actionasync def add_note(title: str) -> None: NOTES.append(title)from src.actions.notes import NOTES<form method="post" action="{{ __actions_path['add_note'] }}"> <input name="title"> <button type="submit">Add</button></form><ul> {% for note in NOTES %} <li>{{ note }}</li> {% endfor %}</ul>Сессии
from flask import session app.secret_key = "change-me" @app.post("/api/login")def login(): session["user"] = request.json["name"] return {"user": session["user"]}Секрет перемещается в nexyconfig.py, и сеанс зависает на запросе:
from nexy.core.models import NexyConfigModel class NexyConfig(NexyConfigModel): useSession = {"secret_key": "change-me"}from pydantic import BaseModel from nexy.http import Request class Login(BaseModel): name: str async def POST(request: Request, body: Login): request.session["user"] = body.name return {"user": body.name}Страница читает его с помощью useSession():
from nexy import useSession user = useSession().get("user", "guest")<h1>Hello {{ user }}</h1>abort(404) и обработчики ошибок
@app.errorhandler(404)def not_found(error): return render_template("404.html"), 404 @app.get("/posts/<slug>")def post(slug): if slug not in POSTS: abort(404) return render_template("post.html", post=POSTS[slug])notFound() останавливает страницу, а notfound.nexy рисует ответ:
from nexy import notFound POSTS = {"hello": "Hello, world"} slug: prop[str]if slug not in POSTS: notFound()<h1>{{ POSTS[slug] }}</h1><h1>Not found</h1><p><a href="/">Back home</a></p>Статические файлы
static/logo.png Flask: /static/logo.pngpublic/logo.png Nexy: /logo.png
Колба-КОРС
from flask_cors import CORS CORS(app, origins=["https://example.com"])from nexy.core.models import NexyConfigModel class NexyConfig(NexyConfigModel): useCORS = {"allow_origins": ["https://example.com"]}Тесты
from app import app def test_hello(): assert app.test_client().get("/api/hello").json == {"message": "Hello"}from fastapi.testclient import TestClient from nexy.app import app def test_hello(): assert TestClient(app).get("/api/hello").json() == {"message": "Hello"}Бег
| Flask | Некси |
|---|---|
flask run --debug |
nx dev |
gunicorn app:app |
nx build, затем nx start |
flask db migrate (Flask-Migrate) |
nx migrate -m "message" |
| нет документации по API | /docs, список всех .py маршрутов |
Следующие шаги
- От FastAPI до Nexy: обработчики Nexy - это обработчики FastAPI
- Действия
- файлы .nexy
- Рецепты: вход в систему, CRUD и загрузка