Comments (11)
by the way. Thanks for this module. It's much more elegant than the fastapi-htmx I tried earlier.
from fasthx.
Version 0.2403.0
has been deployed to PyPI with the requested feature. As mentioned in the release notes, this is probably the last release before the project goes sem-ver.
from fasthx.
Okay, I added the missing Response
support to Jinja
by internally converting the template response back into a string. This way the core hx()
and page()
decorators do the response header update as expected. There are minor drawbacks for this solution, but nothing notable I think.
If a more complex solution becomes necessary, the right way to implement it would be probably to introduce a custom response class, which would signal to hx
that it should execute the same logic on the response as for strings.
from fasthx.
Hi,
Thanks for reporting the issue in such detail!
Yes, the header you set on response
is discarded because hx()
and page()
internally creates an HTMLResponse
and it doesn't copy the headers you set inside the route.
Maybe this missing feature could be implemented simply by adding a __response: Response
parameter to the wrapper method within hx()
and page()
. Copying headers and session data could be helpful for other scenarios as well. I'll need to check whether the Response
object supports this. If it doesn't work, then maybe a special dependency class could be added to lib: all response attributes could be set on that object, and the lib would take care of applying those changes to the created response. I'll think about this.
If you'd like to give the implementation a go, I'd be happy to get a PR 🙂 Otherwise I'll try to get it done when I have a bit of time.
Peter
from fasthx.
I couldn't resist. The feature itself is ready. I still need to add tests and docs.
If having this feature is very urgent, I can publish a new release without these.
from fasthx.
Haha. Thank you. I was looking at your code just now too :)
I can just checkout the branch you committed to for the time being.
best regards,
Oliver.
from fasthx.
Thanks 🙂
from fasthx.
See the linked PR. I'd appreciate a comment there, just to make sure the feature fixes your issue.
from fasthx.
Not sure you get notified for comments in a closed PR:
In my case at least rendered
is already an instance of Response, hence the headers would have to be merged.
This works for me:
if isinstance(rendered, str):
return HTMLResponse(
rendered, headers=None if response is None else response.headers
)
else:
rendered.headers.update({} if response is None else response.headers)
return rendered
But maybe I am not seeing the bigger picture.
I am not too sure for which cases isinstance(rendered, str) is checked?
Calling code:
# Get all
@router.get("/")
@jinja.hx("portfolios/portfolios.jinja2")
async def get_portfolios(response: Response, db: Session = Depends(database.get_db)) -> list[dict]:
response.headers["HX-Trigger"] = "newPortfolio"
records = db.query(Portfolio).all()
return records
from fasthx.
Ah, right. I tested with the custom templating version, which worked, and I stupidly assumed the Jinja-version will also work by default, but it doesn't.
from fasthx.
Version 0.2403.1 is out on PyPI with the fix.
from fasthx.
Related Issues (10)
- Add more examples HOT 4
- Pass endpoint dependencies to Jinja context HOT 7
- Swagger compliant `HX-Request` headers. HOT 6
- Unable to access Response in the Route HOT 10
- Allow non-dict return values by default for Jinja template rendering HOT 1
- Run sync routes in a threadpool HOT 1
- Add a page() method to Jinja which renders the template even for non-HTMX requests HOT 1
- Template per Response Code HOT 2
- [Jinja] Let routes choose from multiple templates
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fasthx.