Home arrow Python arrow Page 4 - CherryPy: Object-Oriented Web Development

Handling User Input - Python

Python users who engage in web development may appreciate some CherryPy. It is an object-oriented web development framework that allows you to develop web applications easily. It can run on its own server, or behind Apache. Keep reading to learn more.

TABLE OF CONTENTS:
  1. CherryPy: Object-Oriented Web Development
  2. A Simple Script
  3. Expanding Your Application
  4. Handling User Input
By: Peyton McCullough
Rating: starstarstarstarstar / 14
January 10, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Your application is probably going to have to deal with user input somewhere along the line, and, fortunately, CherryPy makes user input easy to work with. User input is passed in the form of arguments to methods, like this:

import cherrypy

class InputExample:

   def index ( self ):
      return "<form method='POST' action='submit'>\n" + \
             "E-Mail: <input type='text' name='email' />\n" + \
             "<br /><input type='submit' value='Submit' />\n" + \
             "</form>"
   def submit ( self, email = None ):
      if email:
         return "Thank you for your submission, " + email + "."
      else:
         return "You have not submitted anything!"
   index.exposed = submit.exposed = True

cherrypy.root = InputExample()
cherrypy.config.update ( file = 'development.conf' )
cherrypy.server.start()

In the above example, we set the default value of email to None, but, of course, you can set it to whatever value you would like. As you can see above, handling user input is not a complex issue. Simply make your function accept user input as arguments, and then manipulate or access those variables as needed.

User Sessions

Sessions are used to store user data in between pages. In order to access them in CherryPy, we must modify our configuration file a bit to enable sessions:

[global]

server.socketPort = 8080
server.environment = "development"
server.threadPool = 10
sessionFilter.on = True

Now, we are able to access sessions via the cherrypy.session object. Here's a simple session example:

import cherrypy

class SessionExample:

   def index ( self ):
      if cherrypy.session.has_key ( 'color' ):
         out =  "<font color='%s'>%s</font>" % ( cherrypy.session
[ 'color' ], cherrypy.session [ 'color' ] )
      else:
         out = ""
      return out + "<form method='POST' action='setColor'>\n" + \
             "Please choose a color:<br />\n" + \
             "<select name='color'>\n" + \
             "<option>Black</option>\n" + \
             "<option>Red</option>\n" + \
             "<option>Green</option>\n" + \
             "<option>Blue</option>\n" + \
             "</select><br />\n" + \
             "<input type='submit' value='Select' />\n" + \
             "</form>"
   def setColor ( self, color ):
      cherrypy.session [ 'color' ] = color
      return "<a href=''>Back</a>"
   index.exposed = setColor.exposed = True

cherrypy.config.update ( file = 'development.conf' )
cherrypy.root = SessionExample()
cherrypy.server.start()

When the user first visits the page, he or she sees a form with a color field. When the user selects a value, that value is stored inside of a session, and a link back to the first page is displayed. This time, however, the user will see the name of the chosen color displayed in the chosen color. The form will also be visible so that the chosen color may be changed.

Sessions work fairly simply. Each session name is stored as a key, with the session's value stored as the value of that key. We can check for the existence of a particular key by using the has_key method of cherrypy.session. Again, however, sessionFilter.on must be set to True in order for sessions to be enabled in your CherryPy application.

Conclusion

Though there are quite a few different web development platforms available for Python, CherryPy stands out because it makes heavy use of object orientation. Each page is a method contained in an object, and the elements in a request match the names of objects and methods. Moreover, each CherryPy script is actually an independent server. Tasks such as accepting user input and manipulating user sessions are especially easy with CherryPy, and making CherryPy work behind Apache is an easy feat. CherryPy allows for simple, object oriented web development in a simple, object oriented language.



 
 
>>> More Python Articles          >>> More By Peyton McCullough
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: