Reputation: 67
I'm having an issue changing the default cherrypy favicon to my own, called book.ico located in public/images/books.ico. I've tried disabling it already using the following code:
'/favicon.ico': {
'tools.staticfile.on': False,
}
But the icon still shows up as a tab label. I'm browsing to the site in incognito mode with chrome.
import cherrypy
import os
import glob
class HelloWorld(object):
favicon_ico = None
@cherrypy.expose
def index(self):
return file('public/html/index.html')
... skipping over def generate(self, name)
if __name__ == '__main__':
cherrypy.config.update({
'server.socket_host':
'192.168.2.9','server.socket_port':8080,
'/static': {
'tools.staticdir.on': True,
'tools.staticdir.dir': os.getcwd(),
},
'/public': {
'tools.staticdir.on': True,
'tools.staticdir.dir': os.path.join(os.getcwd(), "/public"),
},
'/favicon.ico': {
'tools.staticfile.on': True,
'tools.staticfile.filename': os.path.join(os.getcwd(), '/public/images/books.ico')
}
})
cherrypy.tree.mount(HelloWorld())
cherrypy.engine.start()
cherrypy.engine.block()
directory structure
.
├── app.conf
├── bin
│ ├── activate
│ ├── activate.csh
│ ├── activate.fish
│ ├── activate_this.py
│ ├── cherryd
│ ├── easy_install
│ ├── easy_install-2.7
│ ├── pip
│ ├── pip2
│ ├── pip2.7
│ ├── python
│ ├── python2 -> python
│ └── python2.7 -> python
├── books.ico
├── CherrypyProd.py
├── images
├── pip-selfcheck.json
├── public
│ ├── css
│ ├── html
│ │ ├── books.png
│ │ └── index.html
│ └── images
│ ├── books.ico
│ └── books.png
├── ssl
├── static
└── books.png
How can i replace the default favicon.ico with my own books.ico???
Thank you in advance for your help. Please let me know if I can clarify anything further.
Upvotes: 3
Views: 2271
Reputation: 300
Try a "favicon_ico" handler method in your root class, as suggested in older CherryPy docs here: https://cherrypy.readthedocs.org/en/3.2.6/progguide/files/favicon.html
Upvotes: 1
Reputation: 25244
It is somewhat known yet annoying favicon cache issue.
The W3C recommends using link tag instead of relying on /favicon.ico
since HTML 4.01. It allows you to avoid making exception route for favicon and use normal image formats like JPEG and PNG. It also allows firm cache invalidation be providing version in query string.
<link rel="icon" type="image/png" href="/static/myicon.png?v=1">
To ensure your configuration is fine and it's a browser cache issue make a checksum of the file and of the CherryPy response.
cat books.ico | md5sum
wget -qO- http://192.168.2.9:8080/favicon.ico | md5sum
A side suggestion, don't rely on os.getcwd
because it's too easy to forget about the assumption of setting current directory beforehand. Better set path = os.path.abspath(os.path.dirname(__file__))
and use it later on.
Upvotes: 3