BottlePy - How can I find the current route from within a hook?
bottle, python, python-3.x
Solution
The request has both a `bottle.route` and a `route.handle` entry, both contain the same value:
from bottle import request
print request['bottle.route']
This isn't documented; I had to find it in the `bottle.py` source. The value is a `Route` instance; it has both a `.name` and a `.rule` attribute you could inspect to determine which route was matched.
if request['bottle.route'].rule == '/':
# matched the `/` route.
For your specific example this is perhaps overkill, since you are only matching simple paths, but for more complex rules with regular expression rules this would work better than trying to match the `request.path` attribute (but it'd be a good idea to give your routes a `name` value).
Problem
I have the following hook in BottlePy: ``` @bottle_app.hook('before_request') def update_session(): # do stuff return ``` And some routes: ``` @bottle_app.route('/') def index(): return render('index') @bottle_app.route('/example') def example(): return render('example') ``` From within `update_session()`, how can I determine which route is being called? I have looked through the documentation to no avail, but surely this is possible?