Table of contents
1.
Introduction🌸
2.
Favicon🧑‍💻
3.
Favicon in CherryPy🍒
4.
Changing an Application’s Favicon in CherryPy🍒
5.
Frequently Asked Questions
5.1.
What is the purpose of favicons?
5.2.
What exactly does CherryPy expose?
5.3.
Is CherryPy free and open source?
5.4.
Is CherryPy a web server?
5.5.
What exactly is the CherryPy framework?
6.
Conclusion
Last Updated: Mar 27, 2024

Favicon and cherrypy

Author Shiva
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction🌸

CherryPy is a Python web framework that provides a user-friendly interface to the HTTP protocol for Python developers. It's also known as a web application library.

cherrypy image

CherryPy models and binds the HTTP protocol into an API using Python's strengths as a dynamic language. It is one of the oldest Python web frameworks, with a clean interface and a dependable platform.

In late June 2002, Remi Delon released the first version of CherryPy. This was the foundation for a successful Python web library. Remi is a French hacker who believes Python is one of the best options for web application development.

In this article, we will cover everything you need to know about Favicon and cherrypy.

Favicon🧑‍💻

A favicon is a small icon file that is associated with a specific website or web page. It is also known as a website icon, a tab icon, a URL icon, or a bookmark icon. Examine Coding Ninja's favicon:

coding ninjas favicon

A web designer can create such an icon and then upload it to a website (or web page) in a variety of ways, and it will be used by graphical web browsers. In browsers that support favicons, the favicon of a page is typically displayed in the address bar (sometimes also in the history) and next to the page's name in a list of bookmarks. The favicon of a page is typically displayed next to the title of the page on the tab in browsers that support a tabbed document interface, and site-specific browsers use the favicon as a desktop icon.

Favicon in CherryPy🍒

A "favicon" is a browser icon that represents tabs and bookmarks. This helps to distinguish your website and give it a distinct identity.

favicons image

Using the static file tool, CherryPy serves its own sweet red cherrypy as the default favicon. You can use the following code to serve your own favicon:
 

import favicon
import urllib.request

url = “http://www.stackoverflow.com”
icons = favicon.get(url)
icon = icons[0]
print(icon.url)

urllib.request.urlretrieve(icon.url, “stackOverflowIcon.png”)

 

Output:

Changing an Application’s Favicon in CherryPy🍒

There are several methods for supplying configuration information; we'll look at how to supply configuration information from a Python dictionary in your code.

Here's my config dictionary:

config = {    
	'/': {        
		'log.error_file': os.path.join(os.path.dirname(__file__), 'site.log'),        '
		environment': 'production',    
	},    
	'/favicon.ico': {        
		'tools.staticfile.on': True,        
		'tools.staticfile.filename': '/path/to/favicon.ico',    
	}
}


There are two sub-dictionaries in the dictionary: one for '/' and one for '/favicon.ico'.

'/' is used for global application configuration. In this case, we're deciding where the error log should go and what kind of environment to use.

If no favicon.ico is specified, CherryPy will use the default. The second part of the dictionary ('/favicon.ico') overrides the default and specifies the location of a different file to use

My previous post about configuring CherryPy to run with Apache defined a start() method that was responsible for starting the CherryPy engine when called from apache.

change image

To use this new configuration, we'll modify the start() method slightly as follows:

def start():    
	cherrypy.tree.mount(root, '/', config=config)    
	cherrypy.engine.start()


Previously, we used cherrypy.config.update to pass the '/' configuration parameters ().

We now pass the entire configuration dictionary along. This approach is useful because we can store application configuration information (that CherryPy may or may not care about) and then retrieve values from it using the cherrypy.request.app.config dictionary.

Configuring an area for serving static content is another addition to this approach (CSS, javascript, images, etc). This is easily accomplished by adding the following item to the configuration dictionary:

'/static': {        
	'tools.staticdir.on': True,        
	'tools.staticdir.dir': os.path.join(os.path.dirname(__file__), 'static')    
},


This adds the path /static (which searches for the directory static in the same directory as the app) and makes it a static dir. Anything with the url /static will now point to files in the static directory.

For example, rather than looking for a CherryPy route, this will now link to the static file global.css.

<link rel="stylesheet" type="text/css" href="/static/css/global.css" media="screen"><link>

Frequently Asked Questions

What is the purpose of favicons?

A favicon is a graphic image (icon) that is associated with a specific Web page or Web site. Many modern user agents (such as graphical browsers and newsreaders) display them in the address bar or in tabs as a visual reminder of the Web site's identity.

What exactly does CherryPy expose?

In a nutshell, once CherryPy has been discovered and is called an exposed method, then it is up to you, the person who writes it, the developer, to provide the tools needed to implement the logic of your application. CherryPy believes that you, the developer, are the most knowledgeable.

Is CherryPy free and open source?

CherryPy is an open-source project, so contributions are welcome. If you're interested, you can fork CherryPy on GitHub and submit a pull request with your changes.

Is CherryPy a web server?

In Brief: CherryPy WSGI Web Server. It is a modular component that can serve any Python WSGI web application.

What exactly is the CherryPy framework?

CherryPy is an object-oriented web application framework that is written in Python. It is intended for the rapid development of web applications by wrapping the HTTP protocol, but it remains at a low level and does not provide much more than RFC 7231 defines. 

Conclusion

This article covers everything you need to know about Favicon and cherrypy. Here are more articles for rescue.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Live masterclass