Creating HTML and XHTML Pages in Python - With and Without CGI

01
of 06

Introduction

Creating HTML or XHTML pages in Python is as simple as output, whether from a CGI script or the command line (or shell). Most good CGI scripts produce some form of HTML feedback to thank the user and to verify that the action of the script did take place. Some thing that is not often exploited in examples, however, is the ability to create another form field in the HTML. In this tutorial, we will go from simple output to creating new forms for user input. After that, we will also look briefly at creating web pages from the command line.

At the end of this tutorial, you will know how to use Python, both standing alone and as a CGI script, to create both plain HTML pages and HTML form pages as well as being able to feed command line options to your scripts.

02
of 06

Creating HTML Output

First, creating a new HTML document is simply a matter of putting the relevant HTML as the argument of a print command:

 print ''' 
 content-type: text/html 
 
 <html> 
 <head> 
 <title> This is the title </title> 
 </head> 
 <body> 
 This is the body. 
 </body> 
 </html> 
 ''' 

Recall that triple quotes in Python tell it to print what follows verbatim. It disregards any character sequences for strings, etc. If you need to represent variable data, default to using the standard single quotation mark method.

 x = "variable"
 print "This is a %s." %(x) 
With this information, one can create a bevy of HTML pages with variable content very quickly and automagically.
03
of 06

Creating New Forms

Creating new forms in the output is the same as creating forms in a standard HTML page.

 print ''' 
 content-type: text/html 
 
 <html> 
 <head> 
 <title> This is the title </title> 
 </head> 
 <body> 
 <br> 
 <form action="./test.cgi" method="post"> 
 <p> Name: <input type="text" name="name" id="name" value=""/></p> 
 <p> Street Address: <input type="text" name="st_address" id="st_address" value=""/></p> 
 <p> Town: <input type="text" name="town" id="town" value=""/></p> 
 <p> County: <input type="text" name="county" id="county" value=""/></p> 
 <p> Postcode: <input type="text" name="postcode" id="postcode" value=""/></p> 
 <p> Telephone: <input type="text" name="telephone" id="telephone" value=""/></p> 
 <p> Fax: <input type="text" name="fax" id="fax" value=""/></p> 
 <p> Email: <input type="text" name="email" id="email" value=""/></p> 
 <p> Website: <input type="text" name="website" id="website" value=""/></p> 
 <br> 
 <input type="submit" value="Submit" /> 
 </form> 
 </body> 
 </html> 
 ''' 
This code will create a form which receives contact information from the user and delivers it to 'test.cgi' for processing. From there, the data can be processed using the methods discussed in "Programming CGI With Python" .
04
of 06

Feeding Options To Your Script

If, however, you prefer to feed options to your scripts, this is easily done as well. Using the above example, one simply need to add to the action line the command line argument prefaced with a question mark. If one wants to test for a string argument of '1', the line might look like this:

 <form action="./test.cgi?1" method="post"> 

In the subsequent CGI script 'test.cgi', one would then have an if loop to test whether the argument is '1' or not.

 x = sys.argv[1] 
 
 if x == '1': 
      [do something] 
 else: 
      [do something else] 
05
of 06

Feeding Options From the Command Line

As you know from reading Jennifer Kyrnin's tutorial on CGI, every CGI script should be executable from the command line. We should be able to feed some options at the command line and have them appear in the output.

To do this, one need only add string format operators to the output as needed. If, for example, you wanted to automate a welcome statement at the head of the form generated in part 2 of this tutorial, The script could look like this:

 import sys name = sys.argv[1] print ''' 
 content-type: text/html 
 
 <html> 
 <head> 
 <title> This is the title </title> 
 </head> 
 <body> 
 <br> 
 ''' 
 print "Welcome to %s's Addressbook Entry Page!" %(name) 
 print ''' 
 <form action="./test.cgi" method="post"> 
 <p> Name: <input type="text" name="name" id="name" value=""/></p> 
 <p> Street Address: <input type="text" name="st_address" id="st_address" value=""/></p> 
 # [more address items go here] 
 <p> Website: <input type="text" name="website" id="website" value=""/></p> 
 <br> 
 <input type="submit" value="Submit" /> 
 </form> 
 </body> 
 </html> 
 ''' 

The sys module enables the program to interact with the system, here we need it to access the options at the command line. We then assign the value to the variable 'name'. Within the output, we break out of the verbatim output just long enough to print a line with the variable. Then we resume and finish the page.

06
of 06

Concluding Comments

It is that simple. Whether working from the command line or the CGI script, the limits are the extent of your imagination. From this point, one can do anything Python can do: retrieve local information, submit information to a database, or contact a server across the world. The limits are as broad as your imagination (and your network). And, as you can learn in the networking section, you will need to imagine a lot if you want to push Python's limitations.

Format
mla apa chicago
Your Citation
Lukaszewski, Al. "Creating HTML and XHTML Pages in Python - With and Without CGI." ThoughtCo, Feb. 18, 2016, thoughtco.com/creating-html-and-xhtml-pages-2813539. Lukaszewski, Al. (2016, February 18). Creating HTML and XHTML Pages in Python - With and Without CGI. Retrieved from https://www.thoughtco.com/creating-html-and-xhtml-pages-2813539 Lukaszewski, Al. "Creating HTML and XHTML Pages in Python - With and Without CGI." ThoughtCo. https://www.thoughtco.com/creating-html-and-xhtml-pages-2813539 (accessed November 19, 2017).