Jinja
fasthx.Jinja
dataclass
Jinja2 renderer utility with FastAPI route decorators.
Source code in fasthx/jinja.py
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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
|
make_context: JinjaContextFactory = 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: bool = 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: Jinja2Templates
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
fasthx.JinjaContext
Core JinjaContextFactory
implementations.
Source code in fasthx/jinja.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 |
|
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": route_context}
, 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. 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 whih the |
None
|
Returns:
Type | Description |
---|---|
JinjaContextFactory
|
The created |
Raises:
Type | Description |
---|---|
ValueError
|
If |
Source code in fasthx/jinja.py
fasthx.TemplateHeader
dataclass
Template selector that takes the Jinja template name from a request header.
This class makes it possible for the client to submit the key/ID of the required template
to the server in a header. The Jinja decorators will then look up and render the requested
template if it exists. If the client doesn't request a specific template, then default
will be used if it was set, otherwise an exception will be raised.
By default this class treats template keys as case-insensitive. If you'd like to disable
this behavior, set case_sensitive
to True
.
This class can also handle route errors if the error
property is set.
Implements
RequestComponentSelector[str]
.
Source code in fasthx/jinja.py
case_sensitive: bool = field(default=False, kw_only=True)
class-attribute
instance-attribute
Whether the keys of templates
are case-sensitive or not (default is False
).
default: str | None = field(default=None, kw_only=True)
class-attribute
instance-attribute
The template to use when the client didn't request a specific one.
error: type[Exception] | tuple[type[Exception], ...] | None = field(default=None, kw_only=True)
class-attribute
instance-attribute
The accepted error or errors.
header: str
instance-attribute
The header which is used by the client to communicate the key of the requested template.
templates: dict[str, str]
instance-attribute
Dictionary that maps template keys to template (file) names.
get_component(request, error)
Returns the name of the template that was requested by the client.
If the request doesn't contain a header (with the name self.header
),
then self.default
will be returned if it's not None
.
Raises:
Type | Description |
---|---|
KeyError
|
If the client requested a specific template but it's unknown, or if no template was requested and there's no default either. |
Source code in fasthx/jinja.py
fasthx.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.
Source code in fasthx/jinja.py
fasthx.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/typing.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. |