Skip to main content

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.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hue-fastapi 0.0.1
File Size Uploaded Provenance Actions
hue_fastapi-0.0.1.tar.gz 63.6 kB Without provenance Details for hue_fastapi-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hue-fastapi 0.0.1
File Interpreter ABI Platform Actions
hue_fastapi-0.0.1-py3-none-any.whl Python 3 none any Details for hue_fastapi-0.0.1-py3-none-any.whl

Total release size: 81.8 kB

Release files / hue_fastapi-0.0.1.tar.gz

Download URL hue_fastapi-0.0.1.tar.gz
Size 63.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f1afd7096f0184746c0f8ca747b475e06f65f0d1ba0b028baef14fc45602e16c
BLAKE2b-256 checksum
How to use checksums
25459add5255aa33d02767c04a1b76f56f6c5833080046422ff037b7218d91ed
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.1-py3-none-any.whl

Download URL hue_fastapi-0.0.1-py3-none-any.whl
Size 18.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2bfcb6761ed70d10e43e73b385eaaedb72bf0b6aaf34219bc10cd241c5d17b10
BLAKE2b-256 checksum
How to use checksums
411341205e1778491c219d9ac9854437057967b1a2dde49090b711c45a57e92a
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.1

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.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page