Skip to content

Instantly share code, notes, and snippets.

@kissgyorgy
Created February 8, 2015 00:39
Show Gist options
  • Save kissgyorgy/b43d3e9db66440582075 to your computer and use it in GitHub Desktop.
Save kissgyorgy/b43d3e9db66440582075 to your computer and use it in GitHub Desktop.
Python: Show flask routes nicely aligned with rules, endpoints and methods
@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