Last active
September 25, 2024 16:35
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) |
from werkzeug.utils import import_string
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
NameError: name 'import_string' is not defined