Skip to main content

Code organization and structuring

Understanding Code Organization and Structuring in Tornado

When it comes to writing Tornado applications, organizing your code in a structure that is both efficient and easy to understand is of utmost importance. This guide will walk you through some of the best practices for code organization and structuring in Tornado.

Why is Code Organization Important?

Good code organization makes your application easier to understand, test, and maintain. It also helps in identifying and fixing bugs, and facilitates collaboration between team members.

Basic Project Structure

A typical Tornado project might have the following structure:

/myproject
/myproject
__init__.py
/handlers
__init__.py
home.py
user.py
/templates
home.html
user.html
/static
style.css
app.py
setup.py

The outer myproject/ directory is the root of your project. The inner myproject/ directory is the actual Python package.

handlers/ contains the request handlers, templates/ contains the HTML templates, and static/ contains static files like CSS and JavaScript.

app.py is the application module and is responsible for creating, setting up, and starting the Tornado application.

Handlers

In Tornado, handlers are Python classes that process HTTP requests. Each handler corresponds to a URL pattern that is defined in your application's routing.

A good practice is to have one handler per HTTP verb. This makes your handlers small, easy to test, and easy to maintain.

# user.py
from tornado.web import RequestHandler

class GetUserHandler(RequestHandler):
# Handler for GET requests
def get(self, user_id):
# handler code here

class PostUserHandler(RequestHandler):
# Handler for POST requests
def post(self):
# handler code here

Templates

Templates are HTML files that contain placeholders for dynamic content. The Tornado template engine replaces these placeholders with actual data when a template is rendered.

Keep your templates as simple as possible. Avoid putting complex logic in your templates. If you find yourself needing to do this, consider moving that logic to your handlers instead.

<!-- user.html -->
<h1>{{ user.name }}</h1>
<p>{{ user.email }}</p>

Static Files

Static files are files that are served to the client as-is. They are typically CSS and JavaScript files, but can also be images or other types of files.

Keep your static files organized in directories based on their file type. This makes it easier to find the file you're looking for.

/static
/css
style.css
/js
app.js

App Module

Your app module (app.py) is responsible for setting up and starting your Tornado application. It should define your application's routing and any application-wide settings.

# app.py
from tornado.web import Application
from .handlers import GetUserHandler, PostUserHandler

def make_app():
return Application([
(r"/user/(.*)", GetUserHandler),
(r"/user", PostUserHandler),
])

if __name__ == "__main__":
app = make_app()
app.listen(8888)
IOLoop.current().start()

In this tutorial, we've covered the basics of code organization and structuring in Tornado. Remember, good code organization is a habit that takes time to develop. Keep practicing and your code will become easier to write, understand, and maintain.