technically, you don't need blueprints, you can just register each route on your create_app
function. Generally speaking that's not a great idea, and it's kind of why blueprints exist.
Example without blueprints
def create_app():
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
return app
You can have a single app factory for both testing and whatever else if you configure it that way. If you want to load different blueprints based on if it is in 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 like this:
class DefaultConfig(object):
PROJECT_NAME = 'my project'
class TestingConfig(DefaultConfig):
TESTING = True
class DevConfig(DefaultConfig):
DEBUG = True
configurations = {
'testing': TestingConfig,
'dev': DevConfig,
'default': DefaultConfig
}
Make a folder for each blueprint where the __init__.py
in the folder instantiates the blueprint. Let's say for a blueprint called 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')