mikeckennedy / fastapi-chameleon

Adds integration of the Chameleon template language to FastAPI. #pypackage
MIT License
140 stars 38 forks source link

render template inside view method #25

Open possnfiffer opened 2 years ago

possnfiffer commented 2 years ago

I'm trying to build a video collector similar to the one from your HTMX course. Mine uses FastAPI, Chameleon, Beanie. I'm having trouble rendering the search results due to the conditional partial render inside of the view method. I don't know how to render a template when it's inside the view method like that, only done it through the decorator on the view method function itself.

How would I implement something like this in fastapi-chameleon?

html = flask.render_template('videos/partials/search_results.html', videos=vm.videos)
return flask.make_response(html)

I have tried the following with from fastapi_chameleon import template imported

html = template(template_file="videos/partials/search_results.pt", videos=vm.videos)
return fastapi.responses.HTMLResponse(html)

I also tried getting creative and reading through your fastapi-chameleon source, so I tried

page: PageTemplate = "/usr/home/roller/Projects/coach-aaron/templates/videos/partials/search_results.pt"
return page.render(encoding='utf-8', videos=vm.videos)

That didn't work either.

At the moment I'm going to try following the FastAPI docs If I have to for now I'll do just this one partial using Jinja if I can get it to do that.. we'll see. I appreciate any help or suggestions you may have on this. I'll definitely submit this webapp I'm working on to the Student Creation Showcase once it's ready enough and is actually doing interesting things.

possnfiffer commented 2 years ago

Actually trying render_partial first to see if that solves it

possnfiffer commented 2 years ago

Ok, so I have

html = chameleon_partials.render_partial("videos/partials/search_results.pt", videos={"videos": vm.videos}, search_text=vm.search_text)

working to render the HTML but now I have to figure out how to properly return that and have it load in my #search-results div

possnfiffer commented 2 years ago

fastapi_chameleon.exceptions.FastAPIChameleonException: Invalid return type <class 'chameleon_partials.HTML'>, we expected a dict or fastapi.Response as the return value. That's what I get if I return the HTML object.

I'm working on figuring out what needs to be returned. I have tried something different variations of the following like:

model = json.dumps(html.html_text)

return fastapi.responses.HTMLResponse(status_code=status.HTTP_200_OK, content=model)

I'm getting somewhere though, that's good.

I guess this whole discussion should really fit in the new GitHub Discussions section rather than Issues

possnfiffer commented 2 years ago
html = chameleon_partials.render_partial("videos/partials/search_results.pt", videos=vm.videos, search_text=vm.search_text)

return fastapi.responses.HTMLResponse(status_code=status.HTTP_200_OK, content=html.html_text.encode('utf-8'))

This is what I have that's working. PyCharm is complaining about vm.videos. It says "Expected type 'dict', got 'list[Video] | None' instead"

possnfiffer commented 2 years ago

After all this, now that I think about it, this is essentially a feature request. If there were something like from fastapi.templating import Jinja2Templates that works similar to the Jinja one but for chameleon that would be cool then there could be a blurb about it up on the FastAPI Docs page