natsuki_2002
natsuki_2002

Reputation: 25349

python cherrypy: serve css file

I'm using webfaction as a webhost. I'm trying to serve my cherrypy application a css file but something isn't working. I've got my python application in

home/webapps/spotipy

and my css file in

home/webapps/spotipy/css

At the top of my python code I've got

#!/usr/local/bin/python3.2
import cherrypy

class Root(object):
    @cherrypy.expose
    def index(self):

        return '''<html> 
                  <head>
                    <title>Spoti.py</title>
                    <link rel="stylesheet" href="css/my_css.css" type="text/css" />
                  </head>
                  <p> hi </p>

                  <body>
                  <p> hi joey </p>
        %s
        </body></html>''' %text

And this at the bottom

cherrypy.quickstart(Root(),config={

        '/css':
        { 'tools.staticdir.on':True,
          'tools.staticdir.dir':"home/webapps/spotipy/css"
        },
        '/my_css.css':
        { 'tools.staticfile.on':True,
          'tools.staticfile.filename':"home/webapps/spotipy/css/my_css.css"
        }
    })

Upvotes: 0

Views: 2923

Answers (2)

Ozan
Ozan

Reputation: 1074

This is a complete working example below in addition to zero323 answer. Change the shebang and run it in your /home/webapps/spotipy directory. If it does not work, there may be browser cache issue so refresh your page with Ctrl+F5. You can check if the css file is loaded correctly by pressing Ctrl+U to see the page source and click to see the pointing css links. If everything seems normal and still your css file does not apply on your page, it may be a css issue.

#!/usr/bin/python
import os
import cherrypy

class Root(object):
    @cherrypy.expose
    def index(self):
        text="dummy text"
        return '''<html> 
                  <head>
                    <title>Spoti.py</title>
                    <link rel="stylesheet" href="/css/my_css.css" type="text/css" />
                    <link rel="stylesheet" href="/joey_css.css" type="text/css" />
                  </head>
                  <p> hi </p>

                  <body>
                  <p> hi joey </p>
        %s
        </body></html>''' %text

conf={"/css": {"tools.staticdir.on": True,
               "tools.staticdir.dir": os.path.abspath("./css"),},
       '/joey_css.css':
                    { 'tools.staticfile.on':True,
                      'tools.staticfile.filename': os.path.abspath("./css/my_css.css"),
                    }
               }

cherrypy.quickstart(Root(),config=conf)

Upvotes: 2

zero323
zero323

Reputation: 330073

Try using absolute paths instead of relative. I suppose you are messing things up by trying to access home/webapps/spotipy/css. Try this in config:

cherrypy.quickstart(Root(),config={

        '/css':
        { 'tools.staticdir.on':True,
          'tools.staticdir.dir': "/home/webapps/spotipy/css"
        },

        '/joey_css.css':
        { 'tools.staticfile.on':True,
          'tools.staticfile.filename': "/home/webapps/spotipy/css/my_css.css"
        }
    })

and this in html:

<link rel="stylesheet" href="/css/my_css.css" type="text/css" />

Upvotes: 1

Related Questions