Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Bottle route traversal
# Demonstration of inspecting all the routes, including those on sub-apps,
# from the default app instance.
# This should be run directly to print a list of route prefixes and the rules.
# Tested with Python 2.7 and Bottle-dev. Patch here
import bottle
subapp = bottle.Bottle()
def subhello():
return 'Sub Hello World'
def hello():
return 'Hello World'
subapp2 = bottle.Bottle()
def subhello2():
return 'Sub Hello World 2'
bottle.mount('/subapp/', subapp)
bottle.mount('/one/two/three/', subapp2)
def inspect_routes(app, prefix=''):
"""Yields mount prefix and route object for an app. Mount-points for sub-
apps are inspected recursively, yielding their routes too.
wildcard = r'/:#.*#'
for route in app.routes:
cb = route.callback
rule = route.rule
# Only follow a route with the wildcard. Some mounted apps will have
# a second route to re-direct the request URL with a trailing slash.
if isinstance(cb, bottle.MountPoint) and rule.endswith(wildcard):
prefix = prefix + rule[:rule.rfind(wildcard)]
for sprefix, sroute in inspect_routes(, prefix):
yield sprefix, sroute
yield prefix, route
for prefix, route in inspect_routes(bottle.default_app()):
print prefix, route.rule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.