Search code examples
pythontornado

How to replace or modify a Tornado Handler at runtime?


I am writing a Jupyter server extension, allowing me to write a tornado.web.RequestHandler class. I would like to modify one of the handlers that the application has been initialized with, specifically the one creating a default redirect:

(r'/?', web.RedirectHandler, {
    'url' : settings['default_url'],
    'permanent': False, # want 302, not 301
})

From the RequestHandler object I have access to the tornado.web.Application subclass used. Is there a public API to get the list of handlers that I could modify?

Specifically, I'm looking to change the 'url' parameter the tornado.web.RedirectHandler is created with. It doesn't look like there is a documented api for this, so I'm guessing I'd have to replace the handler entirely.


Solution

  • Tornado does not support changing handlers at runtime. Instead, make your own handler which does the desired redirect based on whatever criteria you want:

    class MyRedirectHandler(RequestHandler):
        def get(self):
            self.redirect(self.settings['default_url'], permanent=False)