Python 如何将url_map.iter_规则与blueprint对象而不是应用程序一起使用

Python 如何将url_map.iter_规则与blueprint对象而不是应用程序一起使用,python,flask,flask-restful,Python,Flask,Flask Restful,我有一个blueprint对象“api”和一个api.py文件,其中我有许多用api.route注释定义的api。例如: @api.route('/', methods=['GET']) def get_info(): 我想迭代并获得所有API的摘要,这些API与我们在app对象上使用“url\u map.iter\u rules”时得到的相同。我们如何使用api blueprint对象实现这一点?我已经在init.py文件中使用 from .api_1 import api as api_b

我有一个blueprint对象“api”和一个api.py文件,其中我有许多用api.route注释定义的api。例如:

@api.route('/', methods=['GET'])
def get_info():
我想迭代并获得所有API的摘要,这些API与我们在app对象上使用“url\u map.iter\u rules”时得到的相同。我们如何使用api blueprint对象实现这一点?我已经在init.py文件中使用

from .api_1 import api as api_blueprint
app.register_blueprint(api_blueprint)

我想如果你在注册蓝图后调用app.url\u map.iter\u rules(),你也会得到子域的所有端点,例如

api.py

from flask import Blueprint
api = Blueprint('api', __name__)
@api.route('/')
def call_api():
        return ""
init.py:

from flask import Flask, Blueprint
from api import api

public = Blueprint('public', __name__)
@public.route('/')
def home():
        return render_template('public/home.html')


app = Flask(__name__)
app.register_blueprint(public)
app.register_blueprint(api, subdomain='api')
print(list(app.url_map.iter_rules()))

[<Rule 'api|/' (GET, HEAD, OPTIONS) -> api.call_api>,
 <Rule '/' (GET, HEAD, OPTIONS) -> public.home>,
 <Rule '/static/<filename>' (GET, HEAD, OPTIONS) -> static>]
从烧瓶导入烧瓶,蓝图
从api导入api
public=蓝图('public',_名称__)
@public.route(“/”)
def home():
返回呈现模板('public/home.html')
app=烧瓶(名称)
应用程序注册蓝图(公共)
应用程序注册蓝图(api,子域='api')
打印(列表(app.url\u map.iter\u rules())
[api.call_api>,
public.home>,
静态>]

如果您觉得有用,我制作了一个函数,根据主应用程序中注册的蓝图显示每个url(仅用于测试)。 这是我发现的唯一能够打印端点的解决方案,这些端点通过它们所属的蓝图来分隔。 当然,您可以创建一个函数,通过以字符串格式传递名称或蓝图本身,只打印其中一个蓝图的url_映射。 以下是一些例子:

from flask.logging import create_logger
def log_routes(app: Flask):

    log = create_logger(app)

    with app.app_context():
        """
        Maps every single endpoint for blueprint registered in the main application.
        Also shows methos available for each endpoint
        """

        log.info('MAP ROUTER')
        bps = app.blueprints
        for bp in bps:
            print('', end='\n')
            log.info(f'BLUEPRINT RULES: "{bp}"')
            for ep in app.url_map.iter_rules():
                bp_name = ep.endpoint.split('.')[0]
                if bp_name == bp:
                    log.debug(f'Endpoint: {ep} methods={ep.methods}')
下面是一个函数示例,该函数仅获取需要从中获取其url\u映射的蓝图的名称:

def log_blueprint_urls(app: Flask, bp_name: str):

    log = create_logger(app)

    with app.app_context():
        """
        Maps every single endpoint for an specific blueprint in the main application.
        Also shows methos available for each endpoint
        """
        bps = app.blueprints
        if bp_name in bps:
            log.info(f'MAP ROUTER FOR BLUEPRINT "{bp_name}"')

            for ep in app.url_map.iter_rules():
                bp_name = ep.endpoint.split('.')[0]
                if bp_name == bp_name:
                    log.debug(f'Endpoint: {ep} methods={ep.methods}')
        else:
            log.critical(
                f'BLUEPRINT "{bp_name}" has not registered in main application')

我在init.py中启动我的app对象,如我的问题所示,然后我有另一个名为api.py的文件,我在其中定义了所有API。因此,我没有在apis.py文件中注册蓝图,我也没有app对象。我只是从中使用
导入api.py中的蓝图。导入api
。你能告诉我我需要在api.py或init.py中包含什么吗?我需要这是单独的API
API.route('/API/help',methods=['GET'])
,并且我已经有了代码来使用
app.url\u map.iter\u rules()
在这里使用app对象我想使用:whch读取文档字符串:
@API.route('/API/help',methods=['GET'])def help():func\u list={}对于app.url\u map.iter\u rules()中的规则:if rule.endpoint!='静态:docstring=app.view\u函数[rule.endpoint]。\uu doc\uuuu如果docstring:func\u list[docstring]=rule.rule返回jsonify(func\u list)
ok,那么我已经在回答中重新排列了模块,这符合您的情况了吗?模块中不需要app对象。什么是
public.route('/')
?我们不能跳过这个吗?我还希望这个输出是一个json格式,key:value=function\u name:url,这可能吗?我想你可以跳过它,但你可能不应该,最好公开这个“默认”路由,以便进行健康检查,以查看你的服务是否正在运行。如果需要json,则需要编写一些代码来解析输出并将其放入dict中