hue-fastapi
FastAPI integration for hue. It provides class-based views that render hue pages and Alpine AJAX fragments, a router that reads Starlette's path syntax, signed double-submit CSRF protection, and routes that serve hue's CSS and JS from the installed package.
Install
uv add hue-fastapi
from fastapi import FastAPI
from hue_fastapi import CsrfConfig, configure_hue
app = FastAPI()
configure_hue(app, csrf=CsrfConfig(secret_key=SECRET_KEY)) # also serves /__hue__/ assets
You must pass csrf. Setting it to None turns hue's CSRF checks off, so the choice is always explicit.
Usage
from hue import html
from hue.context import HueContext
from hue_fastapi import HueView, Page, Router
from starlette.requests import Request
class LoginView(HueView):
router = Router[Request]()
async def index(self, request: Request, context: HueContext[Request]) -> Page:
return Page(title="Login", body=html.div("Login page"))
@router.fragment_post("login/")
async def login(self, request: Request, context: HueContext[Request]):
return html.div("Login successful")
app.include_router(LoginView.api_router, prefix="/login")
index serves the full page. Fragment routes are AJAX-only and return HTML fragments. A handler can be sync or async; sync handlers run in a thread pool. A handler can also return a Starlette response, such as a redirect, which is passed through unchanged.
Path parameters use Starlette's syntax ("items/{item_id:int}/"). Any parameter after request and context goes to FastAPI, so you can use path parameters, query parameters and Depends as in any other endpoint. To have the body parsed and validated with Pydantic, declare a typed body parameter. If validation fails, the view returns 422 through handle_body_validation_error, which you can override.
HueFragmentsView works like HueView without an index, for grouping related fragments.
CSRF
Every unsafe hue route (anything but GET, HEAD, OPTIONS and TRACE) checks the token. The Alpine bundle sends it as X-CSRFToken on AJAX requests. A form that posts without AJAX needs hue_fastapi.ui.CsrfTokenInput inside it. To protect one of your own endpoints the same way, add Depends(csrf_protect) to it (from hue_fastapi import csrf_protect).
The token is a signed value in a cookie, checked against the copy the request sends in the X-CSRFToken header or the csrf_token form field. The signature means the server only accepts tokens it minted. A token is not tied to one browser, so a sibling subdomain could plant one it fetched from the site. With cookie_secure=True the cookie is named with the __Host- prefix, which browsers accept only from the host itself over HTTPS, and that closes the gap.
CsrfConfig takes these settings besides secret_key:
| Setting | Default | What it sets |
|---|---|---|
cookie_name |
"csrftoken" |
The cookie's name. |
cookie_secure |
False |
Send the cookie over HTTPS only, under the __Host- prefix. Set it to True in production. |
cookie_samesite |
"lax" |
The cookie's SameSite attribute. |
max_age |
52 weeks | How long the cookie lives, in seconds. |
Toasts across a redirect
A toast raised before a redirect is shown on the next page. This needs a session. Add HueRequestMiddleware, then Starlette's SessionMiddleware; Starlette runs the middleware added last first, so the session wraps hue's middleware:
from starlette.middleware.sessions import SessionMiddleware
from hue_fastapi import HueRequestMiddleware
app.add_middleware(HueRequestMiddleware)
app.add_middleware(SessionMiddleware, secret_key=SECRET_KEY)
The same middleware lets toast() and shortcuts.use() work in plain FastAPI endpoints, not only in hue routes.
Settings
configure_hue also accepts these keyword arguments:
| Argument | Default | What it sets |
|---|---|---|
css_url |
hue's own stylesheet | Your stylesheet, if you build Tailwind with your own classes. |
js_url |
hue's own bundle | Your script bundle, if you build your own. |
extra_css_urls |
() |
Stylesheets loaded after hue's. |
html_title_factory |
"{title} - Hue" |
A function that formats the document title. |
formats |
Formats() |
Date and time formats. |
shortcuts |
None |
The app's ShortcutRegistry, bound on every page. |
Metadata
Release files for hue-fastapi 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| hue_fastapi-0.0.2.tar.gz | 63.6 kB | Without provenance | Details for hue_fastapi-0.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| hue_fastapi-0.0.2-py3-none-any.whl | Python 3 | none | any | Details for hue_fastapi-0.0.2-py3-none-any.whl |
Total release size: 81.8 kB
Release files / hue_fastapi-0.0.2.tar.gz
| Download URL | hue_fastapi-0.0.2.tar.gz |
|---|---|
| Size | 63.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a1e68d6866fbaad3fa503efc915a257f0174973e3a11af53ce85b3ffc0dba946
|
|
BLAKE2b-256 checksum How to use checksums |
3f832e581b1b9874e632a0d44e2e626bd7373e1fc937fbd09affbdfaed77af94
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / hue_fastapi-0.0.2-py3-none-any.whl
| Download URL | hue_fastapi-0.0.2-py3-none-any.whl |
|---|---|
| Size | 18.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c6162b4a68429d0df206f182472b651ea72bbbf359bf4796a95bbbd8e6ddfc8d
|
|
BLAKE2b-256 checksum How to use checksums |
f784b36fab78e5d153467e626d552d2fa3759f3f9db138f756955a68b7e3dc88
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for hue-fastapi 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.