Hi,
We have been starting to porting existing Flask code to an asyncio based server (Quart).
Part of it makes use of the Jinja Native environment.
The render_async works very well, but not in conjunction with Native.
Here is replicable code in a python -m asyncio console:
from jinja2.nativetypes import NativeEnvironment
def render(s, my_vars):
native_environment = NativeEnvironment()
template = native_environment.from_string(s)
return template.render(**my_vars)
async def render_async(s, my_vars):
native_environment = NativeEnvironment(enable_async=True)
template = native_environment.from_string(s)
return await template.render_async(**my_vars)
global_vars = {}
number_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
global_vars["number_list"] = number_list
out = render("{{35}}", global_vars)
# int: 35
out = await render_async("{{35}}", global_vars)
# str: '35'
out = render("{{number_list}}", global_vars)
# list: [1, 2, 3, 4, 5, 6, 7, 8, 9]
out = await render_async("{{number_list}}", global_vars)
# Traceback (most recent call last):
# File "/Users/A492YC/.pyenv/versions/3.8.1/lib/python3.8/concurrent/futures/_base.py", line 439, in result
# return self.__get_result()
# File "/Users/A492YC/.pyenv/versions/3.8.1/lib/python3.8/concurrent/futures/_base.py", line 388, in __get_result
# raise self._exception
# File "<console>", line 1, in <module>
# File "<console>", line 4, in render_async
# File "/Users/A492YC/.virtualenvs/jinja_test-SmX6WV4i/lib/python3.8/site-packages/jinja2/asyncsupport.py", line 65, in render_async
# return self.environment.handle_exception()
# File "/Users/A492YC/.virtualenvs/jinja_test-SmX6WV4i/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
# reraise(*rewrite_traceback_stack(source=source))
# File "/Users/A492YC/.virtualenvs/jinja_test-SmX6WV4i/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
# raise value.with_traceback(tb)
# File "/Users/A492YC/.virtualenvs/jinja_test-SmX6WV4i/lib/python3.8/site-packages/jinja2/asyncsupport.py", line 26, in concat_async
# return concat(rv)
# TypeError: sequence item 0: expected str instance, list found
Expected:
In Native mode, instead of forcefully concatenating outputs as strings, outputs of the render_async() should be on par with the render()
Environment:
- Python version: 3.8.1
- Jinja version: 2.11.3
Hi,
We have been starting to porting existing Flask code to an asyncio based server (Quart).
Part of it makes use of the Jinja Native environment.
The render_async works very well, but not in conjunction with Native.
Here is replicable code in a
python -m asyncioconsole:Expected:
In Native mode, instead of forcefully concatenating outputs as strings, outputs of the
render_async()should be on par with therender()Environment: