Wouter Lefebvre
Wouter Lefebvre

Reputation: 35

View route structure of Flask app

Is there a way to see the routes each page makes in the application. Like sort of a sitemap to see if there are unnecessary pages who do nothing or got useless because of the growth of the application. Or another way to see the structure of my site?

         -----------------------
        |                       |
login--home--about              |
      |                         |
      -- contact--contact form--|

Upvotes: 0

Views: 602

Answers (2)

Piotrek
Piotrek

Reputation: 1530

It doesn't do precisely what you want, but maybe that's a starting point. This Flask snippet lists the app's routes. http://flask.pocoo.org/snippets/117/

from flask_script import Manager
from myapp import app
from flask import url_for
manager = Manager(app)

@manager.command
def list_routes():
    import urllib
    output = []
    for rule in app.url_map.iter_rules():

        options = {}
        for arg in rule.arguments:
            options[arg] = "[{0}]".format(arg)

        methods = ','.join(rule.methods)
        url = url_for(rule.endpoint, **options)
        line = urllib.parse.unquote("{:50s} {:20s} {}".format(rule.endpoint, methods, url))
        output.append(line)

    for line in sorted(output):
        print line

if __name__ == "__main__":
    manager.run()

Upvotes: 1

davidism
davidism

Reputation: 127190

Flask comes with the routes command to list the routes registered with the application.

$ export FLASK_APP=my_app
$ flask routes
    Endpoint                             Methods    Rule
-----------------------------------  ---------  ----------------------------------------------
auth.login                           GET, POST  /auth/login
auth.logout                          GET        /auth/logout
auth.register                        GET, POST  /auth/register
index                                GET        /

Upvotes: 1

Related Questions