Search code examples
pythoncherrypy

CherryPy: Turn off tool for one handler


I have a class with several routes and I'd like them to all use the json tools except for one. How can I exclude a specific route from a tool (foo in the example below)?

import cherrypy

class HelloWorld(object):
    _cp_config = {
        'tools.json_out.on': True,
        'tools.json_in.on': True,
        '/foo': {
           'tools.json_out.on': True,
           'tools.json_in.on': True
        }
    }
    @cherrypy.expose()
    def index(self):
        return "Hello World!"
    @cherrypy.expose()
    def foo(self):
        return "Hello World!"

cherrypy.quickstart(HelloWorld())

Solution

  • You can do that with the cherrypy.config decorator:

    import cherrypy
    
    class HelloWorld(object):
        _cp_config = {
            'tools.json_out.on': True,
            'tools.json_in.on': True
        }
    
        @cherrypy.expose
        def index(self):
            return "Hello World!"
    
        @cherrypy.expose
        @cherrypy.config(**{'tools.json_in.on': False, 'tools.json_out.on': False})
        def foo(self):
            return "Hello World!"
    
    cherrypy.quickstart(HelloWorld())