1.. _design:23Design Decisions in Flask4=========================56If you are curious why Flask does certain things the way it does and not7differently, this section is for you. This should give you an idea about8some of the design decisions that may appear arbitrary and surprising at9first, especially in direct comparison with other frameworks.101112The Explicit Application Object13-------------------------------1415A Python web application based on WSGI has to have one central callable16object that implements the actual application. In Flask this is an17instance of the :class:`~flask.Flask` class. Each Flask application has18to create an instance of this class itself and pass it the name of the19module, but why can't Flask do that itself?2021Without such an explicit application object the following code::2223 from flask import Flask24 app = Flask(__name__)2526 @app.route('/')27 def index():28 return 'Hello World!'2930Would look like this instead::3132 from hypothetical_flask import route3334 @route('/')35 def index():36 return 'Hello World!'3738There are three major reasons for this. The most important one is that39implicit application objects require that there may only be one instance at40the time. There are ways to fake multiple applications with a single41application object, like maintaining a stack of applications, but this42causes some problems I won't outline here in detail. Now the question is:43when does a microframework need more than one application at the same44time? A good example for this is unittesting. When you want to test45something it can be very helpful to create a minimal application to test46specific behavior. When the application object is deleted everything it47allocated will be freed again.4849Another thing that becomes possible when you have an explicit object lying50around in your code is that you can subclass the base class51(:class:`~flask.Flask`) to alter specific behavior. This would not be52possible without hacks if the object were created ahead of time for you53based on a class that is not exposed to you.5455But there is another very important reason why Flask depends on an56explicit instantiation of that class: the package name. Whenever you57create a Flask instance you usually pass it `__name__` as package name.58Flask depends on that information to properly load resources relative59to your module. With Python's outstanding support for reflection it can60then access the package to figure out where the templates and static files61are stored (see :meth:`~flask.Flask.open_resource`). Now obviously there62are frameworks around that do not need any configuration and will still be63able to load templates relative to your application module. But they have64to use the current working directory for that, which is a very unreliable65way to determine where the application is. The current working directory66is process-wide and if you are running multiple applications in one67process (which could happen in a webserver without you knowing) the paths68will be off. Worse: many webservers do not set the working directory to69the directory of your application but to the document root which does not70have to be the same folder.7172The third reason is "explicit is better than implicit". That object is73your WSGI application, you don't have to remember anything else. If you74want to apply a WSGI middleware, just wrap it and you're done (though75there are better ways to do that so that you do not lose the reference76to the application object :meth:`~flask.Flask.wsgi_app`).7778Furthermore this design makes it possible to use a factory function to79create the application which is very helpful for unittesting and similar80things (:ref:`app-factories`).8182The Routing System83------------------8485Flask uses the Werkzeug routing system which was designed to86automatically order routes by complexity. This means that you can declare87routes in arbitrary order and they will still work as expected. This is a88requirement if you want to properly implement decorator based routing89since decorators could be fired in undefined order when the application is90split into multiple modules.9192Another design decision with the Werkzeug routing system is that routes93in Werkzeug try to ensure that URLs are unique. Werkzeug will go quite far94with that in that it will automatically redirect to a canonical URL if a route95is ambiguous.969798One Template Engine99-------------------100101Flask decides on one template engine: Jinja2. Why doesn't Flask have a102pluggable template engine interface? You can obviously use a different103template engine, but Flask will still configure Jinja2 for you. While104that limitation that Jinja2 is *always* configured will probably go away,105the decision to bundle one template engine and use that will not.106107Template engines are like programming languages and each of those engines108has a certain understanding about how things work. On the surface they109all work the same: you tell the engine to evaluate a template with a set110of variables and take the return value as string.111112But that's about where similarities end. Jinja2 for example has an113extensive filter system, a certain way to do template inheritance, support114for reusable blocks (macros) that can be used from inside templates and115also from Python code, uses Unicode for all operations, supports116iterative template rendering, configurable syntax and more. On the other117hand an engine like Genshi is based on XML stream evaluation, template118inheritance by taking the availability of XPath into account and more.119Mako on the other hand treats templates similar to Python modules.120121When it comes to connecting a template engine with an application or122framework there is more than just rendering templates. For instance,123Flask uses Jinja2's extensive autoescaping support. Also it provides124ways to access macros from Jinja2 templates.125126A template abstraction layer that would not take the unique features of127the template engines away is a science on its own and a too large128undertaking for a microframework like Flask.129130Furthermore extensions can then easily depend on one template language131being present. You can easily use your own templating language, but an132extension could still depend on Jinja itself.133134135Micro with Dependencies136-----------------------137138Why does Flask call itself a microframework and yet it depends on two139libraries (namely Werkzeug and Jinja2). Why shouldn't it? If we look140over to the Ruby side of web development there we have a protocol very141similar to WSGI. Just that it's called Rack there, but besides that it142looks very much like a WSGI rendition for Ruby. But nearly all143applications in Ruby land do not work with Rack directly, but on top of a144library with the same name. This Rack library has two equivalents in145Python: WebOb (formerly Paste) and Werkzeug. Paste is still around but146from my understanding it's sort of deprecated in favour of WebOb. The147development of WebOb and Werkzeug started side by side with similar ideas148in mind: be a good implementation of WSGI for other applications to take149advantage.150151Flask is a framework that takes advantage of the work already done by152Werkzeug to properly interface WSGI (which can be a complex task at153times). Thanks to recent developments in the Python package154infrastructure, packages with dependencies are no longer an issue and155there are very few reasons against having libraries that depend on others.156157158Thread Locals159-------------160161Flask uses thread local objects (context local objects in fact, they162support greenlet contexts as well) for request, session and an extra163object you can put your own things on (:data:`~flask.g`). Why is that and164isn't that a bad idea?165166Yes it is usually not such a bright idea to use thread locals. They cause167troubles for servers that are not based on the concept of threads and make168large applications harder to maintain. However Flask is just not designed169for large applications or asynchronous servers. Flask wants to make it170quick and easy to write a traditional web application.171172Also see the :ref:`becomingbig` section of the documentation for some173inspiration for larger applications based on Flask.174175176What Flask is, What Flask is Not177--------------------------------178179Flask will never have a database layer. It will not have a form library180or anything else in that direction. Flask itself just bridges to Werkzeug181to implement a proper WSGI application and to Jinja2 to handle templating.182It also binds to a few common standard library packages such as logging.183Everything else is up for extensions.184185Why is this the case? Because people have different preferences and186requirements and Flask could not meet those if it would force any of this187into the core. The majority of web applications will need a template188engine in some sort. However not every application needs a SQL database.189190The idea of Flask is to build a good foundation for all applications.191Everything else is up to you or extensions.