Flask: drawings needed for application factories?

I want to have several application factories (currently: one for development and one for testing). I am wondering how to implement them correctly.

I am currently using an application object to register views (via @app.route() decorator). Do I need to start using drawings (instead of the application) to register views? Is there a way to have suitable application factories without drawings?

+11
python flask flask-sqlalchemy
source share
3 answers

technically, you don't need drawings, you can simply register each route in its create_app function. Generally speaking, this is not a great idea, and this is why drawings exist.

Example without drawings

 def create_app(): app = Flask(__name__) @app.route('/') def index(): return render_template('index.html') return app 

You can have one factory application for testing and any other if you configure it this way. If you want to load different drawings based on, if they are in the process of testing, you can do something like this.

 from project.config import configurations as c def create_app(config=None): " make the app " app = Flask(__name__) app.config.from_object(c.get(config, None) or c['default']) configure_blueprints(app) return app def configure_blueprints(app): " register the blueprints on your app " if app.testing: from project.test_bp import bp app.register_blueprint(bp) else: from project.not_test_bp import bp app.register_blueprint(bp) 

then project/config.py could be as follows:

 class DefaultConfig(object): PROJECT_NAME = 'my project' class TestingConfig(DefaultConfig): TESTING = True class DevConfig(DefaultConfig): DEBUG = True configurations = { 'testing': TestingConfig, 'dev': DevConfig, 'default': DefaultConfig } 

Create a folder for each project, where __init__.py in the folder creates the project. Say for drawing routes

 from flask import Blueprint bp = Blueprint('routes', __name__) from project.routes import views 

then in project/routes/views.py you can put your views.

 from project.routes import bp @bp.route('/') def index(): return render_template('routes/index.html') 
+15
source share

You probably might have a factory application without a drawing, but that often doesn't make sense.

Assuming you have an application package called myapp . Here is your __init__.py :

 from flask import Flask def create_app(): app = Flask(__name__) return app 

Now we create a new .py file called autoapp.py (or wsgi.py or manage.py , etc.) outside the wsgi.py package. In autoapp.py you instantiate the application and import the views from myapp :

 from myapp import create_app app = create_app() from myapp import views 

An import statement will associate the app with your routes. You can import the app into your views.py as follows:

 from autoapp import app @app.route('/') def index(): return 'Hello!' 

Application Structure:

 myapp/ myapp/ __init__.py views.py autoapp.py 
+3
source share

For those who google this question. You do not need to use drawings. Just import current_app as an application into your routes. (Or views.py, whatever) and you are free to go.

 from flask import current_app as app 

In addition, you must add this to the factory (create_app function) to register your routes:

 with app.app_context(): from . import routes 
0
source share

All Articles