According to Flask Documentation , the factory application is good because:
Testing. You may have application instances with different settings to test each case.
Several copies. Imagine that you want to run different versions of the same application. Of course, you can have several instances with different settings configured on your web server, but if you use factories, you can have several instances of the same application running in the same application, which can be convenient.
But, as stated in the Other Testing Tricks section of the documentation, if you use application factories, the before_request() and after_request() functions will not be called automatically.
In the following paragraphs, I will show how I used the factory application template with the uWSGI and nginx application server (I only used them, but I can try to help you configure it on another server).
Factory application
So, let's say you have an application inside the yourapplication folder and inside it is the __init__.py file:
import os from flask import Flask def create_app(cfg=None): app = Flask(__name__) load_config(app, cfg)
Now you need a file to create an instance of the application:
from yourapplication import create_app app = create_app() if __name__ == "__main__": app.run()
In the above code, I assume that the environment variable is set using the path to the configuration file, but you can specify the configuration path to the factory, for example:
app = create_app('config/prod.cfg')
In addition, you may have something like a dictionary with environments and related configuration files:
CONFIG_FILES = {'development': 'config/development.cfg', 'test' : 'config/test.cfg', 'production' : 'config/production.cfg' }
In this case, the load_config function will look like this:
def load_config(app, env): app.config.from_pyfile('config/default.cfg') var = "YOURAPPLICATION_ENV" if env is None and var in os.environ: env = os.environ[var] if env in CONFIG_FILES: app.config.from_pyfile(CONFIG_FILES[env])
Nginx and uWSGI
Here is an example configuration file for nginx:
server { listen 80; server_name yourapplication.com; access_log /var/www/yourapplication/logs/access.log; error_log /var/www/yourapplication/logs/error.log; location / { try_files $uri @flask; } location @flask { include uwsgi_params; uwsgi_pass unix:/tmp/yourapplication.sock;
And the uWSGI configuration file looks like this:
[uwsgi] plugins=python vhost=true socket=/tmp/yourapplication.sock env = YOURAPPLICATION_ENV=production logto = /var/www/yourapplication/logs/uwsgi.log
How to use before_request() and after_request()
The problem with these functions is that if you call them in other modules, these modules cannot be imported before the application is created. Again, the documentation has something to say about this:
The disadvantage is that you cannot use the application object in drawings during import. However, you can use it from the request. How do you access the application using config? Use current_app:
from flask import current_app, Blueprint, render_template admin = Blueprint('admin', __name__, url_prefix='/admin') @admin.route('/') def index(): return render_template(current_app.config['INDEX_TEMPLATE'])
Or you might consider creating an extension , then you can import the class without any existing Flask instances, since the class extension will only use the Flask instance after it is created.