FastAPI + SQLModel + fasthtml #215
Replies: 6 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 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