Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flask blueprints list routes

I have a flask app with the below tree:

root/
  - modules/
    __init__.py
    - common/
        __init__.py
    - citrixlb/
        __init__.py # blueprint code
        citrixlb.py # module class
    - aws/
        __init__.py # blueprint code
        aws.py      # module class

So I would like to know, is there a way to dynamically list all routes for all blueprints?

I have this in my root/__init__.py:

# import our modules
from modules.citrixlb import *
from modules.aws import *

app.register_blueprint(aws)
app.register_blueprint(citrix)

If I can dynamically list all routes for all blueprints, I can auto generate the links.

Example:

DropDown: AWS

  • /aws

  • /aws/ec2

  • /aws/subnets

like image 716
Simply Seth Avatar asked May 06 '15 15:05

Simply Seth


1 Answers

def get_bp_urls(blueprint):
    from flask import Flask
    temp_app = Flask(__name__) 
    temp_app.register_blueprint(blueprint)
    return [str(p) for p in temp_app.url_map.iter_rules()]
like image 200
Mohamed Feddad Avatar answered Sep 18 '22 14:09

Mohamed Feddad