fasthx.jinja
ComponentHeader
dataclass
Bases: ComponentHeader[str]
RequestComponentSelector
for Jinja templates that selects the rendered template
based on a request header.
Source code in fasthx/jinja.py
Jinja
dataclass
Jinja2 renderer utility with FastAPI route decorators.
Source code in fasthx/jinja.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
|
make_context = JinjaContext.unpack_result
class-attribute
instance-attribute
Function that will be used by default to convert a route's return value into
a Jinja rendering context. The default value is JinjaContext.unpack_result
.
no_data = field(default=False, kw_only=True)
class-attribute
instance-attribute
If set, hx()
routes will only accept HTMX requests.
Note that if this property is True
, then the hx()
decorator's no_data
argument
will have no effect.
templates
instance-attribute
The Jinja2 templates of the application.
hx(template, *, error_template=None, make_context=None, no_data=False, prefix=None)
Decorator for rendering a route's result if the request was an HTMX one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
template
|
ComponentSelector[str]
|
The Jinja2 template selector to use. |
required |
error_template
|
ComponentSelector[str] | None
|
The Jinja2 template selector to use for route error rendering. |
None
|
make_context
|
JinjaContextFactory | None
|
Route-specific override for the |
None
|
no_data
|
bool
|
If set, the route will only accept HTMX requests. |
False
|
prefix
|
str | None
|
Optional template name prefix. |
None
|
Returns:
Type | Description |
---|---|
Callable[[MaybeAsyncFunc[P, Any]], Callable[P, Coroutine[None, None, Any | Response]]]
|
The rendered HTML for HTMX requests, otherwise the route's unchanged return value. |
Source code in fasthx/jinja.py
page(template, *, error_template=None, make_context=None, prefix=None)
Decorator for rendering a route's result.
This decorator triggers HTML rendering regardless of whether the request was HTMX or not.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
template
|
ComponentSelector[str]
|
The Jinja2 template selector to use. |
required |
error_template
|
ComponentSelector[str] | None
|
The Jinja2 template selector to use for route error rendering. |
None
|
make_context
|
JinjaContextFactory | None
|
Route-specific override for the |
None
|
prefix
|
str | None
|
Optional template name prefix. |
None
|
Source code in fasthx/jinja.py
JinjaContext
Core JinjaContextFactory
implementations.
Source code in fasthx/jinja.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
|
unpack_object(obj)
classmethod
Utility function that unpacks an object into a dict
.
Supports dict
and Collection
instances, plus anything with __dict__
or __slots__
attributes, for example Pydantic models, dataclasses, or "standard" class instances.
Conversion rules:
dict
: returned as is.Collection
: returned as{"items": obj}
, available in templates asitems
.- Objects with
__dict__
or__slots__
: known keys are taken from__dict__
or__slots__
and the context will be created as{key: getattr(route_result, key) for key in keys}
, omitting property names starting with an underscore. For Pydantic models, computed fields will also be included. None
is converted into an empty context.
Raises:
Type | Description |
---|---|
ValueError
|
If the given object can not be handled by any of the conversion rules. |
Source code in fasthx/jinja.py
unpack_result(*, route_result, route_context)
classmethod
Jinja context factory that tries to reasonably convert non-dict
route results
to valid Jinja contexts (the route_context
argument is ignored).
Supports everything that JinjaContext.unpack_object()
does and follows the same
conversion rules.
Raises:
Type | Description |
---|---|
ValueError
|
If |
Source code in fasthx/jinja.py
unpack_result_with_route_context(*, route_result, route_context)
classmethod
Jinja context factory that tries to reasonably convert non-dict
route results
to valid Jinja contexts, also including every key-value pair from route_context
.
Supports everything that JinjaContext.unpack_object()
does and follows the same
conversion rules.
Raises:
Type | Description |
---|---|
ValueError
|
If |
Source code in fasthx/jinja.py
use_converters(convert_route_result, convert_route_context=None)
classmethod
Creates a JinjaContextFactory
that uses the provided functions to convert
route_result
and route_context
to a Jinja context.
The returned JinjaContextFactory
raises a ValueError
if the overlapping keys are found.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
convert_route_result
|
Callable[[Any], dict[str, Any]] | None
|
Function that takes |
required |
convert_route_context
|
Callable[[dict[str, Any]], dict[str, Any]] | None
|
Function that takes |
None
|
Returns:
Type | Description |
---|---|
JinjaContextFactory
|
The created |
Source code in fasthx/jinja.py
wrap_as(result_key, context_key=None)
cached
classmethod
Creates a JinjaContextFactory
that wraps the route's result and optionally the route
context under user-specified keys.
result_key
and context_key
must be different.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result_key
|
str
|
The key by which the |
required |
context_key
|
str | None
|
The key by which the |
None
|
Returns:
Type | Description |
---|---|
JinjaContextFactory
|
The created |
Raises:
Type | Description |
---|---|
ValueError
|
If |
Source code in fasthx/jinja.py
JinjaContextFactory
Bases: Protocol
Protocol definition for methods that convert a FastAPI route's result and route context
(i.e. the route's arguments) into a Jinja context (dict
).
Source code in fasthx/jinja.py
__call__(*, route_result, route_context)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
route_result
|
Any
|
The result of the route. |
required |
route_context
|
dict[str, Any]
|
Every keyword argument the route received. |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
The Jinja context dictionary. |
Raises:
Type | Description |
---|---|
ValueError
|
If converting the arguments to a Jinja context fails. |
Source code in fasthx/jinja.py
JinjaPath
Bases: str
String subclass that can be used to mark a template path as "absolute".
In this context "absolute" means the template path should be exempt from any prefixing behavior during template name resolution.
Note: calling any of the "mutation" methods (e.g. .lower()
) of an instance will
result in a plain str
object.