Related Articles

Related Articles

Perform addition and subtraction using CherryPy
  • Last Updated : 01 Oct, 2020

CherryPy also known as a web application library is a Python web framework that provides a friendly interface to the HTTP protocol for Python developers. It allows developers to build web applications the same way as in traditional object-oriented Python programs. Thereby, resulting in smaller source code developed in no time.

This framework is mainly for the developers who want to create a portable database-driven web application using Python, as it provides Create, Retrieve, Update and Delete functionalities.

The basic requirements for the installation of CherryPy include:

  • Python with version 2.4 or above
  • Cherrypy 3.0

Installation

To install cherrypy run the following command in terminal:

pip install cherrypy

Approach:



  1. Create user interface to take input from user.
  2. Write cherrypy program to perform required operations

HTML code to take input from the user-

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
<head>
   
  </head>
<body>
  
  <div class="container">  
    <h2><u><i>Operation</i></u></h2>
    <form action="store" id="form" method="GET">
    <input type="number" name="number1" /><br />
    <input type="number" name="number2" /><br />
  
  
    <input style="margin-left: 250px;" id=" submit" type="submit"/></div>
  </div>    
    </form>
  </div>
              
</body>
</html>

chevron_right


Cherrypy code for addition

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import cherrypy
  
  
class Root(object):
     
    @cherrypy.expose
    def index(self):
        return """<html>
<head>
   
  </head>
<body>
  
  <div class="container">  
    <h2><u><i>Addition</i></u></h2>
    <form action="store" id="form" method="GET">
    <input type="number" name="number1" /><br />
    <input type="number" name="number2" /><br />
  
  
    <input style="margin-left: 250px;" id=" submit" type="submit"/></div>
  </div>    
    </form>
  </div>
              
</body>
</html>"""
     
      
    @cherrypy.expose
    def store(self, number1, number2):
        num1=int(number1)
        num2=int(number2)
        answer=num1+num2
        
        out= """<html>
        <body>
          
          
<p> Sum: %s</p>
  
         
          
        <a style="color:red; font-size:35px;" id="shutdown"; href="./shutdown"><i>Shutdown Server</i></a>
        </body>
        </html>
          
        """
         
        return out % (str(answer))
    
      
    @cherrypy.expose
    def shutdown(self):
        cherrypy.engine.exit()
  
if __name__=="__main__":
    cherrypy.config.update({'server.socket_port': 8087})
      
    cherrypy.quickstart(Root())

chevron_right


Output:

Code for subtraction

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import cherrypy
  
  
class Root(object):
     
    @cherrypy.expose
    def index(self):
        return """<html>
<head>
   
  </head>
<body>
  
  <div class="container">  
    <h2><u><i>Subtraction</i></u></h2>
    <form action="store" id="form" method="GET">
    <input type="number" name="number1" /><br />
    <input type="number" name="number2" /><br />
  
  
    <input style="margin-left: 250px;" id=" submit" type="submit"/></div>
  </div>    
    </form>
  </div>
              
</body>
</html>"""
     
      
    @cherrypy.expose
    def store(self, number1, number2):
        num1=int(number1)
        num2=int(number2)
        answer=num1-num2
        
        out= """<html>
        <body>
          
          
<p> Result: %s</p>
  
         
          
        <a style="color:red; font-size:35px;" id="shutdown"; href="./shutdown"><i>Shutdown Server</i></a>
        </body>
        </html>
          
        """
         
        return out % (str(answer))
    
      
    @cherrypy.expose
    def shutdown(self):
        cherrypy.engine.exit()
  
if __name__=="__main__":
    cherrypy.config.update({'server.socket_port': 8087})
      
    cherrypy.quickstart(Root())

chevron_right


Output:


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :