[파이썬] aiohttp 웹페이지 렌더링

In modern web development, asynchronous programming has become crucial for building high-performance web applications. One popular framework in the Python ecosystem for asynchronous web development is aiohttp. With aiohttp, you can easily create web servers and clients that handle multiple requests concurrently.

In this blog post, we will explore how to use aiohttp for webpage rendering, which involves fetching HTML templates, rendering them with data, and serving the dynamically generated HTML content to clients.

Installing aiohttp

To get started, you need to install aiohttp. You can do this using pip:

pip install aiohttp

Setting up a basic aiohttp server

Let’s begin by setting up a basic aiohttp server that can handle HTTP GET requests. Create a new Python file, for example, server.py, and start by importing the necessary modules:

import aiohttp
from aiohttp import web

Next, define a simple handler function that will be called when a request is received:

async def handle(request):
    return web.Response(text="Hello, World!")

Now, we can create the aiohttp application and add our handler as a GET route:

app = web.Application()
app.router.add_get('/', handle)

Finally, we need to run the server:

web.run_app(app)

Save server.py and execute it with Python:

python server.py

If everything goes well, you should see the following output:

======== Running on http://0.0.0.0:8080 ========

Open your browser and navigate to http://localhost:8080. You should see the text “Hello, World!” displayed on the page.

Rendering HTML templates with aiohttp_jinja2

For more complex webpage rendering, aiohttp integrates well with aiohttp_jinja2, a library that allows us to use Jinja2 templates with aiohttp.

To install aiohttp_jinja2, run the following command:

pip install aiohttp_jinja2

Next, we need to configure aiohttp_jinja2 in our application:

import aiohttp_jinja2
import jinja2

aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader('./templates'))

Make sure to create a templates directory in the same location as your server.py file. This is where our HTML templates will be stored.

Let’s create a simple HTML template called index.html:


<!DOCTYPE html>
<html>
<head>
    <title>aiohttp Webpage Rendering</title>
</head>
<body>
    <h1>{{ message }}</h1>
</body>
</html>

Now, modify our handler function to render the index.html template:

async def handle(request):
    context = {'message': 'Hello, World!'}
    response = aiohttp_jinja2.render_template('index.html', request, context)
    return response

To serve static files such as CSS or JavaScript, we can use aiohttp’s built-in functionality:

app.router.add_static('/static/', path='./static', name='static')

In this example, we assume that static files are stored in a static directory located in the same folder as server.py.

With everything in place, you can now run the server again and navigate to http://localhost:8080. The rendered HTML from the template should be displayed on the page.

Conclusion

In this blog post, we learned how to use aiohttp for webpage rendering in Python. We explored the basics of setting up a simple aiohttp server and then expanded it to render HTML templates using aiohttp_jinja2. This is just the beginning - aiohttp has many other powerful features for building async web applications, such as handling POST requests, working with websockets, and more.

Feel free to explore the aiohttp documentation to learn more about its capabilities and how to leverage them for your projects. Happy coding!