Skip to content

Instantly share code, notes, and snippets.

@jtratner
Last active November 26, 2019 14:24
Show Gist options
  • Select an option

  • Save jtratner/3844721 to your computer and use it in GitHub Desktop.

Select an option

Save jtratner/3844721 to your computer and use it in GitHub Desktop.

Revisions

  1. jtratner revised this gist May 19, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion dynamic_blueprints.py
    Original file line number Diff line number Diff line change
    @@ -27,6 +27,6 @@ def register_blueprints(app, files, blueprint_attribute):

    if __name__ == "__main__":
    # get the files that end in py in the folder
    filelist = [file for file in os.listdir(PATH) if file.endswith ".py"]
    filelist = (file for file in os.listdir(PATH) if file.endswith ".py")
    from my_app import app
    register_blueprints(app, filelist, BLUEPRINT)
  2. jtratner revised this gist Oct 6, 2012. 1 changed file with 10 additions and 4 deletions.
    14 changes: 10 additions & 4 deletions dynamic_blueprints.py
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,6 @@
    PATH = path/to/my/blueprints/directory
    BLUEPRINT = 'the_blueprint'



    def import_file(path, name=None):
    """ imports a file with given name and path """
    # use the imp module to do actual imports
    @@ -14,7 +12,15 @@ def import_file(path, name=None):
    return imp.load_module(name, fp, pathname, description)

    def register_blueprints(app, files, blueprint_attribute):
    """ registers the blueprints from a given set of files on the given app """
    """ registers the blueprints from a given set of files on the given app
    Parameters:
    app - the Flask application on which to register the blueprints
    files - an iterable of file names to be dynamically imported
    blueprint_attribute - the name of the actually blueprint in each file.
    (e.g. for the flask blueprints example - http://flask.pocoo.org/docs/blueprints/ - you'd
    use 'simple_page')
    """
    imported_modules = [import_file(file) for file in files]
    for module in imported_modules:
    app.register(getattr(module, blueprint_attribute))
    @@ -23,4 +29,4 @@ def register_blueprints(app, files, blueprint_attribute):
    # get the files that end in py in the folder
    filelist = [file for file in os.listdir(PATH) if file.endswith ".py"]
    from my_app import app
    register_blueprints(app, filelist, BLUEPRINT_ATTRIBUTE)
    register_blueprints(app, filelist, BLUEPRINT)
  3. jtratner created this gist Oct 6, 2012.
    26 changes: 26 additions & 0 deletions dynamic_blueprints.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    import os

    PATH = path/to/my/blueprints/directory
    BLUEPRINT = 'the_blueprint'



    def import_file(path, name=None):
    """ imports a file with given name and path """
    # use the imp module to do actual imports
    import imp
    name = name or os.path.split(path)[-1].replace(".", "_")
    fp, pathname, description = imp.find_module(path)
    return imp.load_module(name, fp, pathname, description)

    def register_blueprints(app, files, blueprint_attribute):
    """ registers the blueprints from a given set of files on the given app """
    imported_modules = [import_file(file) for file in files]
    for module in imported_modules:
    app.register(getattr(module, blueprint_attribute))

    if __name__ == "__main__":
    # get the files that end in py in the folder
    filelist = [file for file in os.listdir(PATH) if file.endswith ".py"]
    from my_app import app
    register_blueprints(app, filelist, BLUEPRINT_ATTRIBUTE)