Output BottlePy Routes

Tuesday, March 27th, 2012 | Programming, Web Application, Work

To quickly get a list of routes in a Bottle wsgi application you will first need to add this method to the Bottle class in bottle.py:

    def get_routes(self):
        """ Returns all the routes """
        return self.routes

This merely exposes the route objects.

In my main.py I’ve split up the project into several Bottle objects in different module files and mounted them to the default application.

import file_requests
mount('/file', file_requests.app)

Now for the actual output function:

def display_routes(prefix, app):
    for route in app.get_routes():
        print route.method + " " + prefix + route.rule

Then all we need to do is call that function using the correct prefix and application (ie what was used for mount):

# if any routes are in the default_app
display_routes("", default_app())
# display file request routes
display_routes("/file", file_requests.app)

It will output something like this:

ANY /file/:#.*#
ANY /file
POST /file/
GET /file/<file_id>
POST /file/<file_id>

Tags: ,