samedi 16 septembre 2017

Flask with Python: How do i share app object and organize code in factory pattern

I'm following this instruction http://ift.tt/2kNjqIe

However, i don't understand how do i share app object to other package like Flask-Admin?

For now, i'm passing app object around like this (app/init.py):

from .admin import register_admin

def create_app():
    app = Flask(__name__)
    ...
    register_admin(app)
    ...
    return app

I have admin folder, i want to put all code about admin execution here like routing, config, admin info ... In the app/admin/__init__.py file:

from flask_admin import Admin
def register_admin(app):
    Admin(app, name='admin', template_mode='bootstrap3')

But by doing this way there are few things make me confused:

  1. I have to pass app object around, i don't thing this is good practice
  2. Even if i don't access to http://localhost:5000/admin path, the register_admin method still being called at the initialization - I want register admin only when user access to admin panel

  3. Every piece of code i put inside create_app will be execute twice even after putting if __name__ == '__main__' in run.py file ?!!

Aucun commentaire:

Enregistrer un commentaire