Bottle framework and OOP, using method instead of function


I’ve done some coding with Bottle. It’s really simple and fits my needs. However, I got stick when I tried to wrap the application into a class :

import bottle
app = bottle

class App():
    def __init__(self,param):
        self.param   = param

    # Doesn't work
    def index1(self):
        return("I'm 1 | self.param = %s" % self.param)

    # Doesn't work
    def index2(self):
        return("I'm 2")

    # Works fine
    def index3():
        return("I'm 3")

Is it possible to use methods instead of functions in Bottle?

Asked By: user1129665



Your code does not work because you are trying to route to non-bound methods. Non-bound methods do not have a reference to self, how could they, if instance of App has not been created?

If you want to route to class methods, you first have to initialize your class and then bottle.route() to methods on that object like so:

import bottle        

class App(object):
    def __init__(self,param):
        self.param   = param

    def index1(self):
        return("I'm 1 | self.param = %s" % self.param)

myapp = App(param='some param')

If you want to stick routes definitions near the handlers, you can do something like this:

def routeapp(obj):
    for kw in dir(app):
        attr = getattr(app, kw)
        if hasattr(attr, 'route'):

class App(object):
    def __init__(self, config):
        self.config = config

    def index(self):
    index.route = '/index/'

app = App({'config':1})

Don’t do the bottle.route() part in App.__init__(), because you won’t be able to create two instances of App class.

If you like the syntax of decorators more than setting attribute index.route=, you can write a simple decorator:

def methodroute(route):
    def decorator(f):
        f.route = route
        return f
    return decorator

class App(object):
    def index(self):
Answered By: Ski

Below works nicely for me 🙂
Quite object orientated and easy to follow.

from bottle import Bottle, template

class Server:
    def __init__(self, host, port):
        self._host = host
        self._port = port
        self._app = Bottle()

    def _route(self):
        self._app.route('/', method="GET", callback=self._index)
        self._app.route('/hello/<name>', callback=self._hello)

    def start(self):, port=self._port)

    def _index(self):
        return 'Welcome'

    def _hello(self, name="Guest"):
        return template('Hello {{name}}, how are you?', name=name)

server = Server(host='localhost', port=8090)
Answered By: Matt H

I took @Skirmantas answer and modified it a bit to allow for keyword arguments in the decorator, like method, skip, etc:

def routemethod(route, **kwargs):
    def decorator(f):
        f.route = route
        for arg in kwargs:
            setattr(f, arg, kwargs[arg])
        return f
    return decorator

def routeapp(obj):
    for kw in dir(obj):
        attr = getattr(obj, kw)
        if hasattr(attr, "route"):
            if hasattr(attr, "method"):
                method = getattr(attr, "method")
                method = "GET"
            if hasattr(attr, "callback"):
                callback = getattr(attr, "callback")
                callback = None
            if hasattr(attr, "name"):
                name = getattr(attr, "name")
                name = None
            if hasattr(attr, "apply"):
                aply = getattr(attr, "apply")
                aply = None
            if hasattr(attr, "skip"):
                skip = getattr(attr, "skip")
                skip = None

            bottle.route(attr.route, method, callback, name, aply, skip)(attr)
Answered By: Brendan Howell

try this, worked for me, documentation is also pretty decent to get started with …
Answered By: Adeel

You have to extend the Bottle class. It’s instances are WSGI web applications.

from bottle import Bottle

class MyApp(Bottle):
    def __init__(self, name):
        super(MyApp, self).__init__() = name
        self.route('/', callback=self.index)

    def index(self):
        return "Hello, my name is " +

app = MyApp('OOBottle')'localhost', port=8080)

What most examples out there are doing, including the answers previously provided to this question, are all reusing the “default app”, not creating their own, and not using the convenience of object orientation and inheritance.

Answered By: jpcgt

I know this question is quite old, but it is useful.

I am currently refactoring a Bottle application code into class. I am thinking about using something like:

import bottle

class App:
    def __init__(self, port=80): = bottle.Bottle()
        self.port = port

    def setup_routes(self):"/foo")
        def foo():
        def bar():

    def start(self):

a = App()

Does it work for your applications too? Feel free to comment, I am interested about this.

Notes from the creator of Bottle:

Answered By: Basj
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.