DocsPython frameworks
From Flask to Nexy
In Nexy a Flask view becomes a file under src/routes/: a .py file for JSON, a .nexy file for
HTML, with Jinja2 kept and the decorators, blueprints and request global gone.
Each section below shows a Flask habit, then the same thing in Nexy.
A route
from flask import Flask app = Flask(__name__) @app.get("/api/hello")def hello(): return {"message": "Hello"}The file path is the URL and the function name is the method:
async def GET(): return {"message": "Hello"}URL variables
@app.get("/api/users/<int:id>")def get_user(id): return {"id": id}The converter becomes a type annotation, and the variable a [id] segment:
async def GET(id: int): return {"id": id}<path:rest> is a catch-all segment, [...rest].py.
Query strings
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}There is no request global: a query parameter is a function parameter, converted and validated:
async def GET(q: str = "", page: int = 1): return {"q": q, "page": page}A JSON body
@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"]}, 201A Pydantic model validates the body, and a bad one answers 422 on its own:
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)Several methods on one URL
@app.route("/api/notes/<int:id>", methods=["GET", "DELETE"])def note(id): if request.method == "DELETE": return {"deleted": id} return {"id": id}One function per method, in the same file:
async def GET(id: int): return {"id": id} async def DELETE(id: int): return {"deleted": id}Templates
from flask import render_template @app.get("/users")def users(): return render_template("users.html", users=[{"name": "Ada"}, {"name": "Alan"}])The view and its template are one .nexy file: Python above the --- line, the same Jinja2 below:
users = [{"name": "Ada"}, {"name": "Alan"}]<ul> {% for user in users %} <li>{{ user.name }}</li> {% endfor %}</ul>A base template
<!DOCTYPE html><html> <body>{% block content %}{% endblock %}</body></html>A layout.nexy wraps every page of its folder and below; pages do not {% extends %} it:
children: prop[NexyElement]<!DOCTYPE html><html> <body>{{ children | safe }}</body></html>Macros and includes
{% macro card(title) %}<article><h2>{{ title }}</h2>{{ caller() }}</article>{% endmacro %}A component is a .nexy file with props, imported in the frontmatter:
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>@ is src/: nx new writes useAliases = {"@": "src"} in nexyconfig.py.
Blueprints
from flask import Blueprint admin = Blueprint("admin", __name__, url_prefix="/admin") @admin.get("/stats")def stats(): return {"users": 3} app.register_blueprint(admin)A folder is the prefix, and nothing is registered:
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)A dependencies.py runs before every route of its folder and below:
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 responseA middleware.py sees the request before the route and the response after it:
from nexy.http import Request async def middleware(request: Request, call_next): response = await call_next(request) response.headers["x-app"] = "demo" return responseForms and redirects
from flask import redirect, url_for @app.post("/notes")def add_note(): NOTES.append(request.form["title"]) return redirect(url_for("notes"))An action is a Python function the form posts to. Nexy gives it a path, reads the form fields as its
parameters, and sends the visitor back to the page with a 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>Sessions
from flask import session app.secret_key = "change-me" @app.post("/api/login")def login(): session["user"] = request.json["name"] return {"user": session["user"]}The secret moves to nexyconfig.py, and the session hangs off the request:
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}A page reads it with useSession():
from nexy import useSession user = useSession().get("user", "guest")<h1>Hello {{ user }}</h1>abort(404) and error handlers
@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() stops the page, and notfound.nexy draws the answer:
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 files
static/logo.png Flask: /static/logo.pngpublic/logo.png Nexy: /logo.png
Flask-CORS
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"]}Tests
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"}Running
| Flask | Nexy |
|---|---|
flask run --debug |
nx dev |
gunicorn app:app |
nx build, then nx start |
flask db migrate (Flask-Migrate) |
nx migrate -m "message" |
| no API docs | /docs, listing every .py route |
Next steps
- From FastAPI to Nexy: Nexy handlers are FastAPI handlers
- Actions
- .nexy files
- Recipes: sign-in, CRUD and uploads