Thoughts on routing in Python Web Frameworks
A friend and I were discussing Flask as he’s quite taken with its simplicity. I’m more on the fence but, our discussion helped me realize how much of my thinking is now Django oriented. A little concerned that I might be seeing everything as a django nail for my django hammer, I started thinking about what does a web framework need?
Going back to first principles, the core things of a web framework is to easily and obviously route requests to my code. Everything else, all the “batteries included”, all the community projects, are extra. Here’s how three popular handle the routing problem:
@app.route("/")
def hello():
return "Hello World!"
- Pyramid uses a two step approach, first defining the route.
config.add_route(‘helloworld’, ‘helloworld’)
and then pairing the view and route:
config.add_view(hello, route_name=‘helloworld’)
Though a decorator syntax is also available for the add_view call.
patterns(‘’,
url(r’^helloworld$’, hello, name=“helloworld”),
)
Looking at these three approaches… I still think Django’s is the most obvious. With all the things that a framework actually has to do (help with sessions, input validation, data persistence, authorization, etc.) routing is pretty basic part so it definitely shouldn’t be the only thing one looks for in a framework and choosing Django just for the routing is probably a poor idea… Still I like Django best.