Table of contents
1.
Introduction
2.
About CherryPy
3.
Submit a form
4.
Frequently Asked Questions
4.1.
Is CherryPy free to use?
4.2.
What does the CherryPy expose method do?
4.3.
How can you use CherryPy in Python?
4.4.
Can we see CherryPy as a Web Server?
4.5.
When was the first version of CherryPy released?
5.
Conclusion
Last Updated: Mar 27, 2024

Tutorial 3: How to submit a form in cherrypy

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

Introduction

Have you ever worked on developing web applications using frameworks of Python? Have you ever worked on form submission? You might have heard about Django, a high-level Python web framework. But in this article, we will study how to submit a form in CherryPy.

Let's initiate it by learning a little bit about CherryPy.  

intro image

About CherryPy

CherryPy is one of the most well-known Python web frameworks; only a few people know its existence. Due to CherryPy's absence of a complete stack with built-in support for a multi-tier design, this is the case.

CherryPy is a Python object-oriented framework. It is used by programmers so that they will be able to design web apps in a way similar to that of another object-oriented Python program. The result is shorter development times and small source code.

Submit a form

You can construct web apps using the web framework CherryPy. The most common form apps take through an HTML user interface communicating with your CherryPy server. Let's check out an example that will help us understand how we can handle HTML forms:

import random
import string
import cherrypy

class StringGenerate(object):
    @cherrypy.expose
    def index(self):
        return """<html>
          <head></head>
          <body>
            <form method="get" action="generate">
              <input type="text" value="8" name="length" />
              <button type="submit">Initiate the sending...</button>
            </form>
          </body>
        </html>"""

    @cherrypy.expose
    def generate(self, length=8):
        return ''.join(random.sample(string.hexdigits, int(length)))


if __name__ == '__main__':
    cherrypy.quickstart(StringGenerate())
You can also try this code with Online Python Compiler
Run Code

 

Now, you have to save this code into a file which we can name anything, but here we will call it "tutorial03.py". After that, try to run it using the following command:

$ python tutorial03.py
You can also try this code with Online Python Compiler
Run Code

 

After that, navigate your browser to the URL:

http://127.0.0.1:8080 

It should show you this output:

url output

It will show a straightforward input form to specify the string length you want to create. You should also notice that we have used the GET method in this tutorial. Also, when you press the "Initiate the sending" button, it will show you something like this:

initiate the sending output

HTML forms also allow the POST method, in which case the query string is given to the server as the body of the client's request rather than being attached to the URL. However, this would not alter the exposed method in your application because CherryPy handles both in the same manner and uses the exposed handler arguments to handle the query-string (key, value) pairs.

Frequently Asked Questions

Is CherryPy free to use?

CherryPy is an open-source project and thus, welcomes contributions. If you are genuinely interested, it is allowed for you to Fork CherryPy on GitHub and submit a pull request with your changes or updates.

What does the CherryPy expose method do?

Once CherryPy has been found and a method is known as exposing, it is totally up to you, as a programmer or developer, to provide the tools for implementing the application's logic. CherryPy knows that the developer knows best.

How can you use CherryPy in Python?

CherryPy allows you to use the CRUD (Create, Retrieve, Update and Delete) functionalities to develop web applications. It also provides help in managing the project from anywhere in the world just by using the user's web browser.

Can we see CherryPy as a Web Server?

Cherry WSGI Web Server is a modularized component that provides functionality that allows it to serve any Python WSGI web-based application.

When was the first version of CherryPy released?

The very first version of CherryPy was released in June 2002.

Conclusion

In this article, we have extensively discussed how to submit a form in cherrypy. We have also explained cherrypy, the procedure to submit a form using cherrypy, and more in detail.

We hope this blog has helped you enhance your knowledge of submitting a form in cherrypy. If you would like to learn more, check out our articles on Basics of pythonPython vs. JavaScript, and the best python frameworks. Practice makes a man perfect. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Do upvote our blog to help other ninjas grow. Happy Coding!

thank you image
Live masterclass