1

I want to use Flask-Perm's decorators in different modules in my application. I am using the app factory pattern. If I create the extension in the factory, I can't import it to use in the other modules. How can I import an extension when using an app factory?

from flask_perm import Perm

def create_app():
    app = Flask(__name__)
    perm = Perm(app)
    return app
davidism
  • 121,510
  • 29
  • 395
  • 339
ChrisGuest
  • 3,398
  • 4
  • 32
  • 53

1 Answers1

2

Flask-Perm's docs show how to do this right at the top.

Create the extension outside the factory. Init the extension inside the factory. Every Flask extension that supports app factories uses this pattern.

perm = Perm()

def create_app():
    app = Flask(__name__)
    perm.init_app(app)
    return app

Now you can use from myproject import perm wherever you need to access it.

davidism
  • 121,510
  • 29
  • 395
  • 339