Add page to list all flask endpoints with filter

This commit is contained in:
Peter Stockings
2024-11-09 19:07:55 +11:00
parent c7013e0eac
commit 120a94ff45
4 changed files with 188 additions and 16 deletions

43
app.py
View File

@@ -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()