Can I save all checkbox drawings in one file?

I am using a factory app to initialize my app. In it, I import all the drawings and register them one at a time. Is there a way to move the imports and registrations to another file, or communicate this to the factory without referencing them separately?

def create_app(config_filename):
    app = Flask(__name__)
    app.config.from_object(config_filename)

    from app.users.models import db
    db.init_app(app)

    from app.users.views import users
    from app.posts.views import posts
    app.register_blueprint(posts, url_prefix='/posts')
    app.register_blueprint(users, url_prefix='/users')

    return app

      

In my project, I am actually creating blueprints using another script, so I would like to be able to generate the registration by adding a file or whatever, instead of trying to change the code in the factory.

+3


source to share


1 answer


Yes, you can import and register drawings in any other module. But there is no practical point for this approach, it just moves imports and logs calls somewhere else.

myapp/blueprints.py

:

from app.users.views import users
from app.posts.views import posts

def init_app(app):
    app.register_blueprint(users, prefix='/users')
    app.register_blueprint(posts, prefix='/posts')

      

myapp/__init__.py

:



def create_app():
    app = Flask(__name__)
    # ...
    from myapp import blueprints
    blueprints.init_app(app)
    # ...

      


Something more useful might be to tell the application which packages to import, and the application expects to find a plan in some standard location for each package. Assuming the blueprint variable will always have the same name as the package is defined in views

and has the same prefix as the name:

from werkzeug.utils import import_string

def create_app():
    app = Flask(__name__)
    # ...
    for name in ('users', 'posts'):
        bp = import_string('myapp.{0}.views:{1}'.format(name, name))
        app.register_blueprint(bp, prefix='/{0}'.format(name))
    # ...

      

+1


source







All Articles