I have a flask api which I have wrapped up in an object. Doing this has made unit testing a breeze, because I can instantiate the api with a variety of different settings de
This way it's possible to add a blueprint, passing parameters, inside an appfactory. Sometimes you'll need it and it's not described anywhere in the flask docs.
Assuming bp1.py, bp2.py are in the same folder as your appname
from appname import bp1, bp2
app.register_blueprint(bp1.bp)
# with parameters:
app.register_blueprint(bp2.construct_blueprint(arg1, arg2))
inside bp2.py
:
def construct_blueprint(arg1, arg2):
bp = Blueprint("bp2", __name__)
@bp.route('/test')
def test():
ret = {'arg1': arg1, 'arg2': arg2}
return jsonify(ret)
return bp
You could create the blueprint dynamically in a constructor function:
def construct_blueprint(database):
myblueprint = Blueprint('myblueprint', __name__)
@myblueprint.route('/route', methods=['GET'])
def route():
database = database
return(myblueprint)