diff options
Diffstat (limited to 'studio/static/doc/flask-docs/patterns/appfactories.html')
-rw-r--r-- | studio/static/doc/flask-docs/patterns/appfactories.html | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/studio/static/doc/flask-docs/patterns/appfactories.html b/studio/static/doc/flask-docs/patterns/appfactories.html new file mode 100644 index 0000000..ccd26e0 --- /dev/null +++ b/studio/static/doc/flask-docs/patterns/appfactories.html @@ -0,0 +1,186 @@ + +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> + + +<html xmlns="http://www.w3.org/1999/xhtml"> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + + <title>Application Factories — Flask 0.8 documentation</title> + + <link rel="stylesheet" href="../_static/flasky.css" type="text/css" /> + <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> + + <script type="text/javascript"> + var DOCUMENTATION_OPTIONS = { + URL_ROOT: '../', + VERSION: '0.8', + COLLAPSE_INDEX: false, + FILE_SUFFIX: '.html', + HAS_SOURCE: true + }; + </script> + <script type="text/javascript" src="../_static/jquery.js"></script> + <script type="text/javascript" src="../_static/underscore.js"></script> + <script type="text/javascript" src="../_static/doctools.js"></script> + <link rel="top" title="Flask 0.8 documentation" href="../index.html" /> + <link rel="up" title="Patterns for Flask" href="index.html" /> + <link rel="next" title="Application Dispatching" href="appdispatch.html" /> + <link rel="prev" title="Larger Applications" href="packages.html" /> + + + <link rel="apple-touch-icon" href="../_static/touch-icon.png" /> + + <link media="only screen and (max-device-width: 480px)" href="../_static/small_flask.css" type= "text/css" rel="stylesheet" /> + + </head> + <body> + <div class="related"> + <h3>Navigation</h3> + <ul> + <li class="right" style="margin-right: 10px"> + <a href="../genindex.html" title="General Index" + accesskey="I">index</a></li> + <li class="right" > + <a href="appdispatch.html" title="Application Dispatching" + accesskey="N">next</a> |</li> + <li class="right" > + <a href="packages.html" title="Larger Applications" + accesskey="P">previous</a> |</li> + <li><a href="../index.html">Flask 0.8 documentation</a> »</li> + <li><a href="index.html" accesskey="U">Patterns for Flask</a> »</li> + </ul> + </div> + + <div class="document"> + <div class="documentwrapper"> + <div class="bodywrapper"> + <div class="body"> + + <div class="section" id="application-factories"> +<span id="app-factories"></span><h1>Application Factories<a class="headerlink" href="#application-factories" title="Permalink to this headline">¶</a></h1> +<p>If you are already using packages and blueprints for your application +(<a class="reference internal" href="../blueprints.html#blueprints"><em>Modular Applications with Blueprints</em></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 and later.</p> +<p>So why would you want to do this?</p> +<ol class="arabic simple"> +<li>Testing. You can have instances of the application with different +settings to test every case.</li> +<li>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.</li> +</ol> +<p>So how would you then actually implement that?</p> +<div class="section" id="basic-factories"> +<h2>Basic Factories<a class="headerlink" href="#basic-factories" title="Permalink to this headline">¶</a></h2> +<p>The idea is to set up the application in a function. Like this:</p> +<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">create_app</span><span class="p">(</span><span class="n">config_filename</span><span class="p">):</span> + <span class="n">app</span> <span class="o">=</span> <span class="n">Flask</span><span class="p">(</span><span class="n">__name__</span><span class="p">)</span> + <span class="n">app</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">from_pyfile</span><span class="p">(</span><span class="n">config_filename</span><span class="p">)</span> + + <span class="kn">from</span> <span class="nn">yourapplication.views.admin</span> <span class="kn">import</span> <span class="n">admin</span> + <span class="kn">from</span> <span class="nn">yourapplication.views.frontend</span> <span class="kn">import</span> <span class="n">frontend</span> + <span class="n">app</span><span class="o">.</span><span class="n">register_blueprint</span><span class="p">(</span><span class="n">admin</span><span class="p">)</span> + <span class="n">app</span><span class="o">.</span><span class="n">register_blueprint</span><span class="p">(</span><span class="n">frontend</span><span class="p">)</span> + + <span class="k">return</span> <span class="n">app</span> +</pre></div> +</div> +<p>The downside is that you cannot use the application object in the blueprints +at import time. You can however use it from within a request. How do you +get access to the application with the config? Use +<a class="reference internal" href="../api.html#flask.current_app" title="flask.current_app"><tt class="xref py py-data docutils literal"><span class="pre">current_app</span></tt></a>:</p> +<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">flask</span> <span class="kn">import</span> <span class="n">current_app</span><span class="p">,</span> <span class="n">Blueprint</span><span class="p">,</span> <span class="n">render_template</span> +<span class="n">admin</span> <span class="o">=</span> <span class="n">Blueprint</span><span class="p">(</span><span class="s">'admin'</span><span class="p">,</span> <span class="n">__name__</span><span class="p">,</span> <span class="n">url_prefix</span><span class="o">=</span><span class="s">'/admin'</span><span class="p">)</span> + +<span class="nd">@admin.route</span><span class="p">(</span><span class="s">'/'</span><span class="p">)</span> +<span class="k">def</span> <span class="nf">index</span><span class="p">():</span> + <span class="k">return</span> <span class="n">render_template</span><span class="p">(</span><span class="n">current_app</span><span class="o">.</span><span class="n">config</span><span class="p">[</span><span class="s">'INDEX_TEMPLATE'</span><span class="p">])</span> +</pre></div> +</div> +<p>Here we look up the name of a template in the config.</p> +</div> +<div class="section" id="using-applications"> +<h2>Using Applications<a class="headerlink" href="#using-applications" title="Permalink to this headline">¶</a></h2> +<p>So to use such an application you then have to create the application +first. Here an example <cite>run.py</cite> file that runs such an application:</p> +<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">yourapplication</span> <span class="kn">import</span> <span class="n">create_app</span> +<span class="n">app</span> <span class="o">=</span> <span class="n">create_app</span><span class="p">(</span><span class="s">'/path/to/config.cfg'</span><span class="p">)</span> +<span class="n">app</span><span class="o">.</span><span class="n">run</span><span class="p">()</span> +</pre></div> +</div> +</div> +<div class="section" id="factory-improvements"> +<h2>Factory Improvements<a class="headerlink" href="#factory-improvements" title="Permalink to this headline">¶</a></h2> +<p>The factory function from above is not very clever so far, you can improve +it. The following changes are straightforward and possible:</p> +<ol class="arabic simple"> +<li>make it possible to pass in configuration values for unittests so that +you don’t have to create config files on the filesystem</li> +<li>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.)</li> +<li>Add in WSGI middlewares when the application is creating if necessary.</li> +</ol> +</div> +</div> + + + </div> + </div> + </div> + <div class="sphinxsidebar"> + <div class="sphinxsidebarwrapper"><p class="logo"><a href="../index.html"> + <img class="logo" src="../_static/flask.png" alt="Logo"/> +</a></p> + <h3><a href="../index.html">Table Of Contents</a></h3> + <ul> +<li><a class="reference internal" href="#">Application Factories</a><ul> +<li><a class="reference internal" href="#basic-factories">Basic Factories</a></li> +<li><a class="reference internal" href="#using-applications">Using Applications</a></li> +<li><a class="reference internal" href="#factory-improvements">Factory Improvements</a></li> +</ul> +</li> +</ul> +<h3>Related Topics</h3> +<ul> + <li><a href="../index.html">Documentation overview</a><ul> + <li><a href="index.html">Patterns for Flask</a><ul> + <li>Previous: <a href="packages.html" title="previous chapter">Larger Applications</a></li> + <li>Next: <a href="appdispatch.html" title="next chapter">Application Dispatching</a></li> + </ul></li> + </ul></li> +</ul> + <h3>This Page</h3> + <ul class="this-page-menu"> + <li><a href="../_sources/patterns/appfactories.txt" + rel="nofollow">Show Source</a></li> + </ul> +<div id="searchbox" style="display: none"> + <h3>Quick search</h3> + <form class="search" action="../search.html" method="get"> + <input type="text" name="q" /> + <input type="submit" value="Go" /> + <input type="hidden" name="check_keywords" value="yes" /> + <input type="hidden" name="area" value="default" /> + </form> + <p class="searchtip" style="font-size: 90%"> + Enter search terms or a module, class or function name. + </p> +</div> +<script type="text/javascript">$('#searchbox').show(0);</script> + </div> + </div> + <div class="clearer"></div> + </div> + <div class="footer"> + © Copyright 2010, Armin Ronacher. + Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>. + </div> + </body> +</html>
\ No newline at end of file |