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?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
You can view all the Routes via flask shell by running the following commands after exporting or setting FLASK_APP environment variable.
flask shell app.url_map
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.
The same thing using a list comprehension:
Sample output:
I make a helper method on my
manage.py
:It solves the the missing argument by building a dummy set of options. The output looks like:
Then to run it:
python manage.py list_routes
For more on manage.py checkout: http://flask-script.readthedocs.org/en/latest/