0

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"
        }
    })
5
  • What exactly do you mean by "something isn't working"? I guess you get 404 while serving css, am i right? Commented Sep 15, 2013 at 20:52
  • Sorry, I wasn't very specific. I get no error. Its just that none of my formatting contained in the my_css.css file is applied to the website. Commented Sep 15, 2013 at 20:58
  • Can you access css file directly? Commented Sep 15, 2013 at 21:12
  • as in use the absolute path to it? Commented Sep 15, 2013 at 21:21
  • I was thinking more about: your-host/css/your-css-file.css Commented Sep 15, 2013 at 21:25

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks for a complete solution. It works well still in 2021 !
1

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" />

2 Comments

sorry, not working still. I even fixed the mistake. it says joey_css when it should be my_css
sorry, nothing yet :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.