<h1>Application Factories<aclass="headerlink"href="#application-factories"title="Link to this heading">¶</a></h1>
<p>If you are already using packages and blueprints for your application
(<aclass="reference internal"href="../blueprints.html"><spanclass="doc">Modular Applications with Blueprints</span></a>) there are a couple of really nice ways to further improve
the experience. A common pattern is creating the application object when
the blueprint is imported. But if you move the creation of this object
into a function, you can then create multiple instances of this app later.</p>
<p>So why would you want to do this?</p>
<olclass="arabic simple">
<li><p>Testing. You can have instances of the application with different
settings to test every case.</p></li>
<li><p>Multiple instances. Imagine you want to run different versions of the
same application. Of course you could have multiple instances with
different configs set up in your webserver, but if you use factories,
you can have multiple instances of the same application running in the
same application process which can be handy.</p></li>
</ol>
<p>So how would you then actually implement that?</p>
<sectionid="basic-factories">
<h2>Basic Factories<aclass="headerlink"href="#basic-factories"title="Link to this heading">¶</a></h2>
<p>The idea is to set up the application in a function. Like this:</p>
<p>Using this design pattern, no application-specific state is stored on the
extension object, so one extension object can be used for multiple apps.
For more information about the design of extensions refer to <aclass="reference internal"href="../extensiondev.html"><spanclass="doc">Flask Extension Development</span></a>.</p>
</section>
<sectionid="using-applications">
<h2>Using Applications<aclass="headerlink"href="#using-applications"title="Link to this heading">¶</a></h2>
<p>To run such an application, you can use the <strongclass="command">flask</strong> command:</p>
<divclass="highlight-text notranslate"><divclass="highlight"><pre><span></span>$ flask --app hello run
</pre></div>
</div>
<p>Flask will automatically detect the factory if it is named
<codeclass="docutils literal notranslate"><spanclass="pre">create_app</span></code> or <codeclass="docutils literal notranslate"><spanclass="pre">make_app</span></code> in <codeclass="docutils literal notranslate"><spanclass="pre">hello</span></code>. You can also pass arguments
to the factory like this:</p>
<divclass="highlight-text notranslate"><divclass="highlight"><pre><span></span>$ flask --app 'hello:create_app(local_auth=True)' run
</pre></div>
</div>
<p>Then the <codeclass="docutils literal notranslate"><spanclass="pre">create_app</span></code> factory in <codeclass="docutils literal notranslate"><spanclass="pre">hello</span></code> is called with the keyword
argument <codeclass="docutils literal notranslate"><spanclass="pre">local_auth=True</span></code>. See <aclass="reference internal"href="../cli.html"><spanclass="doc">Command Line Interface</span></a> for more detail.</p>
</section>
<sectionid="factory-improvements">
<h2>Factory Improvements<aclass="headerlink"href="#factory-improvements"title="Link to this heading">¶</a></h2>
<p>The factory function above is not very clever, but you can improve it.
The following changes are straightforward to implement:</p>
<olclass="arabic simple">
<li><p>Make it possible to pass in configuration values for unit tests so that
you don’t have to create config files on the filesystem.</p></li>
<li><p>Call a function from a blueprint when the application is setting up so
that you have a place to modify attributes of the application (like
hooking in before/after request handlers etc.)</p></li>
<li><p>Add in WSGI middlewares when the application is being created if necessary.</p></li>