ソースコード:https://github.com/volfpeter/fasthx
ドキュメントと例:https://volfpeter.github.io/fasthx
内蔵のHTMXサポートを備えたFastAPIサーバー側のレンダリング。
主な機能:
htmy 、 jinja2などdominateで動作します。パッケージはPYPIで利用でき、以下でインストールできます。
$ pip install fasthxパッケージには、次の公式統合のオプションの依存関係があります。
pip install fasthx[htmy] 。pip install fasthx[jinja] 。 FastHXの基本的な使用を紹介する完全な、しかし簡単な例については、例フォルダーを参照してください。
要求: pip install fasthx[htmy] 。
HTMLとHTMXリクエストをHTMYで提供するのは、 fasthx.htmy.HTMYインスタンスを作成し、ルートでhx()およびpage()デコレーターメソッドを使用するのと同じくらい簡単です。
以下の例は、 IndexPageとUserList htmyコンポーネントの存在を想定しています。 htmyコンポーネントを使用した完全な作業例は、こちらをご覧ください。
from datetime import date
from fastapi import FastAPI
from pydantic import BaseModel
from fasthx . htmy import HTMY
# Pydantic model for the application
class User ( BaseModel ):
name : str
birthday : date
# Create the FastAPI application.
app = FastAPI ()
# Create the FastHX HTMY instance that renders all route results.
htmy = HTMY ()
@ app . get ( "/users" )
@ htmy . hx ( UserList ) # Render the result using the UserList component.
def get_users ( rerenders : int = 0 ) -> list [ User ]:
return [
User ( name = "John" , birthday = date ( 1940 , 10 , 9 )),
User ( name = "Paul" , birthday = date ( 1942 , 6 , 18 )),
User ( name = "George" , birthday = date ( 1943 , 2 , 25 )),
User ( name = "Ringo" , birthday = date ( 1940 , 7 , 7 )),
]
@ app . get ( "/" )
@ htmy . page ( IndexPage ) # Render the index page.
def index () -> None : ...要求: pip install fasthx[jinja] 。
HTMLとHTMXのリクエストの提供を開始するには、 fasthx.Jinjaのインスタンスを作成し、ルート上のデコレーターとしてhx()またはpage()メソッドを使用するだけです。 hx() HTMX要求のHTMLレンダリングのみをトリガーし、 page()は無条件にHTMLをレンダリングします。以下の例を参照してください。
from fastapi import FastAPI
from fastapi . templating import Jinja2Templates
from fasthx import Jinja
from pydantic import BaseModel
# Pydantic model of the data the example API is using.
class User ( BaseModel ):
first_name : str
last_name : str
# Create the app.
app = FastAPI ()
# Create a FastAPI Jinja2Templates instance and use it to create a
# FastHX Jinja instance that will serve as your decorator.
jinja = Jinja ( Jinja2Templates ( "templates" ))
@ app . get ( "/" )
@ jinja . page ( "index.html" )
def index () -> None :
...
@ app . get ( "/user-list" )
@ jinja . hx ( "user-list.html" )
async def htmx_or_data () -> list [ User ]:
return [
User ( first_name = "John" , last_name = "Lennon" ),
User ( first_name = "Paul" , last_name = "McCartney" ),
User ( first_name = "George" , last_name = "Harrison" ),
User ( first_name = "Ringo" , last_name = "Starr" ),
]
@ app . get ( "/admin-list" )
@ jinja . hx ( "user-list.html" , no_data = True )
def htmx_only () -> list [ User ]:
return [ User ( first_name = "Billy" , last_name = "Shears" )]ここで完全な作業例をご覧ください。
必要: pip install fasthx 。
FASTHX統合なしでレンダリングエンジンを使用したい場合は、必要なすべての機能を提供するhx()およびpage()デコレータに簡単に構築できます。必要なのは、 HTMLRendererプロトコルを実装することです。
Jinjaのケースと同様に、 hx() HTMX要求のHTMLレンダリングのみをトリガーし、 page()は無条件にHTMLをレンダリングします。以下の例を参照してください。
from typing import Annotated , Any
from fastapi import Depends , FastAPI , Request
from fasthx import hx , page
# Create the app.
app = FastAPI ()
# Create a dependecy to see that its return value is available in the render function.
def get_random_number () -> int :
return 4 # Chosen by fair dice roll.
DependsRandomNumber = Annotated [ int , Depends ( get_random_number )]
# Create the render methods: they must always have these three arguments.
# If you're using static type checkers, the type hint of `result` must match
# the return type annotation of the route on which this render method is used.
def render_index ( result : list [ dict [ str , str ]], * , context : dict [ str , Any ], request : Request ) -> str :
return "<h1>Hello FastHX</h1>"
def render_user_list ( result : list [ dict [ str , str ]], * , context : dict [ str , Any ], request : Request ) -> str :
# The value of the `DependsRandomNumber` dependency is accessible with the same name as in the route.
random_number = context [ "random_number" ]
lucky_number = f"<h1> { random_number } </h1>"
users = "" . join (( "<ul>" , * ( f"<li> { u [ 'name' ] } </li>" for u in result ), "</ul>" ))
return f" { lucky_number } n { users } "
@ app . get ( "/" )
@ page ( render_index )
def index () -> None :
...
@ app . get ( "/htmx-or-data" )
@ hx ( render_user_list )
def htmx_or_data ( random_number : DependsRandomNumber ) -> list [ dict [ str , str ]]:
return [{ "name" : "Joe" }]
@ app . get ( "/htmx-only" )
@ hx ( render_user_list , no_data = True )
async def htmx_only ( random_number : DependsRandomNumber ) -> list [ dict [ str , str ]]:
return [{ "name" : "Joe" }]ここで完全な作業例をご覧ください。
Jinja2例。 このパッケージの唯一の依存関係はfastapiです。
糸くずとフォーマットにはruffを使用し、静的コード分析にはmypy 、およびテストにはpytest使用します。
このドキュメントはmkdocs-materialおよびmkdocstringsで構築されています。
お気軽に質問したり、新機能をリクエストしたりしてください。
そしてもちろん、より多くのドキュメント、例、コード、テストなど、すべての貢献は歓迎されます。
目標は、 fasthx 、最も複雑なHTMXユースケースでさえ簡単に実装できるようにするためのバランスの取れたプロジェクトにすることです。
このパッケージは、MITライセンスの条件の下でオープンソーリングされています。
プロジェクトをサポートしてくれたSmart-Nowに感謝します。
![]()