Get a list of all routes defined in the Flask application

I have a sophisticated flak-based web application. There are many separate files with viewing functions. Their URLs are determined using the @app.route('/...') decorator. Is there a way to get a list of all the routes that were advertised in my application? Perhaps there is some method that I can call for the app object?

+114
python flask
Nov 09
source share
9 answers

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

 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) @app.route("/site-map") 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 Show links to newly created web pages for more information.

+123
Nov 10
source share
β€” -

I just met the same question. This decision is too complicated. Just open a new shell under your project:

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

The first "application" is my script project: app.py, the other is my name on the network.

(this is a solution for a tin network with a short route)

+56
Aug 09 '16 at 7:11
source share

I am doing a helper method on my manage.py :

 @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.unquote("{:50s} {:20s} {}".format(rule.endpoint, methods, url)) output.append(line) for line in sorted(output): print line 

It solves the missing argument by creating a dummy set of parameters. The result looks like this:

 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 run it:

python manage.py list_routes

More about control on manage.py: http://flask-script.readthedocs.org/en/latest/

+52
01 Oct '13 at
source share

Like Jonathan, I decided to do it instead. I see no reason to use url_for as it will break if your arguments are not strings, for example. Float

 @manager.command 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)) output.append(line) for line in sorted(output): print(line) 
+39
Mar 26 '14 at 4:05
source share

Apparently, starting with version 0.11, Flask has a built-in command line interface . One of the built-in commands lists the routes:

 FLASK_APP='my_project.app' flask routes 
+14
Dec 15 '18 at 20:34
source share

Since you did not specify that it should be run on the command line, in json you can easily return the following information for a control panel or other interface without a command line. The result and the result really should not be mixed with the design perspective. This is a poor program design, even if it is a tiny program. The result below could be used in a web application, command line or anything else that absorbs json.

You also did not specify that you need to know the python function associated with each route, so this answers your original question more precisely.

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

Rule repr () converts the required arguments to a route.

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

Same thing using list comprehension:

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

Output Example:

 { "routes": [ "/endpoint1", "/nested/service/endpoint2", "/favicon.ico", "/static/<path:filename>" ] } 
+2
Apr 25 '18 at 15:32
source share

If you need to access view functions, use app.view_functions instead of app.view_functions .

Example script:

 from flask import Flask app = Flask(__name__) @app.route('/foo/bar') def route1(): pass @app.route('/qux/baz') def route2(): pass for name, func in app.view_functions.items(): print(name) print(func) print() 

The result of running the script above:

 static <bound method _PackageBoundObject.send_static_file of <Flask '__main__'>> route1 <function route1 at 0x128f1b9d8> route2 <function route2 at 0x128f1ba60> 

(Note the inclusion of a β€œstatic” route, which is automatically created by Flask.)

+1
Dec 01 '18 at 15:56
source share

You can view all routes through the jar shell by running the following commands after exporting or setting the FLASK_APP environment variable. flask shell app.url_map

0
Jul 19 '19 at 8:25
source share

Flask has a cool snippet, there is one for:

http://flask.pocoo.org/snippets/117/

 @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.unquote("{:50s} {:20s} {}".format(rule.endpoint, methods, url)) output.append(line) for line in sorted(output): print line 
-one
Sep 18 '18 at 17:04
source share



All Articles