Skip to content
Myr

Added appfactory pattern

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 182ee31 · Browse files · 2 files+74 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+74 added−0 removed

  • other:+74 added−0 removed

2 files+74 added−0 removed

added:docs/patterns/appfactories.rst+73 added
1Application Factories2=====================34If you are already using packages and modules for your application5(:ref:`packages`) there are couple of really nice ways to further improve6the experience. A common pattern is creating the application object when7the module is imported. But if you move the creation of this object,8into a function, you can then create multiple instances of this and later.910So why would you want to do this?11121. Testing. You can have instances of the application with different13 settings to test every case.142. Multiple instances. Imagine you want to run different versions of the15 same application. Of course you could have multiple instances with16 different configs set up in your webserver, but if you use factories,17 you can have multiple instances of the same application running in the18 same application process which can be handy.1920So how would you then actually implement that?2122Basic Factories23---------------2425The idea is to set up the application in a function. Like this::2627 def create_app(config_filename):28 app = Flask(__name__)29 app.config.from_pyfile(config_filename)3031 from yourapplication.views.admin import admin32 from yourapplication.views.frontend import frontend33 app.register_module(admin)34 app.register_module(frontend)3536 return app3738The downside is that you cannot use the application object in the modules39at import time. You can however use it from within a request. How do you40get access the application with the config? Use41:data:`~flask.current_app`::4243 from flask import current_app, Module, render_template44 admin = Module(__name__, url_prefix='/admin')4546 @admin.route('/')47 def index():48 return render_template(current_app.config['INDEX_TEMPLATE'])4950Here we look up the name of a template in the config.5152Using Applications53------------------5455So to use such an application you then have to create the application56first. Here an example `run.py` file that runs such an application::5758 from yourapplication import create_app59 app = create_app('/path/to/config.cfg')60 app.run()6162Factory Improvements63--------------------6465The factory function from above is not very clever so far, you can improve66it. The following changes are straightforward and possible:67681. make it possible to pass in configuration values for unittests so that69 you don't have to create config files on the filesystem702. call a function from a module when the application is setting up so71 that you have a place to modify attributes of the application (like72 hooking in before / after request handlers etc.)733. Add in WSGI middlewares when the application is creating if necessary.
modified:docs/patterns/index.rst+1 added
15 unmodified lines
16.. toctree::17 :maxdepth: 21819 packages20 appfactories21 distribute22 sqlite323 sqlalchemy24 fileuploads
8 unmodified lines