Closed diloreto closed 2 years ago
It sounds like you are describing the Application Factory approach to structuring flask apps, which is completely supported by this extension. A basic example might look like (note this code has not been tested):
# extensions.py
from flask import jsonify
from flask_jwt_extended import JWTManager
jwt = JWTManager()
@jwt.unauthoirzed_loader
def custom_unauthorized(_error):
return jsonfiy(foo='bar'), 200
# user_routes.py
from flask import Blueprint, jsonify
from flask_jwt_extended import jwt_required
user_blueprint = Blueprint('blueprint', __name__, url_prefix='/users')
@user_blueprint.route('/')
@jwt_required()
def index():
return jsonify(hello='world')
# app.py
from flask import Flask
def create_app():
app = Flask(__name__)
from extensions import jwt
jwt.init_app(app)
from user_routes import user_blueprint
app.register_blueprint(user_blueprint)
return app
Additionally, if you don't want to use the @jwt_required()
decorator in your routes, you can call verify_jwt_in_request() directly in your routes implementation, which will do the same thing as the decorator.
This is perfect! Thank you very much @vimalloc ! We'll give this a shot.
We're trying to make our code cleaner to read and extensible, and have broken up our Flask app in the following manner:
Additionally, our
routes.py
imports aservices.py
where all of the functions for each route are contained.Our question is, is it possible to use Flask JWT Extended if our
app.py
does not have the routes contained within it, and if the route logic is contained within a 3rd file? I would love to see an example of how to use this service when you don't have a monolithicapp.py
file.Thanks!