When building web applications with aiohttp, you'll often need to return HTML content to the client. aiohttp makes this easy by allowing you to directly return HTML strings or render templates.
Returning Raw HTML
You can directly return a string containing HTML from your route handler:
@aiohttp_app.get('/')
async def handle(request):
return web.Response(text='<h1>Hello World</h1>', content_type='text/html')
This will send the raw HTML string in the response body.
Rendering Templates
For most real applications, you'll want to use a template engine instead of writing raw HTML strings. aiohttp supports multiple Python template engines like Jinja2 out of the box.
Here's an example using Jinja2:
import jinja2
template = jinja2.Template("<h1>Hello {{name}}</h1>")
@aiohttp_app.get('/')
async def handle(request):
return web.Response(text=template.render(name='John'),
content_type='text/html')
This keeps your presentation logic separate from the route handlers.
Streaming HTML
For very large HTML responses, you may want to stream the output to avoid loading the entire string in memory.
You can do this by returning a
@aiohttp_app.get('/')
async def handle(request):
resp = web.StreamResponse()
resp.content_type = 'text/html'
await resp.prepare(request)
await resp.write(bytes('<h1>Hello</h1>', 'utf-8'))
return resp
Streaming the output chunk-by-chunk can improve memory usage for big pages.
In summary, aiohttp provides flexible options for returning HTML to clients, from raw strings to rendered templates to streaming output. Leveraging these can help build robust, production-ready web applications.