Skip to content

Instantly share code, notes, and snippets.

@mattupstate
Created June 25, 2013 15:04
Show Gist options
  • Select an option

  • Save mattupstate/5859194 to your computer and use it in GitHub Desktop.

Select an option

Save mattupstate/5859194 to your computer and use it in GitHub Desktop.
Register all Blueprint instances on the specified Flask application found in all modules for the specified package
# -*- coding: utf-8 -*-
"""
helpers
~~~~~~~
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
@saulr7

saulr7 commented Jan 15, 2020

Copy link
Copy Markdown

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment