Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

25
Lecture # 32.1 Lab 10: Server 1

Transcript of Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

Page 1: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

Lecture # 32.1

Lab 10: Server 1

Page 2: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

Lab 10 Server 1

Add and delete names

Page 3: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

Lab 10 Server 1

from a pull-down/select list

Page 4: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

Lab 10 Server 1

and store them in a name database:

ServerServer

Name DatabaseName Database

Page 5: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

Lab 10 Server 1

DEMO

Page 6: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

Here are the Steps A through G:

A. Modify the HTML File: MyFamilyHistory.html

B. Set Up the cgi file

C. Set Up the Database

D. Making life easier (use a “dictionary”)

E. Processing the Form (write python functions)

F. Modify the generateHTMLPage function

G. Test it and Link it Up

Page 7: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

Here are the Steps A through G:

A. Modify the HTML File: MyFamilyHistory.html

B. Set Up the cgi file

C. Set Up the Database

D. Making life easier (use a “dictionary”)

E. Processing the Form (write python functions)

F. Modify the generateHTMLPage function

G. Test it and Link it Up

Page 8: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

A: Modify the HTML File: MyFamilyHistory.html

1.

1. Set the action attribute in to:

1. Name all necessary widgets. For example

1. Insert the list of people (string %listOfPeople%)

CopyCopy

MyFamilyHistory.htmlMyFamilyHistory.htmlHTML_CSS Server1

YourAccountName

Page 9: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

Here are the Steps A through G:

A. Modify the HTML File: MyFamilyHistory.html

B. Set Up the cgi file

C. Set Up the Database

D. Making life easier (use a “dictionary”)

E. Processing the Form (write python functions)

F. Modify the generateHTMLPage function

G. Test it and Link it Up

Page 10: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

B: Set up the cgi file

Copy the MFH.cgi file from HTML_CSS folder to the Server1 folder

CopyCopy

MFH.htmlMFH.htmlHTML_CSS Server1

Page 11: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

Here are the Steps A through G:

A. Modify the HTML File: MyFamilyHistory.html

B. Set Up the cgi file

C. Set Up the Database

D. Making life easier (use a “dictionary”)

E. Processing the Form (write python functions)

F. Modify the generateHTMLPage function

G. Test it and Link it Up

Page 12: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

C: Set up the Database – Step 1

1.

Modify initializeWithNames.py so that it has your names

Run createDB.py and initializeWithNames.py by double

clicking on them, in that order

CopyCopy

createDB.pycreateDB.pyHTML_CSS Server1

initializeWithNames.pyinitializeWithNames.py

Page 13: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

C: Set up the Database – Step 2

2. Modify MFH.cgi to include theses four lines at the end:

openDB() {Opens External DB; copies names to Internal DB}

processForm() {Adds/Deletes names to/from Internal DB} print generateHTMLPage(variables) {Generates new html

page to be sent back to Client}

closeDB() {Copies names from Internal DB to External DB}

Add the following python constructs for the Internal DB:

names = [] {[] defines names as an empty list} mothers = {} {{} declare mothers and fathers as fathers = {} empty dictionaries}

Page 14: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

C: Set up the Database – Step 3

3. Modify MFH.cgi to read and write from the database:

Insert the line “import squlite3”

Insert the functions openDB() and closeDB():

Page 15: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

Here are the Steps A through G:

A. Modify the HTML File: MyFamilyHistory.html

B. Set Up the cgi file

C. Set Up the Database

D. Making life easier (use a “dictionary”)

E. Processing the Form (write python functions)

F. Modify the generateHTMLPage function

G. Test it and Link it Up

Page 16: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

D: Making life easier (use a dictionary)

Organize the keywords with the text that is to replace the keywords in the html document using a dictionary.  

To define and initialize this dictionary put the following in the cgi file right after the cgitb.enable() statement:

•variables ={"%newName%":          "",            "%errorMessageArea%": "",            "%listOfPeople%":     "”}

Page 17: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

Here are the Steps A through G:

A. Modify the HTML File: MyFamilyHistory.html

B. Set Up the cgi file

C. Set Up the Database

D. Making life easier (use a “dictionary”)

E. Processing the Form (write python functions)

F. Modify the generateHTMLPage function

G. Test it and Link it Up

Page 18: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

E: Processing the Form – Step 1

1. Create the processForm() function called by the main routine

and insert it after the openDB() function

Page 19: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

E: Processing the Form – Step 2

2. Create the addPerson() function called by the function

processForm() function

Page 20: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

E: Processing the Form – Step 3

3. Create the deleteSelectedPerson() function called by the

function processForm() :

Page 21: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

E: Processing the Form – Step 4

4. Create the createListOfPeople() function called by the

function processForm() :

Page 22: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

Here are the Steps A through G:

A. Modify the HTML File: MyFamilyHistory.html

B. Set Up the cgi file

C. Set Up the Database

D. Making life easier (use a “dictionary”)

E. Processing the Form (write python functions)

F. Modify the generateHTMLPage function

G. Test it and Link it Up

Page 23: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

F: Modify the generateHTMLPage function

Modify the generateHTMLPage() function to replace the

keywords %newName%, %errorMessageArea% and

%listOfPeople% in the html document with the

corresponding values in the dictionary in variables.

Do this using the replace method:

Page 24: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

Here are the Steps A through G:

A. Modify the HTML File: MyFamilyHistory.html

B. Set Up the cgi file

C. Set Up the Database

D. Making life easier (use a “dictionary”)

E. Processing the Form (write python functions)

F. Modify the generateHTMLPage function

G. Test it and Link it Up

Page 25: Lecture # 32.1 Lab 10: Server 1. Lab 10 Server 1 Add and delete names.

G: Test it and link it up

Make sure the “Add Person” and the “Delete Person”

buttons work.

Also make sure that a list of names now appears in the

selection list.