Skip to content
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

Support async rendering #333

Closed
Dreamsorcerer opened this issue Apr 12, 2020 · 4 comments · Fixed by #393
Closed

Support async rendering #333

Dreamsorcerer opened this issue Apr 12, 2020 · 4 comments · Fixed by #393

Comments

@Dreamsorcerer
Copy link
Member

template() and render_template() should be async functions, or for backwards compatibility new async versions of the functions should be added.

They should call jinja2.Template.render_async(), which also requires enable_async in the environment setup.

See:
https://jinja.palletsprojects.com/en/2.11.x/api/#jinja2.Environment
https://jinja.palletsprojects.com/en/2.11.x/api/#jinja2.Template.render_async
https://jinja.palletsprojects.com/en/2.11.x/api/#async-support

@Dreamsorcerer
Copy link
Member Author

Currently, it looks like the only way to use the async functionality is like this:

template = aiohttp_jinja2.get_env(request.app).get_template("index.jinja")
return web.Response(text=await template.render_async(), content_type="text/html")

@Dreamsorcerer
Copy link
Member Author

Given that passing enable_async breaks the existing functions, maybe a good way to handle backwards compatibility is just to switch the functions when enable_async is passed by the user.

e.g.

if kwargs.get("enable_async", False):
    aiohttp_jinja2.template = aiohttp_jinja2._template_async
    aiohttp_jinja2.render_template = aiohttp_jinja2._render_template_async

Or, something like that. Then all the user has to do is add enable_async=True to the setup() call.

@Dreamsorcerer
Copy link
Member Author

@asvetlov I'm happy to work on this PR. Any thoughts on the correct way to implement it? Does my last comment seem reasonable?

@asvetlov
Copy link
Member

asvetlov commented Nov 7, 2020

I think we need separate functions for async mode.

If you are willing to work on it -- please go ahead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants