Home arrow Python arrow Page 2 - Using Spyce

Basic Syntax - Python

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.

TABLE OF CONTENTS:
  1. Using Spyce
  2. Basic Syntax
  3. Active Tags
  4. Handling Form Data
By: Peyton McCullough
Rating: starstarstarstarstar / 9
January 17, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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:

<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>

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.



 
 
>>> 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: