From 120a94ff45415375a8cf33598fba565a3a2a8f6f Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Sat, 9 Nov 2024 19:07:55 +1100 Subject: [PATCH] Add page to list all flask endpoints with filter --- app.py | 43 +++++++++++ templates/base.html | 44 +++++++----- templates/endpoints.html | 23 ++++++ templates/partials/endpoints_table.html | 94 +++++++++++++++++++++++++ 4 files changed, 188 insertions(+), 16 deletions(-) create mode 100644 templates/endpoints.html create mode 100644 templates/partials/endpoints_table.html diff --git a/app.py b/app.py index 59247dc..5c6e80a 100644 --- a/app.py +++ b/app.py @@ -534,6 +534,49 @@ def plot_query(query_id): plot_div = generate_plot(results_df, title) return plot_div +def get_routes(): + + return routes + +@app.route('/endpoints') +def list_endpoints(): + """ + Lists all API endpoints available in the Flask application. + + This endpoint retrieves all registered routes, excluding static routes, + and displays their details such as endpoint name, URL, allowed HTTP methods, + view function name, and a brief description. + """ + routes = [] + for rule in app.url_map.iter_rules(): + if rule.endpoint == 'static': + continue + methods = ', '.join(sorted(rule.methods - {'HEAD', 'OPTIONS'})) + route_info = { + 'endpoint': rule.endpoint, + 'url': rule.rule, + 'methods': methods, + 'view_func': app.view_functions[rule.endpoint].__name__, + 'doc': app.view_functions[rule.endpoint].__doc__ + } + routes.append(route_info) + + search = request.args.get('search', '').lower() + if search: + routes = [ + route for route in routes + if search in route['endpoint'].lower() + or search in route['url'].lower() + or search in route['methods'].lower() + or search in route['view_func'].lower() + or (route['doc'] and search in route['doc'].lower()) + ] + + if htmx: + return render_template('partials/endpoints_table.html', routes=routes) + + return render_template('endpoints.html', routes=routes) + @app.teardown_appcontext def closeConnection(exception): db.close_connection() diff --git a/templates/base.html b/templates/base.html index 8535f55..2e9e4b4 100644 --- a/templates/base.html +++ b/templates/base.html @@ -108,26 +108,11 @@ -
- - - - - Settings - -
+ on htmx:afterRequest go to the top of the body"> SQL Explorer + + + + + + + Endpoints + + + + + + Settings +
diff --git a/templates/endpoints.html b/templates/endpoints.html new file mode 100644 index 0000000..0be769c --- /dev/null +++ b/templates/endpoints.html @@ -0,0 +1,23 @@ +{% extends 'base.html' %} + +{% block content %} +
+ +
+

API Endpoints Overview

+

{{ routes|length }} Routes Available

+
+ + +
+ +
+ +
+ {{ render_partial('partials/endpoints_table.html', routes=routes) }} +
+
+{% endblock %} \ No newline at end of file diff --git a/templates/partials/endpoints_table.html b/templates/partials/endpoints_table.html new file mode 100644 index 0000000..53b2333 --- /dev/null +++ b/templates/partials/endpoints_table.html @@ -0,0 +1,94 @@ +
+ +
+ + + + + +
+ + + + + + + + + + + + + + {% for route in routes %} + + + + + + + + {% endfor %} + +
EndpointURLMethodsView Function + Description +
{{ route.endpoint }}{{ route.url }}{{ route.methods }}{{ route.view_func }} + {% if route.doc %} +
+ + {{ route.doc.split('\n')[0] }}{% if route.doc.count('\n') > 0 %}...{% endif %} + +
+ {{ route.doc }} +
+
+ {% else %} + N/A + {% endif %} +
+
\ No newline at end of file