fasthx 2.2.0__tar.gz → 2.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: fasthx
3
- Version: 2.2.0
3
+ Version: 2.3.0
4
4
  Summary: FastAPI server-side rendering with built-in HTMX support.
5
5
  License: MIT
6
6
  Author: Peter Volf
@@ -32,7 +32,7 @@ Description-Content-Type: text/markdown
32
32
 
33
33
  FastAPI server-side rendering with built-in HTMX support.
34
34
 
35
- Key features:
35
+ ## Key features
36
36
 
37
37
  - **Decorator syntax** that works with FastAPI as one would expect, no need for unused or magic dependencies in routes.
38
38
  - Built for **HTMX**, but can be used without it.
@@ -237,7 +237,7 @@ We welcome contributions from the community to help improve the project! Whether
237
237
  - **Discuss**: Join our [Discussion Board](https://github.com/volfpeter/fasthx/discussions) to ask questions, share ideas, provide feedback, and engage with the community.
238
238
  - **Document**: Help improve the documentation by fixing typos, adding examples, and updating guides to make it easier for others to use the project.
239
239
  - **Develop**: Prototype requested features or pick up issues from the issue tracker.
240
- - **Share**: Share you own project by adding it to the external examples list, helping others discover and benefit from your work.
240
+ - **Share**: Share your own project by adding it to the external examples list, helping others discover and benefit from your work.
241
241
  - **Test**: Write tests to improve coverage and enhance reliability.
242
242
 
243
243
  ## License - MIT
@@ -11,7 +11,7 @@
11
11
 
12
12
  FastAPI server-side rendering with built-in HTMX support.
13
13
 
14
- Key features:
14
+ ## Key features
15
15
 
16
16
  - **Decorator syntax** that works with FastAPI as one would expect, no need for unused or magic dependencies in routes.
17
17
  - Built for **HTMX**, but can be used without it.
@@ -216,7 +216,7 @@ We welcome contributions from the community to help improve the project! Whether
216
216
  - **Discuss**: Join our [Discussion Board](https://github.com/volfpeter/fasthx/discussions) to ask questions, share ideas, provide feedback, and engage with the community.
217
217
  - **Document**: Help improve the documentation by fixing typos, adding examples, and updating guides to make it easier for others to use the project.
218
218
  - **Develop**: Prototype requested features or pick up issues from the issue tracker.
219
- - **Share**: Share you own project by adding it to the external examples list, helping others discover and benefit from your work.
219
+ - **Share**: Share your own project by adding it to the external examples list, helping others discover and benefit from your work.
220
220
  - **Test**: Write tests to improve coverage and enhance reliability.
221
221
 
222
222
  ## License - MIT
@@ -0,0 +1,47 @@
1
+ from collections.abc import Mapping
2
+ from typing import TYPE_CHECKING, Annotated, Any, TypeAlias
3
+
4
+ from fastapi import Depends, Header
5
+ from fastapi import Request as FARequest
6
+
7
+ if TYPE_CHECKING:
8
+ Request: TypeAlias = FARequest
9
+ else:
10
+ Request: TypeAlias = Mapping[str, Any]
11
+ """
12
+ Alias for `Request` arguments.
13
+
14
+ Workaround for this FastAPI bug: https://github.com/fastapi/fastapi/discussions/12403.
15
+ And here's a FastAPI bugfix: https://github.com/fastapi/fastapi/pull/12406.
16
+
17
+ This workaround should be removed when FastAPI had several new releases with the fix.
18
+ """
19
+
20
+
21
+ def get_hx_request(
22
+ request: FARequest, hx_request: Annotated[str | None, Header()] = None
23
+ ) -> Request | None:
24
+ """
25
+ FastAPI dependency that returns the current request if it is an HTMX one,
26
+ i.e. it contains an `"HX-Request: true"` header.
27
+ """
28
+ return request if hx_request == "true" else None
29
+
30
+
31
+ def get_page_request(request: FARequest) -> Request:
32
+ """
33
+ Replacement dependency for `Request` to work around this FastAPI bug:
34
+ https://github.com/fastapi/fastapi/discussions/12403.
35
+ """
36
+ return request
37
+
38
+
39
+ DependsHXRequest = Annotated[Request | None, Depends(get_hx_request)]
40
+ """Annotated type (dependency) for `get_hx_request()` for FastAPI."""
41
+
42
+ DependsPageRequest = Annotated[Request, Depends(get_page_request)]
43
+ """
44
+ Annotated `Request` dependency alias.
45
+
46
+ Workaround for this FastAPI bug: https://github.com/fastapi/fastapi/discussions/12403
47
+ """
@@ -177,8 +177,30 @@ class HTMY:
177
177
  else self._make_error_render_function(error_component_selector),
178
178
  )
179
179
 
180
+ async def render_component(self, component: h.Component, request: Request) -> str:
181
+ """
182
+ Renders the given component.
183
+
184
+ This method is useful for rendering components directly, outside of the context of a route
185
+ (meaning no access to route parameters), for example in exception handlers.
186
+
187
+ The method adds all the usual data to the `htmy` rendering context, including the result of
188
+ all request processors. There is no access to route parameters though, so while `RouteParams`
189
+ will be in the context, it will be empty.
190
+
191
+ Arguments:
192
+ component: The component to render.
193
+ request: The current request.
194
+
195
+ Returns:
196
+ The rendered component.
197
+ """
198
+ return await self.htmy.render(component, self._make_render_context(request, {}))
199
+
180
200
  def _make_render_function(self, component_selector: HTMYComponentSelector[T]) -> HTMLRenderer[T]:
181
- """Creates a render function that uses the given component selector."""
201
+ """
202
+ Creates a render function that uses the given component selector.
203
+ """
182
204
 
183
205
  async def render(result: T, *, context: dict[str, Any], request: Request) -> str:
184
206
  component = (
@@ -193,7 +215,9 @@ class HTMY:
193
215
  def _make_error_render_function(
194
216
  self, component_selector: HTMYComponentSelector[Exception]
195
217
  ) -> HTMLRenderer[Exception]:
196
- """Creates an error renderer function that uses the given component selector."""
218
+ """
219
+ Creates an error renderer function that uses the given component selector.
220
+ """
197
221
 
198
222
  async def render(result: Exception, *, context: dict[str, Any], request: Request) -> str:
199
223
  component = (
@@ -206,7 +230,16 @@ class HTMY:
206
230
  return render
207
231
 
208
232
  def _make_render_context(self, request: Request, route_params: dict[str, Any]) -> h.Context:
209
- """Creates the HTMY rendering context."""
233
+ """
234
+ Creates the `htmy` rendering context for the given request and route parameters.
235
+
236
+ Arguments:
237
+ request: The current request.
238
+ route_params: The route parameters.
239
+
240
+ Returns:
241
+ The `htmy` rendering context.
242
+ """
210
243
  # Add the current request to the context.
211
244
  result = CurrentRequest.to_context(request)
212
245
 
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "fasthx"
3
- version = "2.2.0"
3
+ version = "2.3.0"
4
4
  description = "FastAPI server-side rendering with built-in HTMX support."
5
5
  authors = ["Peter Volf <do.volfp@gmail.com>"]
6
6
  readme = "README.md"
@@ -1,47 +0,0 @@
1
- from collections.abc import Mapping
2
- from typing import TYPE_CHECKING, Annotated, Any, TypeAlias
3
-
4
- from fastapi import Depends, Header, Request
5
-
6
- RequestAlias: TypeAlias = Mapping[str, Any]
7
- """
8
- Alias for `Request` arguments.
9
-
10
- Workaround for this FastAPI bug: https://github.com/fastapi/fastapi/discussions/12403.
11
- And here's a FastAPI bugfix: https://github.com/fastapi/fastapi/pull/12406.
12
-
13
- This workaround should be removed when FastAPI had several new releases with the fix.
14
- """
15
-
16
-
17
- def get_hx_request(
18
- request: Request, hx_request: Annotated[str | None, Header()] = None
19
- ) -> RequestAlias | None:
20
- """
21
- FastAPI dependency that returns the current request if it is an HTMX one,
22
- i.e. it contains an `"HX-Request: true"` header.
23
- """
24
- return request if hx_request == "true" else None
25
-
26
-
27
- def get_page_request(request: Request) -> RequestAlias:
28
- """
29
- Replacement dependency for `Request` to work around this FastAPI bug:
30
- https://github.com/fastapi/fastapi/discussions/12403.
31
- """
32
- return request
33
-
34
-
35
- if TYPE_CHECKING:
36
- DependsHXRequest: TypeAlias = Request | None
37
- DependsPageRequest: TypeAlias = Request
38
- else:
39
- DependsHXRequest = Annotated[RequestAlias | Request | None, Depends(get_hx_request)]
40
- """Annotated type (dependency) for `get_hx_request()` for FastAPI."""
41
-
42
- DependsPageRequest = Annotated[RequestAlias | Request, Depends(get_page_request)]
43
- """
44
- Annotated `Request` dependency alias.
45
-
46
- Workaround for this FastAPI bug: https://github.com/fastapi/fastapi/discussions/12403
47
- """
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes