Skip to content

Instantly share code, notes, and snippets.

@cybertoast
Last active September 25, 2024 16:35
Show Gist options
  • Save cybertoast/6499708 to your computer and use it in GitHub Desktop.
Save cybertoast/6499708 to your computer and use it in GitHub Desktop.
Get a list of all flask routes, and their endpoint's docstrings as a helper resource for API documentation.
@admin_api.route('/help', methods=['GET'])
def routes_info():
"""Print all defined routes and their endpoint docstrings
This also handles flask-router, which uses a centralized scheme
to deal with routes, instead of defining them as a decorator
on the target function.
"""
routes = []
for rule in app.url_map.iter_rules():
try:
if rule.endpoint != 'static':
if hasattr(app.view_functions[rule.endpoint], 'import_name'):
import_name = app.view_functions[rule.endpoint].import_name
obj = import_string(import_name)
routes.append({rule.rule: "%s\n%s" % (",".join(list(rule.methods)), obj.__doc__)})
else:
routes.append({rule.rule: app.view_functions[rule.endpoint].__doc__})
except Exception as exc:
routes.append({rule.rule:
"(%s) INVALID ROUTE DEFINITION!!!" % rule.endpoint})
route_info = "%s => %s" % (rule.rule, rule.endpoint)
app.logger.error("Invalid route: %s" % route_info, exc_info=True)
# func_list[rule.rule] = obj.__doc__
return jsonify(code=200, data=routes)
@stefets
Copy link

stefets commented Nov 18, 2021

from werkzeug.utils import import_string

@alexzanderr
Copy link

its, working, i can get all routes defined from code, without even trying to look for them. thanks a lot for this gist man.

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