Created
November 22, 2018 15:21
-
-
Save arianmaykon/135ec30189e95613e77b62defe6ab147 to your computer and use it in GitHub Desktop.
Flask CLI method to list routes
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
# Based on http://flask.pocoo.org/snippets/117/ | |
@manager.command | |
def list_routes(): | |
import urllib | |
from flask import url_for | |
output = [] | |
for rule in manager.app.url_map.iter_rules(): | |
print(rule.endpoint) | |
# print(url_for(rule.endpoint)) | |
print(rule.arguments) | |
print('-' * 80) | |
options = {} | |
for arg in rule.arguments: | |
# options[arg] = "[{0}]".format(arg) | |
options[arg] = 666 | |
methods = ','.join(rule.methods) | |
try: | |
url = url_for(rule.endpoint, **options) | |
except ValueError as e: | |
print('>>>') | |
print(options) | |
print('>>>') | |
break | |
line = urllib.unquote("{:50s} {:20s} {}".format(rule.endpoint, methods, url)) | |
output.append(line) | |
for line in sorted(output): | |
print line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment