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?

11/30/2018 1:52:29 PM

Accepted Answer

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.

5/23/2017 11:47:31 AM

I just met the same question. Those solution above is 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 tinny web with a little route)

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow