[PYTHON] Flask site construction memorandum

Directory structure

The basic configuration is as follows, and write the initial operation in \ __ init__.py under app.

root/ ┣run.py ┣app/  ┣_init_.py  ┣config.py  ┣module1/   ┣_init_.py   ┣models.py   ┣controllers.py   ┣forms.py  ┣templates/  ┣static/

run.py


from app import create_app

if __name__ == "__main__":
    app = create_app()
    app.run()

app/__init__.pyy


from flask import Flask
from flask_debugtoolbar import DebugToolbarExtension
from flask.ext.sqlalchemy import SQLAlchemy # Import SQLAlchemy
from flask.ext.mongoengine import MongoEngine # Import Mongoengine
from flask.ext.admin import Admin
from app import config

# app setup
db = SQLAlchemy()
mongodb = MongoEngine()
admin = Admin()
toolbar = DebugToolbarExtension()

def create_app():

    app = Flask()
    app.config.config_from_object(config)

    #Initializing
    db.init_app(app)
    mongodb.init_app(app)
    admin.init_app(app)
    toolbar.init_app(app)

   # Import a module
    from app.module1.controllers import mod_app

    # Register blueprint(s)
    app.register_blueprint(mod_app)

    # Build the database:
    # This will create the database file using SQLAlchemy
    db.create_all()

    return app

If you do like this, you can specify url_prefix in each module and register with register_bluepoint. You can leave the config as a file, but in the case of windows etc., "../" is subtle, so I think it's better to import it and then read it as an object. Templates and satatic can be anywhere, but under the app is smart.

Recommended Posts

Flask site construction memorandum
Vue.js + Flask environment construction memorandum ~ with Anaconda3 ~
The highlight of REST + Normal Site construction with Flask, Eve
[Django] Memorandum of environment construction procedure
flask
flask
Memorandum / memo about programming learning / competitive programming site
Infrastructure construction practice by AWS, Docker memorandum