If you are looking for a way to embed Python within HTML you might want to take a look at a web environment called Spyce. Two of its advantages are its features and its flexibility. Keep reading to learn how you can Spyce up your programs.
Let's start with a form example to learn the basics of Spyce. Create a file called form.spy and place it in either Spyce's www directory if you are using the Spyce web server or somewhere in Apache if you are not. We'll start off with some simple HTML to describe the layout of the form:
At the basis of your Spyce documents, assuming that you are using Spyce for embedding Python within HTML (it can be used for other purposes), will be your plain old vanilla HTML document. With Spyce, however, we are able to add functionality to the mix by using special tags. Let's add a Spyce comment to the file:
[[-- Just a simple profile/personal information form --]] <html> <head> <title>Profile</title> </head> <body> <form method='POST'> First Name: <input type='text' name='first' /><br /> Last Name: <input type='text' name='last' /><br /> E-Mail: <input type='text' name='email' /><br /> <input type='submit' value='Submit Information' /> </form> </body> </head> </html>
If you request the file, you'll see that the comment is not displayed. Obviously, this is the proper behavior of comments. Next come statements, which, as their name suggests, are made of Python statements. Brackets can be used where indentation afterward would normally be required, such as a conditional statement. We'll use a conditional statement to determine whether the form has been submitted or not:
[[-- Just a simple profile/personal information form --]] [[ if request.post('first', None): { ]] [[-- The form has been submitted --]] [[ } else: { ]] <html> <head> <title>Profile</title> </head> <body> <form method='POST'> First Name: <input type='text' name='first' /><br /> Last Name: <input type='text' name='last' /><br /> E-Mail: <input type='text' name='email' /><br /> <input type='submit' value='Submit Information' /> </form> </body> </head> </html> [[ } ]]
We make use of the request module here, which can be accessed from every Spyce script. We use post to check whether a variable named first has been passed via the “POST” method. If it has, the value is returned inside a list, which equates to True. If it hasn't, we set the default as None, which equates to False.
Next up are Python chunks, which simply contain sections of Python code. We'll use one in our script to set variables for the form data:
[[-- Just a simple profile/personal information form --]] [[ if request.post('first', None): { ]] [[\ first = request.post('first')[0] last = request.post('last')[0] name = first + ' ' + last email = request.post('email')[0] ]] [[ } else: { ]] <html> <head> <title>Profile</title> </head> <body> <form method='POST'> First Name: <input type='text' name='first' /><br /> Last Name: <input type='text' name='last' /><br /> E-Mail: <input type='text' name='email' /><br /> <input type='submit' value='Submit Information' /> </form> </body> </head> </html> [[ } ]]
The last instruction we'll use in our script is an expression, which simply displays the end result of a particular Python expression. We'll use it to display the information back to the user:
[[-- Just a simple profile/personal information form --]] [[ if request.post('first', None): { ]] [[\ first = request.post('first')[0] last = request.post('last')[0] name = first + ' ' + last email = request.post('email')[0] ]] Name: [[=name]]<br /> E-Mail: [[=email]] [[ } else: { ]] <html> <head> <title>Profile</title> </head> <body> <form method='POST'> First Name: <input type='text' name='first' /><br /> Last Name: <input type='text' name='last' /><br /> E-Mail: <input type='text' name='email' /><br /> <input type='submit' value='Submit Information' /> </form> </body> </head> </html> [[ } ]]
Our script is now complete. Try running and modifying it.