Last active
May 22, 2019 19:45
-
-
Save icshih/864da9590a7f27fc25634be4ff6b7166 to your computer and use it in GitHub Desktop.
A Flask app factory template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from flask_sqlalchemy import SQLAlchemy | |
from flask_admin import Admin | |
from flask_security import Security, SQLAlchemyUserDatastore | |
from config import Config | |
from app.views import MyModelView | |
db = SQLAlchemy() | |
admin = Admin() | |
def create_app(): | |
app = Flask(__name__) | |
app.config.from_object(Config) | |
with app.app_context(): | |
db.init_app(app) | |
from app.models import User, Role | |
from . import models | |
# Setup Flask-Security | |
user_datastore = SQLAlchemyUserDatastore(db, User, Role) | |
security = Security(app, user_datastore) | |
# Create admin | |
admin.init_app(app) | |
# Add model views | |
admin.add_view(MyModelView(Role, db.session)) | |
admin.add_view(MyModelView(User, db.session)) | |
return app | |
if __name__ == "__main__": | |
app = create_app() | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment