CS108 Lecture 24: Web Forms Processing CGI Data Aaron Stevens 27 - - PDF document

cs108 lecture 24 web forms
SMART_READER_LITE
LIVE PREVIEW

CS108 Lecture 24: Web Forms Processing CGI Data Aaron Stevens 27 - - PDF document

CS108 Lecture 24: Web Forms Processing CGI Data Aaron Stevens 27 March 2009 1 Overview/Questions Review: Python and HTML HTML Forms Processing Form Fields Organizing a Python Web Application using functions, decision


slide-1
SLIDE 1

1

1

Aaron Stevens

27 March 2009

CS108 Lecture 24: Web Forms

Processing CGI Data

2

Overview/Questions

– Review: Python and HTML – HTML Forms – Processing Form Fields – Organizing a Python Web Application using functions, decision making, etc.

slide-2
SLIDE 2

2

3

Review: Python and HTML

Recall: HTML is the Hyper Text Markup language, used to describe the formatting

  • f web pages.

Python can generate HTML as output from any program. Run it on a web server, and you get a web page as output.

4

Hello World Python Web

slide-3
SLIDE 3

3

5

HTML Output

This is the HTML Produced by the Python Script

– As seen in Web Browser’s View Page Source.

6

HTML Forms

HTML Forms are a way to provide input to the webserver: Forms are created with the <FORM> tag.

slide-4
SLIDE 4

4

7

HTML Forms

Here is the code that created that form: HTML/GUI Display:

8

Processing CGI Form Fields

Common Gateway Interface or CGI is a way for web applications to send data to the server. Python’s CGI module provides access to a dict of all form fields and their values. Use the cgi.FieldStorage() function to get this dict, which I am referring to as form.

slide-5
SLIDE 5

5

9

Processing CGI Form Fields

Notice a few things:

– Check that a field exists before ‘reading’ it. – Notice the item in the dict is an object of type cgi.MiniFieldStorage. The data we want is its .value attribute.

10

CGI Data in HTML Response

After we ‘read’ the CGI Fields, we can use in our program/output:

slide-6
SLIDE 6

6

11

HTML Forms

There are all kinds of HTML form elements:

type=“radio” type=“checkbox”

12

HTML Forms

There are all kinds of HTML form elements:

<option>… </option> tag:

slide-7
SLIDE 7

7

13

More HTML Forms…

Reference: http://www.w3schools.com/HTML/html_forms.asp for more syntax, etc.

14

Reading Other CGI Form Data

Here are a variety of CGI Form Fields:

slide-8
SLIDE 8

8

15

Reading Other CGI Data

All Form Fields are presented to Python as Type MiniFieldStorage. This code: Produces this HTML output:

16

Reading Other CGI Data

These are key-value pairs: Notice that the checkboxes might produce a list of MiniFieldStorage objects.

– Or a single MiniFieldStorage if only one checkbox was selected!

slide-9
SLIDE 9

9

17

Reading a CGI Data list

If needed, check type of form fields… And process as a list of MiniFieldStorage Notice output obtained by indexing into this list:

18

Organizing a Python Web App

Separate computation from presentation:

– Write functions to do the computation, and unit test with a non-web, console application. – After you are satisfied with inputs/outputs for the functions, write the CGI/HTML parts and test.

Always test computation first, and then begin work on presentation.

slide-10
SLIDE 10

10

19

Organizing a Python Web App

Separate each web interaction into its own function.

– Print a web form. – Display an output. – Got multiple paths/outputs -- one function each!

20

Take-Away Points

– HTML <FORM> tag – CGI field data. – Be mindful of what type of data you are

  • expecting. When in doubt, use introspection!
slide-11
SLIDE 11

11

21

Student To Dos

– Readings:

  • HTML Tutorial: http://www.w3schools.com/HTML/

– Forms (Friday)

– HW 10 due Tuesday 3/31