Table of contents
1.
Introduction
2.
Plugins in Cherrypy 
2.1.
Create a new Plugins in Cherrypy
2.2.
Deploy a plugin 
2.3.
Turn off a plugin 
2.4.
What we get is this: 
3.
Advantages from Plugins 
4.
Disadvantages of plugins 
5.
Frequently Asked Questions
5.1.
What is the purpose of CherryPy?   
5.2.
What accomplishes CherryPy expose? 
5.3.
What Python framework I ought to employ? 
5.4.
How do you run a Python bottle?
5.5.
Which Python frontend is the best? 
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Plugins in Cherrypy

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

Introduction

Plugins in CherryPy's helper class automatically subscribes your start and stop functions to the relevant channels. These methods are called appropriately when the start and stop channels are published.

Intro

A plugins in Cherrypy, also known as an add-on or extension, is a piece of computer software that enables the addition of additional features without changing the host program itself. Plugins in Cherrypy, which are extensively used in digital music, video, and Web browsing, allow programmers to update a host software while keeping the user inside the program's context.

Plugins in Cherrypy 

Simply explained, Plugins in Cherrypy are objects that interact with the bus by publishing to or subscribing to channels—typically both simultaneously.

Plugins in Cherrypy

Whenever you need certain functionalities, Plugins in Cherrypy come in quite handy. 

  • Accessible throughout the entire application server 
  • Associated with the life cycle of the application 
  • Avoid becoming too tightly bound to the application.

Create a new Plugins in Cherrypy

Create a new Plugins in Cherrypy

A typical Plugins in Cherrypy appears as follows: 

import cherrypy
from cherrypy.process import wspbus, plugins

class DatabasePlugin(plugins.SimplePlugin):
    def __init__(self, bus, db_klass):
        plugins.SimplePlugin.__init__(self, bus)
        self.db = db_klass()

    def start(self):
        self.bus.log('Start up DB access')
        self.bus.subscribe("db-save", self.save_it)

    def stop(self):
        self.bus.log('Stop down DB access')
        self.bus.unsubscribe("db-save", self.save_it)

    def save_it(self, entity):
        self.db.save(entity)

 

CherryPy Process Plug-Ins CherryPy offers the utility class known as SimplePlugin, which will automatically subscribe your start and stop methods to the relevant channels. 

These methods are called appropriately when the start and stop channels are published. 

So, take note of how our plugin joins the db-save channel to receive messages from the bus.

Deploy a plugin 

The plugin must be registered to the bus as follows in order to be enabled: 

DatabasePlugin(cherrypy.engine, SQLiteDB).subscribe()


This instance of SQLiteDB is a fictitious class that serves as our database provider.

Turn off a plugin 

Also, you can do the following to deregister a plugin: 

someplugin.unsubscribe() 


This is frequently used when you wish to stop CherryPy from starting the default HTTP server, for instance if you run on top of another HTTP server (WSGI capable): 

cherrypy.server.unsubscribe() 


Let's use this default application for an example:

import cherrypy

class Root(object):
    @cherrypy.expose
    def index(self):
        return "Hi Plugin"

if __name__ == '__main__':
    cherrypy.quickstart(Root())


When using this program, for instance, you might see something like this:


[09/Aug/2022:06:24:13] ENGINE Listening for SIGHUP.
[09/Aug/2022:06:24:13] ENGINE Listening for SIGTERM.
[09/Aug/2022:06:24:13] ENGINE Listening for SIGUSR1.
[09/Aug/2022:06:24:13] ENGINE Bus STARTING
[09/Aug/2022:06:24:13] ENGINE Started monitor thread 'Autoreloader'.
[09/Aug/2022:06:24:13] ENGINE Serving on http://127.0.0.1:8080
[09/Aug/2022:06:24:13] ENGINE Bus STARTED


Let's now terminate the HTTP server: 

import cherrypy

class Root(object):
    @cherrypy.expose
    def index(self):
        return "Hi Plugin"

if __name__ == '__main__':
    cherrypy.server.

