Handling HTML Forms with CherryPy

Handling HTML Forms with CherryPy

Python: CherryPy


In the last article titled 'URLs with Parameters in CherryPy' (link is provided at the end), we made an application which performs mathematical operations in which operands were provided as parameters in the URL. We can improve the UX of our application by using HTML Forms to do this.

We will extend our previous application where '<application-name>/' opens the main page which contains the forms for the two methods square and power. We will use these forms to call the methods with the required parameters.

Save the below code in a file named 'html_forms.py'.

import cherrypy

class application:
def index(self):
return """<html>
<p>Call square function:<br>
<form method="get" action="square">
<input type="text" name="num" placeholder="num"/>
<button type="submit">Submit</button>
<p>Call power function:<br>
<form method="get" action="power">
<input type="text" name="num" placeholder="num"/>
<input type="text" name="pow" placeholder="pow"/>
<button type="submit">Submit</button>

def square(self, num=2):
return "The value of {} square is {}".format(num, int(num)**2)

def power(self, num=2, pow=2):
return "The value of {} to the power {} is {}".format(num, pow, int(num)**int(pow))

if __name__ == '__main__':

Here we can see the methods and the corresponding return values.

Notice that the index() method is returning a HTML code which contains two forms. The forms use the GET method and when we press the Submit button after providing parameter values, the form is sent and the corresponding method is called.

Run the code through the Terminal/Command Line as follows:

python3 html_forms.py

Now, if we enter http://localhost:8080/ in the address bar of a browser, the output will be something like this

Now we will call the square method with value, say, 9. The following output appears

Notice that the query-string in the URL shows the same parameter values we provided while submitting the form.

Similarly, we can call the power method. And the output will be something like this

HTML forms also support the POST method, in that case the query-string is not appended to the URL but is sent as the body of the client's request to the server. The result of the methods, however, remain the same.

Link to the article 'URLs with Parameters in CherryPy' :


More Articles of Aniket Sharma:

Name Views Likes
Pyperclip: Installation and Working 991 2
Number Guessing Game using Python 683 2
Pyperclip: Not Implemented Error 1033 2
Hangman Game using Python 16821 2
Using Databases with CherryPy application 1676 2
nose: Working 509 2
pytest: Working 512 2
Open Source and Hacktoberfest 868 2
Managing Logs of CherryPy applications 1005 2
Top 20 Data Science Tools 684 2
Ajax application using CherryPy 799 2
REST application using CherryPy 664 2
On Screen Keyboard using Python 5532 2
Elastic Net Regression 816 2
US Presidential Election 2020 Prediction using Python 795 2
Sound Source Separation 1165 2
URLs with Parameters in CherryPy 1635 2
Testing CherryPy application 637 2
Handling HTML Forms with CherryPy 1450 2
Applications of Natural Language Processing in Businesses 511 2
NetworkX: Multigraphs 649 2
Tracking User Activity with CherryPy 1404 2
CherryPy: Handling Cookies 822 2
Introduction to NetworkX 633 2
TorchServe - Serving PyTorch Models 1306 2
Fake News Detection Model using Python 735 2
Keeping Home Routers secure while working remotely 484 2
Email Slicer using Python 2998 2
NetworkX: Creating a Graph 1111 2
Best Mathematics Courses for Machine Learning 551 2
Hello World in CherryPy 681 2
Building dependencies as Meson subprojects 979 2
Vehicle Detection System 1081 2
NetworkX: Examining and Removing Graph Elements 608 2
Handling URLs with CherryPy 537 2
PEP 8 - Guide to Beautiful Python Code 759 2
NetworkX: Drawing Graphs 624 2
Mad Libs Game using Python 645 2
Hosting Cherry applications 613 2
Top 5 Free Online IDEs of 2020 867 2
pytest: Introduction 535 2
Preventing Pwned and Reused Passwords 582 2
Contact Book using Python 2095 2
Introduction to CherryPy 547 2
nose: Introduction 505 2
Text-based Adventure Game using Python 3002 2
NetworkX: Adding Attributes 2290 2
NetworkX: Directed Graphs 1021 2
Dice Simulator using Python 562 2
Decorating CherryPy applications using CSS 834 2