Skip to content
Myr

Added routes command, which shows all the endpoints registered for the app.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Orderable by rules, endpoints and methods. Shows up in the builtin command list.

authored by Kiss György · committed by Kiss György · parent 0e46070 · Browse files · 1 file+28 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+28 added−0 removed

  • other:+28 added−0 removed

1 file+28 added−0 removed

modified:flask/cli.py+28 added
302 unmodified lines
303304 if add_default_commands:305 self.add_command(run_command)306 self.add_command(shell_command)307 self.add_command(routes_command)308309 self._loaded_plugin_commands = False310311 def _load_plugin_commands(self):
149 unmodified lines
461462 code.interact(banner=banner, local=ctx)463464465@click.command('routes', short_help='Show routes for the app.')466@click.option('-r', 'order_by', flag_value='rule', default=True, help='Order by route')467@click.option('-e', 'order_by', flag_value='endpoint', help='Order by endpoint')468@click.option('-m', 'order_by', flag_value='methods', help='Order by methods')469@with_appcontext470def routes_command(order_by):471 """Show all routes with endpoints and methods."""472 from flask.globals import _app_ctx_stack473 app = _app_ctx_stack.top.app474475 order_key = lambda rule: getattr(rule, order_by)476 sorted_rules = sorted(app.url_map.iter_rules(), key=order_key)477478 max_rule = max(len(rule.rule) for rule in sorted_rules)479 max_ep = max(len(rule.endpoint) for rule in sorted_rules)480 max_meth = max(len(', '.join(rule.methods)) for rule in sorted_rules)481482 columnformat = '{:<%s} {:<%s} {:<%s}' % (max_rule, max_ep, max_meth)483 click.echo(columnformat.format('Route', 'Endpoint', 'Methods'))484 under_count = max_rule + max_ep + max_meth + 4485 click.echo('-' * under_count)486487 for rule in sorted_rules:488 methods = ', '.join(rule.methods)489 click.echo(columnformat.format(rule.rule, rule.endpoint, methods))490491492cli = FlaskGroup(help="""\493This shell command acts as general utility script for Flask applications.494495It loads the application configured (either through the FLASK_APP environment
39 unmodified lines