FastAPI + SQLModel + fasthtml #215
Replies: 7 comments 3 replies
-
As someone only vaguely familiar with those but who has read much of the FastHTML docs, here's the little I have to offer:
|
Beta Was this translation helpful? Give feedback.
-
Using all the fasttags part from FastHTML in your FastAPI app is super easy. The rough steps, other than installing both the libraries, would be something like -
Ideally, you will make a main layout that will have head, body, footer etc and add the required css and script there. The core beauty, as far as my little understanding tells me, of FastHTML is that you can make any component you like. Everything that is a html tag can be a component. All the components part should work fine I believe as components are rendered as html, that's it. Also, htmx part should be good as that is the idea of htmx to be just like html. from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fasthtml import common as fh
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/", response_class=HTMLResponse)
def home():
return fh.to_xml(fh.Div(fh.H1("Hello World!"))) I am still exploring this project to see what all is there :-) |
Beta Was this translation helpful? Give feedback.
-
I have similar thoughts - we have some apps that feel like a very good fit for FastHTML but needs both HTML and JSON endpoints (we're providing the data to both people and machines). There's some hacky solutions (build the logic universal and include it into both FastAPI and FastHTML services, for example) but if there we non-hacky modular one, it would be great. |
Beta Was this translation helpful? Give feedback.
-
Couldn't I have a FastAPI application acting as a backend and the FastHTML route makes the call to the backend api route and builds and returns the HTML on its route? I thought that was the intended use with these libs, but reading the docs i couldn't find any easy way to call endpoints inside a FastHTML route. Am i overlooking something very obvious? |
Beta Was this translation helpful? Give feedback.
-
FastHTML has response type for json. If the return value is a string then it does html response but if the return value is of a dictionary then it will be a JSON response. |
Beta Was this translation helpful? Give feedback.
-
I think to use this solution for my new project:
|
Beta Was this translation helpful? Give feedback.
-
I've used a similar approach from @psabhay with an existing API on FastApi + SQLModels, and I'm happy to report that it's been really successful! I added a new endpoint for the index page of a single-page application on my server. For the other endpoints, I've added a condition so that the same endpoint can respond with either JSON or HTMX components for my SPA. @router.get(..., response_model=MyResponseModel):
def router_process(...):
...
my_resp_model_obj = MyResponseModel(...)
if hx_request:= request.headers.get('HX-Request', None):
return HTMLResponse(my_resp_model_obj.to_html())
return my_resp_model_obj I've implemented the to_html() function of MyResponseModel using the fasttags part from FastHTML. This has given me a flexible API that responds to both JSON and HTMX components, and the system is working like a charm! I know that not using FastHTML server meant I lost some useful features on the frontend provided by FastHTML, like session management and a login system. I probably will need them in the future, though. But for now, it's all good! I'd love to hear any tips or advice you all might have! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I started a project with FastAPI with SQLModel and I wanted to use HTMX. So now there's fasthtml, that's awesome! 🎉
But now I want to use this awesomeness in FastAPI with SQLModel, but does that even make sense? Has anyone already thought about that? 😀
Beta Was this translation helpful? Give feedback.
All reactions