Add a per-request page-context hook

homepage puts the same handful of things on every page - who is logged in,
whether the weather and system widgets are enabled, whether to show the
admin link. Flask did that with two context processors. There is no
equivalent here, and passing the same eight keys at every render site is
the kind of repetition that goes stale one call at a time.

`ui.page_context` is a callable taking a Request and returning a dict. It
runs per render rather than once at startup, because what it returns depends
on who is asking: which group a visitor is in decides whether a tile is even
mentioned. An explicit value at the render site still wins, which is what
the test pins down.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Michał Kopeć 2026-10-04 19:26:32 +02:00
parent bf840f1c5c
commit ef35636694
5 changed files with 52 additions and 3 deletions

View file

@ -78,7 +78,7 @@ Pin a tag, not a branch:
```toml
dependencies = [
"chrome @ git+https://git.wierzbowa.cloud/michal/chrome@v0.2.0",
"chrome @ git+https://git.wierzbowa.cloud/michal/chrome@v0.3.0",
]
```
@ -106,3 +106,14 @@ chrome's assets then live at `/ui/chrome/...` and the service's at
this: its design (D-0008) puts everything behind exactly two proxied
prefixes, `/ui` and `/api`, so a third top-level path would be unreachable
behind Caddy.
## Per-request page context
Flask's context processors, with the app object taken out:
```python
ui.page_context = lambda request: {"user": current_user(request)}
```
It runs on every render, because what it returns usually depends on who is
asking. An explicit value passed at the render site wins over it.

View file

@ -7,7 +7,7 @@ need an `if service == "marketplace"`, it belongs in the service.
from pathlib import Path
__version__ = "0.2.0"
__version__ = "0.3.0"
PACKAGE_DIR = Path(__file__).parent
STATIC_DIR = PACKAGE_DIR / "static"

View file

@ -101,6 +101,11 @@ class Chrome:
wa_base=self.wa_path,
)
#: Optional per-request context every page gets, set by the service.
#: Flask had context processors; this is the same idea with the app
#: object taken out of it. Takes a Request, returns a dict.
self.page_context = None
app.include_router(health_router(service, version))
self._install_language_route()
self._install_error_pages()
@ -129,7 +134,9 @@ class Chrome:
def context(self, request: Request, **extra: object) -> dict[str, object]:
locale = self.translations.locale_for(request)
shared = self.page_context(request) if self.page_context else {}
return {
**shared,
"title": self.title,
"subtitle": self.subtitle,
"service": self.service,

View file

@ -1,6 +1,6 @@
[project]
name = "chrome"
version = "0.2.0"
version = "0.3.0"
description = "Shared page shell, design tokens and service helpers for the internal services."
requires-python = ">=3.13"
dependencies = [

View file

@ -279,3 +279,34 @@ def test_a_service_can_move_both_static_trees_under_one_prefix(tmp_path):
).status_code == 200
# The default prefix must still be empty for services that want it.
assert client.get("/chrome/tokens.css").status_code == 404
def test_a_service_can_inject_per_request_context(tmp_path):
"""Flask's context processors, without the app object.
The hook runs per render, not once at startup, because what it returns
depends on who is asking - which group a visitor is in decides whether
a tile is even mentioned.
"""
templates = tmp_path / "templates"
templates.mkdir()
(templates / "index.html").write_text(
'{% extends "base.html" %}{% block content %}<p>{{ who }}</p>{% endblock %}'
)
app = FastAPI()
ui = Chrome(app, service="t", version="1", title="T", templates_dir=templates)
ui.page_context = lambda request: {"who": request.headers.get("x-who", "nobody")}
@app.get("/")
def page(request: Request):
return ui.render(request, "index.html")
client = TestClient(app)
assert "<p>alice</p>" in client.get("/", headers={"x-who": "alice"}).text
assert "<p>nobody</p>" in client.get("/").text
# An explicit value at the render site beats the hook.
@app.get("/explicit")
def explicit(request: Request):
return ui.render(request, "index.html", who="override")
assert "<p>override</p>" in TestClient(app).get("/explicit").text