Skip to content

Instantly share code, notes, and snippets.

@hersonls
Created February 8, 2014 03:23
Show Gist options
  • Save hersonls/8876217 to your computer and use it in GitHub Desktop.
Save hersonls/8876217 to your computer and use it in GitHub Desktop.
Flask: Load blueprints from given path
import os
from importlib import import_module
from flask.blueprints import Blueprint
def load_blueprints_from_path(app, packages_path, blueprint_name='bp'):
blueprints = []
for name in os.listdir(packages_path):
if os.path.isdir(os.path.join(packages_path, name)):
blueprint_name = blueprint_name
path, base_name = os.path.split(packages_path)
package_name = name
try:
package = import_module('{}.{}'.format(base_name, package_name))
module = getattr(package, blueprint_name)
if isinstance(module, Blueprint):
blueprints.append(module)
# Register blueprint
app.register_blueprint(module)
print "Blueprint Installed: {}".format(name)
except ImportError, AttributeError:
pass
return blueprints
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment