Created
December 14, 2017 11:27
-
-
Save markph0204/a7de1d4d9641247e903e8a14842c1fd5 to your computer and use it in GitHub Desktop.
Flask list routes
This file contains hidden or 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
from collections import defaultdict | |
@app.cli.command("list_routes") | |
def list_routes(): | |
""" | |
Roll through Flask's URL rules and print them out | |
Thank you to Jonathan Tushman | |
And Thank you to Roger Pence | |
Sourced http://flask.pocoo.org/snippets/117/ "Helper to list routes (like Rail's rake routes)" | |
Note that a lot has possibly changed since that snippet and rule classes have a __str__ | |
which greatly simplifies all of this | |
""" | |
format_str = lambda *x: "{:30s} {:40s} {}".format(*x) # pylint: disable=W0108 | |
clean_map = defaultdict(list) | |
for rule in app.url_map.iter_rules(): | |
methods = ",".join(rule.methods) | |
clean_map[rule.endpoint].append((methods, str(rule),)) | |
print(format_str("View handler", "HTTP METHODS", "URL RULE")) | |
print("-" * 80) | |
for endpoint in sorted(clean_map.keys()): | |
for rule, methods in sorted(clean_map[endpoint], key=lambda x: x[1]): | |
print(format_str(endpoint, methods, rule)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment