Created
April 9, 2014 08:23
-
-
Save wong2/10240962 to your computer and use it in GitHub Desktop.
Flask patterns. From https://github.com/mattupstate/overholt
This file contains 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
# -*- coding: utf-8 -*- | |
""" | |
core module, defines some extension instances etc. | |
""" | |
from flask_sqlalchemy import SQLAlchemy | |
#: Flask-SQLAlchemy extension instance | |
db = SQLAlchemy() |
This file contains 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
# -*- coding: utf-8 -*- | |
""" | |
factory module | |
""" | |
import os | |
from flask import Flask | |
from .core import db | |
from .helpers import register_blueprints | |
def create_app(package_name, package_path, settings_override=None): | |
"""Returns a :class:`Flask` application instance""" | |
:param package_name: application package name | |
:param package_path: application package path | |
:param settings_override: a dictionary of settings to override | |
""" | |
app = Flask(package_name, instance_relative_config=True) | |
app.config.from_object('.settings') | |
app.config.from_pyfile('settings.cfg', silent=True) | |
app.config.from_object(settings_override) | |
db.init_app(app) | |
register_blueprints(app, package_name, package_path) | |
return app |
This file contains 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
# -*- coding: utf-8 -*- | |
""" | |
helpers module | |
""" | |
import pkgutil | |
import importlib | |
from flask import Blueprint | |
def register_blueprints(app, package_name, package_path): | |
"""Register all Blueprint instances on the specified Flask application found | |
in all modules for the specified package. | |
:param app: the Flask application | |
:param package_name: the package name | |
:param package_path: the package path | |
""" | |
rv = [] | |
for _, name, _ in pkgutil.iter_modules(package_path): | |
m = importlib.import_module('%s.%s' % (package_name, name)) | |
for item in dir(m): | |
item = getattr(m, item) | |
if isinstance(item, Blueprint): | |
app.register_blueprint(item) | |
rv.append(item) | |
return rv |
This file contains 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
# -*- coding: utf-8 -*- | |
""" | |
settings module | |
""" | |
DEBUG = True | |
SECRET_KEY = 'super-secret-key' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment