2

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.

Peter
  • 57
  • 1
  • 8
  • Have you tried downloading `/favicon.ico` with `wget` or similar? Favicons are cached differently from all the rest in some browsers, clearing the cache or anonymous mode might not be enough to see the new icon. – Ulrich Schwarz Mar 12 '15 at 18:37

2 Answers2

3

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.

Community
  • 1
  • 1
saaj
  • 23,253
  • 3
  • 104
  • 105
1

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

Isa Hassen
  • 300
  • 1
  • 9
  • Unable to view the page. It says, Permission denied. no permission to view this page. – prashanth manohar Mar 19 '17 at 00:32
  • Thanks. It works for me: `from cherrypy.lib.static import serve_file; def favicon_ico(self): return serve_file('/path/to/favicon.ico'); favicon_ico.exposed = True` – mtoloo Aug 19 '17 at 04:39