Code Monkey home page Code Monkey logo

Comments (11)

cubinet-code avatar cubinet-code commented on July 24, 2024 1

by the way. Thanks for this module. It's much more elegant than the fastapi-htmx I tried earlier.

from fasthx.

volfpeter avatar volfpeter commented on July 24, 2024 1

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.

volfpeter avatar volfpeter commented on July 24, 2024 1

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.

volfpeter avatar volfpeter commented on July 24, 2024

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.

volfpeter avatar volfpeter commented on July 24, 2024

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.

cubinet-code avatar cubinet-code commented on July 24, 2024

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.

volfpeter avatar volfpeter commented on July 24, 2024

Thanks 🙂

from fasthx.

volfpeter avatar volfpeter commented on July 24, 2024

See the linked PR. I'd appreciate a comment there, just to make sure the feature fixes your issue.

from fasthx.

cubinet-code avatar cubinet-code commented on July 24, 2024

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.

volfpeter avatar volfpeter commented on July 24, 2024

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.

volfpeter avatar volfpeter commented on July 24, 2024

Version 0.2403.1 is out on PyPI with the fix.

from fasthx.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.