Get list of all routes defined in the Flask app


I have a complex Flask-based web app. There are lots of separate files with view functions. Their URLs are defined with the @app.route('/...') decorator. Is there a way to get a list of all the routes that have been declared throughout my app? Perhaps there is some method I can call on the app object?

Asked By: J-bob



All the routes for an application are stored on app.url_map which is an instance of werkzeug.routing.Map. You can iterate over the Rule instances by using the iter_rules method:

from flask import Flask, url_for

app = Flask(__name__)

def has_no_empty_params(rule):
    defaults = rule.defaults if rule.defaults is not None else ()
    arguments = rule.arguments if rule.arguments is not None else ()
    return len(defaults) >= len(arguments)

def site_map():
    links = []
    for rule in app.url_map.iter_rules():
        # Filter out rules we can't navigate to in a browser
        # and rules that require parameters
        if "GET" in rule.methods and has_no_empty_params(rule):
            url = url_for(rule.endpoint, **(rule.defaults or {}))
            links.append((url, rule.endpoint))
    # links is now a list of url, endpoint tuples

See Display links to new webpages created for a bit more information.

Answered By: Sean Vieira

I make a helper method on my

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.unquote("{:50s} {:20s} {}".format(rule.endpoint, methods, url))

    for line in sorted(output):
        print line

It solves the the missing argument by building a dummy set of options. The output looks like:

CampaignView:edit              HEAD,OPTIONS,GET     /account/[account_id]/campaigns/[campaign_id]/edit
CampaignView:get               HEAD,OPTIONS,GET     /account/[account_id]/campaign/[campaign_id]
CampaignView:new               HEAD,OPTIONS,GET     /account/[account_id]/new

Then to run it:

python list_routes

For more on checkout:

Answered By: Jonathan

Similar to Jonathan’s answer I opted to do this instead. I don’t see the point of using url_for as it will break if your arguments are not string e.g. float

def list_routes():
    import urllib

    output = []
    for rule in app.url_map.iter_rules():
        methods = ','.join(rule.methods)
        line = urllib.unquote("{:50s} {:20s} {}".format(rule.endpoint, methods, rule))

    for line in sorted(output):
Answered By: John Jiang

I just met the same question. Those solutions above are too complex.
Just open a new shell under your project:

>>> from app import app
>>> app.url_map

The first ‘app‘ is my project script:,
another is my web’s name.

(this solution is for the tiny web with a little route)

Answered By: Vi.Ci

Since you did not specify that it has to be run command-line, the following could easily be returned in json for a dashboard or other non-command-line interface. The result and the output really shouldn’t be commingled from a design perspective anyhow. It’s bad program design, even if it is a tiny program. The result below could then be used in a web application, command-line, or anything else that ingests json.

You also didn’t specify that you needed to know the python function associated with each route, so this more precisely answers your original question.

I use below to add the output to a monitoring dashboard myself. If you want the available route methods (GET, POST, PUT, etc.), you would need to combine it with other answers above.

Rule’s repr() takes care of converting the required arguments in the route.

def list_routes():
    routes = []

    for rule in app.url_map.iter_rules():
        routes.append('%s' % rule)

    return routes

The same thing using a list comprehension:

def list_routes():
    return ['%s' % rule for rule in app.url_map.iter_rules()]

Sample output:

  "routes": [
Answered By: postal

If you need to access the view functions themselves, then instead of app.url_map, use app.view_functions.

Example script:

from flask import Flask

app = Flask(__name__)

def route1():

def route2():

for name, func in app.view_functions.items():

Output from running the script above:

<bound method _PackageBoundObject.send_static_file of <Flask '__main__'>>

<function route1 at 0x128f1b9d8>

<function route2 at 0x128f1ba60>

(Note the inclusion of the “static” route, which is created automatically by Flask.)

Answered By: Mark Amery

Apparently, since version 0.11, Flask has a built-in CLI. One of the built-in commands lists the routes:

FLASK_APP='' flask routes
Answered By: theY4Kman

You can view all the Routes via flask shell by running the following commands after exporting or setting FLASK_APP environment variable.

flask shell

Answered By: Xerrex

inside your flask app do:

flask shell
>>> app.url_map
Map([<Rule '/' (OPTIONS, HEAD, GET) -> helloworld>,
 <Rule '/static/<filename>' (OPTIONS, HEAD, GET) -> static>])
Answered By: Nabaz Maaruf

Use cli command in Directory where your flask project is.

flask routes
Answered By: Ashu Sahu

That, is, if your Flask application name is ‘app’.

It’s an attribute of the instance of the Flask App.


Answered By: jouell
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.