Search code examples
configurationstaticcherrypy

How to set a static file on a cherrypy configuration file?


I have a web application on CherryPy. I'm trying to configure a css file to use over all application but I can't. This is my conf file:

[global]
server.socket_host = "127.0.0.1"
server.socket_port = 8090
server.thread_pool = 10
server.logToScreen = 0

[/main.css]
tools.staticfile.on = True
tools.staticfile.filename = "E:\apyb\main.css"

If I set config on code instead on a file it works fine:

conf = {
    '/main.css': {
    'tools.staticfile.on': True,
    'tools.staticfile.filename': os.path.join(os.path.dirname(__file__), 'main.css'),
    }
}

How can I set up the path file?

I'm using Cherrypy 3.1.2 over Windows 7.


Solution

  • CherryPy configuration files use Python syntax for values. So just like in Python, when you enter a string with backslashes, it may interpret them as control characters:

    >>> "E:\apyb\main.css"
    'E:\x07pyb\\main.css'
    >>> print "E:\apyb\main.css"
    E:pyb\main.css
    

    The solution is to double the slashes:

    >>> "E:\\apyb\\main.css"
    'E:\\apyb\\main.css'
    >>> print "E:\\apyb\\main.css"
    E:\apyb\main.css
    

    Do the same in your config file:

    [/main.css]
    tools.staticfile.on = True
    tools.staticfile.filename = "E:\\apyb\\main.css"