Instead of using the route()
decorator, you should use the add_url_rule
method of the self.server
object, for example:
class MyClass: def __init__(self): self.server = Flask(__name__) self.server.add_url_rule('/', 'home', self.home) self.server.add_url_rule('/route-1', 'route-1', self.route_1) self.server.add_url_rule('/route-2', 'route-2', self.route_2) def home(): return '<h1>Success</h1>' def route_1(): ... def route_2(): ...
This template allows you to define route handlers as class methods, and it is much easier to read, since you can see all your URL rules in one block.
source share