unsubscribe()
    cherrypy.quickstart(Root())


What we get is this: 

[09/Aug/2022:06:31:59] ENGINE Listening for SIGHUP.
[09/Aug/2022:06:31:59] ENGINE Listening for SIGTERM.
[09/Aug/2022:06:31:59] ENGINE Listening for SIGUSR1.
[09/Aug/2022:06:31:59] ENGINE Bus STARTING
[09/Aug/2022:06:31:59] ENGINE Started monitor thread 'Autoreloader'.
[09/Aug/2022:06:31:59] ENGINE Bus STARTED

The server is not launched, as you can see. The lacking 

[09/Aug/2022:06:35:25] ENGINE Serving on http://127.0.0.1:8080

Advantages from Plugins 

Advantages from Plugins
  • The fact that there is programming available to assist you in achieving various objectives for your website is one of the main benefits of plugins. Some plugins are made to assist with tasks like tracking your statistics, growing newsletter sign-ups, or collaborating with your email distribution system to guarantee that your consumers are satisfied. 
     
  • Additionally, plugins make integrating social media on your website simple. Your brand will grow as your consumers spread the word about you to their friends and family via the plugins. 
     
  • The plugins can link your customers to your social media sites. Utilizing this potential is crucial, and plugins make it simple. 

Disadvantages of plugins 

Disadvantages of plugins
  • Using plugins can leave your website susceptible to hacking or security difficulties, which is one of its main drawbacks. This typically occurs with older plugins that your site designer does not keep up to date. These safety concerns can sometimes be grave. 
     
  • If you take the time to update the plugins frequently, you can stay away from them. Additionally, since fixes can be released whenever vulnerabilities are discovered, you are less likely to have security problems if you choose a premium version over a free version. 
     
  • Too many plugins being installed could be another problem. This may result in a number of problems. For instance, plugins from various designers might not work well together. This may reduce their effectiveness or cause your site to load more slowly. 
     
  • To make your website work more smoothly, choose to employ plugins from a single developer. Additionally, if you experience lag after installing a plugin, it may be a sign that the software was poorly constructed; therefore, you might want to think about a different solution.

Frequently Asked Questions

What is the purpose of CherryPy?   

Python is a programming language used by the object-oriented web application framework known as CherryPy. It stays at a low level and does not provide much more than what is stated in RFC 7231, but it is meant to construct web applications by wrapping the HTTP protocol rapidly.

What accomplishes CherryPy expose? 

In other words, it is your responsibility as a developer to offer the tools necessary to implement the logic of your application after CherryPy has discovered and is called an exposed method. CherryPy believes that you, the developer, are the expert.

What Python framework I ought to employ? 

The full-stack framework is the kind. Web2Py might be the solution for Python developers looking for a scalable full-stack framework. The free Python framework offers a web-based integrated development environment (IDE) with a code editor, debugger, and one-click deployment.

How do you run a Python bottle?

Go to http://localhost:8080/hello/world in your browser after running this script or pasting it into a Python prompt. I'm done now. Use pip install bottle to install the most recent stable version, or download bottle.py (unstable) and place it in your project directory. Other than the Python standard library, there are no other hard requirements.

Which Python frontend is the best? 

Python + Frontend + Backend = Django 

The most used Python web development framework is Django. Since it is a full-stack framework, all necessary functions are provided by default rather than as separate libraries.

Conclusion

In this article, we have understood the importance of Plugins in Cherrypy by creating the plugin and Also looked into the advantages and disadvantages of Plugins in Cherrypy.

I suppose it is clear to you. Still, have doubt? Then please comment.

Are you eager to read more articles on Routes in Sinatra? Coding Ninjas cover you, so don't worry. View more topics on Coding ninjas.

Please refer to our guided pathways on Code studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses, and use the accessible sample exams and questions as a guide. For placement preparations, look at the interview experiences and interview package.

If you find any problems, please comment. We will love to answer your questions.

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

 

Thank you
Live masterclass