1

I'm using CherryPy with Python, and I'm quite new to it all. Eventually I will be using JQuery too, so following an example online, I have the below, which is calling the html correctly.

My issue is, that although it's calling my html, I am not sure what to do with my CSS and images that I have with it. I've tried calling them as normal from the html, but I have not had any luck.

def index(self):
    return file('index.html')

 if __name__ == '__main__':
 conf = {
     '/': {
         'tools.sessions.on': True,
         'tools.staticdir.root': os.path.abspath(os.getcwd())
     },
     '/generator': {
         'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
         'tools.response_headers.on': True,
         'tools.response_headers.headers': [('Content-Type', 'text/plain')],
     },
     '/static': {
         'tools.staticdir.on': True,
         'tools.staticdir.dir': './public'
     }
 }

webapp = StringGenerator()
webapp.generator = StringGeneratorWebService()
cherrypy.quickstart(webapp, '/', conf)

within my html I have the normal <link href="/static/css/style2.css" rel="stylesheet"> but it isn't working.

My file structure is - with public and css being a folder:

fitbit.py fitbit.html - public - css style2.css

So it's calling the fitbit.html correctly, but within the html, I cannot get the css and image. Any help as to how to call it would be greatly appreciated.

2 Answers 2

3

The url of the resource is relative to the url you've requested, not on the actual path of the files. So the css href should be href="media/fitbit.css"

Edit (some code):

Application code (app.py) is

import cherrypy
from os.path import abspath

def app():
    return file("media/index.html")
app.exposed = True

CP_CONF = {
        '/media': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': abspath('./media') # staticdir needs an absolute path
            }
        }

if __name__ == '__main__':
    cherrypy.config.update({'server.socket_port': 8082})
    cherrypy.quickstart(app, '/', CP_CONF)

Index.html is

<html>
<head>
<link rel="stylesheet" type="text/css" href="media/style.css">
</head>

<body>
This is the body
</body>

</html>

Directory structure is

Project Dir
    app.py
    /media
         index.html
         style.css

The main points are

  1. Staticdir needs an absolute path and you have to be careful with the mappings. To simplify things, here, static url root is the same with the directory name

  2. Static assets urls are relative to the root url e.g. media/style.css

Sign up to request clarification or add additional context in comments.

3 Comments

I've tried loads of different file paths, and none are working. Edited question to a different version with the same problem. Worst thing about this one is it's an example from the CherryPyDocs
Still exactly the same
With the code above ? It can't be, you are doing something wrong.
0

You guys don't have to think about directory structure btw

Like

├── index.html
├── about-page.html
├── app.py
├── assets
│   ├── css
│   │   ├── animate.css
│   │   ├── bootstrap.css
│   │   ├── color1.css
│   │   ├── color10.css
│   │   ├── color11.css
│   │   ├── color12.css

It works $ Python3 /Users/me/Desktop/html-template/app.py

import cherrypy
from os.path import abspath

def app():
    return open("index.html")
app.exposed = True

CP_CONF = {
        '/': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': abspath('./') # staticdir needs an absolute path
            }
        }

if __name__ == '__main__':
    cherrypy.config.update({'server.socket_port': 8082})
    cherrypy.quickstart(app, '/', CP_CONF)

Comments

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.