Created
February 8, 2015 00:39
-
-
Save kissgyorgy/b43d3e9db66440582075 to your computer and use it in GitHub Desktop.
Python: Show flask routes nicely aligned with rules, endpoints and methods
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
@main.command() | |
def routes(): | |
from mainweb import app | |
sorted_rules = sorted(app.url_map.iter_rules(), key=lambda x: x.rule) | |
max_rule = max(len(rule.rule) for rule in sorted_rules) | |
max_ep = max(len(rule.endpoint) for rule in sorted_rules) | |
max_meth = max(len(', '.join(rule.methods)) for rule in sorted_rules) | |
columnformat = '{:<%s} {:<%s} {:<%s}' % (max_rule, max_ep, max_meth) | |
click.echo(columnformat.format('Route', 'Endpoint', 'Methods')) | |
under_count = max_rule + max_ep + max_meth + 4 | |
click.echo('-' * under_count) | |
for rule in sorted_rules: | |
methods = ', '.join(rule.methods) | |
click.echo(columnformat.format(rule.rule, rule.endpoint, methods)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment