-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
direct return of Element does not work in FastAPI route handler with default_response_class set #54
Comments
Hello, I don't know much about FastAPI being mostly a Flask user, but casting your htpy elemnt to str() is the way to do it. |
Thank you for your input. I tried rewriting a custom response as default, until i found out that before fastapi evaluates the render function on the response class it has an internal encoding mechanism that executes before the render function of the response class, when used as default. Error shows up before ever hitting the response class. |
So I guess it is not possible to just return htpy elements to fastapi? @blast25 can you point to the fastapi source code where it does the internal encoding? Maybe it is possible to add some method to the htpy elements to have it recognized directly by fastapi. If not, we could document the need to call Related to this is PR #38 that would allow using Starlettes StreamingResponse directly. I think that would be the preferred way of using Starlette/FastAPI with htpy. Combined with an async ORM, that will make the entire stack fully async and improve the speed of the page loads for the end user. I think it make Here are the docs for how to use Starlette + async streaming response:
|
https://github.com/fastapi/fastapi/blob/master/fastapi/routing.py |
Thanks for the pointer! Reading the FastAPI docs, it says that when returning "any data", it will be passed to So that is just the way FastAPI works by design. There does not seem to be any way around that. I do not think this contradicts what the docs says about Starlette: You can still Do you have any suggestions for any changes to htpy or the htpy docs in this regards? |
After revising the problem not really, may have been nice to not call str on every object but thats a luxury problem i suppose. Thanks for considering. |
Closing it for now then, if anyone comes up with something else, feel free to open a new issue! |
https://htpy.dev/starlette/
Usage with FastAPI:
Calling localhost:9001/test/ returns error
Does not work for me as the internal FastApi Encoding does not know what to do with it (expects pydantic model). Using HTMLResponse as return works. But using a response from default response class fails for me, this would be necassary as to not delete changes in middleware or dependencies for routes that manipulate eg. the response header. Initializing HTMLReponse yourself would entail setting everything again from previous functions, every time.
Workaround is using
return str(div["test"])
.The text was updated successfully, but these errors were encountered: