user2174050
user2174050

Reputation: 65

cherrypy, serve css file, wrong path

I have an issue to load my css on a html page managed with CherryPy. This is my situation:

class HelloWorld(object):
   @cherrypy.expose
   def index(self):
     return "Hello world!"


   @cherrypy.expose
   def sc(self):
     Session = sessionmaker()
     session = Session(bind=engine)
   ...
   ...
if __name__ == '__main__':
cherrypy.quickstart(HelloWorld(),config={
'/':
{'tools.staticdir.root': True,
'tools.staticdir.root': "Users/mypc/Desktop/data"},
'/css':
{ 'tools.staticdir.on':True,'tools.staticdir.dir':"/css" }, 
'/style.css':
{ 'tools.staticfile.on':True,
'tools.staticfile.filename':"/style.css"}
})

When I launch my script there is wrtten:

CherryPy Checker:
dir is an absolute path, even though a root is provided.
'/css' (root + dir) is not an existing filesystem path.
section: [/css]
root: 'Users/mypc/Desktop/data'
dir: '/css'

but root + dir is the right path (Users/mypc/Desktop/data/css) Where I'm wrong and why I cannot open my css by browser?

Thanks in advance

Upvotes: 0

Views: 1571

Answers (1)

saaj
saaj

Reputation: 25214

Here's the relevant documentation section. It says:

CherryPy always requires the absolute path to the files or directories it will serve. If you have several static sections to configure but located in the same root directory, you can use the following shortcut... tools.staticdir.root

In other works, when you provide tools.staticdir.root, all underlying tools.staticdir.dir entries must not be absolute, i.e. start with slash, which is what the CherryPy Checker is warning you about.

The following is enough. Just put you CSS files the directory.

#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os

import cherrypy


path   = os.path.abspath(os.path.dirname(__file__))
config = {
  'global' : {
    'server.socket_host' : '127.0.0.1',
    'server.socket_port' : 8080,
    'server.thread_pool' : 8
  },
  '/css' : {
    'tools.staticdir.on'  : True,
    'tools.staticdir.dir' : os.path.join(path, 'css')
  }
}


class App:

  @cherrypy.expose
  def index(self):
    return 'Hello world!'


if __name__ == '__main__':
  cherrypy.quickstart(App(), '/', config)

Upvotes: 1

Related Questions