You are on page 1of 4

###################### 25.1.

1 An HTMLgen example from HTMLgen import * # kcdb module is assumed to provide course information and opinions module provi des the student feedback text, # via an interface which should be obvious ( J ) from the code. import kcdb import opinions schools = ['Archeology', 'Biology', 'Cultural Studies', 'Developmental Psycholog y', 'Economics'] term = 'Fall Semester 1999' # create an image as a hyperlink to the home page # first arg to Href class is the URL and the second arg is an object to be sensi tized homelink = Href('index.html', Image('image/logo.gif', align='right', border=0)) # cycle through each of the respective schools for school in schools: pagetitle = 'School of %s' % school schoolurl = school[:4] + '.html' # name the page for this school classes = kcdb.allclasses[school[0]] # fetch all classes for this school # Create a document object for the school topdoc = SimpleDocument('kc.rc', title = pagetitle) # kc.rc is the resource file topdoc.append(Heading(2, pagetitle)) # put a <H2> heading topdoc.append(homelink) # add the link back the homepage topdoc.append(Heading(3, term)) # show which semester it is topdoc.append(HR()) # add a horizontal rule # a definition list takes (term, definition) pairs. this object will behave like any list object. classlist = DefinitionList() for cls in classes: # populate the list url = '%s.html' % cls entry = Href(url, cls) # this will be the left side of the definition li st classlist.append( (entry, kcdb.get_title(cls)) ) topdoc.append(classlist) #remember to attach the list to the document topdoc.append(HR()) topdoc.write(schoolurl) # generate the HTML file for this document # Now we generate the detail file for each course in this school lc = len(classes) for i in range(lc): # loop through each class cls = classes[i] if i != 0: # set the reference to the previous HTML file prev = '%s.html' % classes[i-1] else: prev = None if i < lc - 1: # set the reference to the next HTML file next = '%s.html' % classes[i+1] else: next = None url = '%s.html' % cls pagetitle = '%s: %s' % (school, cls) # now that we have a little context we create a SeriesDocument

doc = SeriesDocument('kc.rc', title = pagetitle, goprev = prev, gonext = next, gotop = schoolurl, gohome = 'index.html') # use an <H3> for the class title doc.append(H(3, kcdb.get_title(cls))) # Now we create a small table to display the course info. table = TableLite(border=0, cell_spacing=0) # this table won't be visibl e # load up a list or tuple with info for the table coursedata = ( ('Description', kcdb.get_description(cls)), ('Schedule', kcdb.get_schedule(cls)), ('Prerequisites', kcdb.get_prereq(cls)), ('Instructor', kcdb.get_instructor(cls)), ('Student Comments', opinions.get_comment(cls)) ) for entry, text in coursedata: row = TR() # add a row for each entry in coursedata # specify the flavor of table cell with TD instances # note that the append methods in HTMLgen support adding # more than one thing at a time row.append(TD(entry, bgcolor="#9999cc", valign="TOP"), TD(text)) table.append(row) # don't forget to add the table to the document doc.append(table) # generate each course file. Done! doc.write(url) #### ------------------------------- File kr.rc -----------------------------# This is actually a Python module supporting all the regular syntax. # The HTMLcolors module provides some web-safe color definitions for those who d on't # think in hex-triplets from HTMLcolors import * # Just set the constant attributes of the document classes here. author = 'Dean Nosuchperson' email = 'dean@kettleman.edu' bgcolor = WHITE textcolor = BLACK linkcolor = RED vlinkcolor = PURPLE # images can be specified as just the file names or as a tuple # containing the width and height in pixels banner = 'image/kc-banner.gif' # if you don't specify dimensions HTMLgen will attempt to read the file # and determine them. logo = 'image/logo.gif' blank = ('image/blank.gif', 90, 30) prev = ('image/back.gif', 90, 30) next = ('image/next.gif', 90, 30) top = ('image/school.gif', 90, 30) home = ('image/college.gif', 90, 30)

###################### 22.12 CGI and forms from HTMLgen import * from HTMLcolors import * doc = SimpleDocument(title='Income Survey', bgcolor=WHITE) # if this in a CGI pr og use cgi=1 F = Form('http://www.kettleman.edu/cgi-bin/income-survey.py') # the URL of the C GI program F.append( Input(type='text', name='city', llabel='City:', size=30), BR() ) F.append( Input(type='text', name='state', llabel='State:', size=30), BR() ) for sex in ('Male', 'Female', 'Other'): F.append(Input(type='radio', name='sex', rlabel=sex), BR()) incomes = [('$0 - $10K', 0), ('$10K - $25K', 1), ('$25K - $40K', 2), ('$40K +', 3)] F.append( Select(incomes, name='income',size=1), BR() ) doc.append(F) doc.write('survey.html') #### from HTMLgen import * from HTMLcolors import * import string class InputTable: """InputTable(entries, [keyword=value]...) Entries is a list of 3-element tuples (name, input_object, note) where name will label the input object on the left column, the input_object can be anything, and the note is a string to provide optional notations to the right of the input widgets. """ def __init__(self, entries, **kw): self.entries = entries self.leftcolor = GREY2 # color used for left label column self.centercolor = WHITE # color used for center input column self.rightcolor = WHITE # color used for right label column self.notecolor = RED # color used for notation text self.leftalign = 'right' # text alignment for left label column for (item, value) in kw.items(): setattr(self, item, value) def __str__(self): table = TableLite(border=0, cellspacing=4) for (label, input, note) in self.entries: #assume a 3-tuple row = TR() row.append(TD(label, align=self.leftalign, bgcolor=self.leftcolor)) row.append(TD(input, bgcolor=self.centercolor)) if note: row.append(TD(Font(note, color=self.notecolor), bgcolor=self.rig htcolor)) table.append(row) return str(table) class RadioButtons: widgettype = 'radio' def __init__(self, items, **kw): self.items = items self.name = 'radiochoice' self.orient = 'vertical'

self.selected = [] for (item, value) in kw.items(): setattr(self, item, value) def __str__(self): if self.orient[0] == 'v': sep = '<BR>\n' else: sep = ', \n' if type(self.selected) is type(""): self.selected = [self.selected] s = [] for item in self.items: if item in self.selected: s.append(str(Input(type=self.widgettype, name=self.name, checked =1))) else: s.append(str(Input(type=self.widgettype, name=self.name))) s.append(str(item)) s.append(sep) return string.join(s[:-1], "") class CheckBoxes(RadioButtons): widgettype = 'checkbox' def make_survey_page(): doc = SimpleDocument(title='Income Survey', bgcolor=WHITE) # if this in a CG I prog use cgi=1 F = Form('http://www.kettleman.edu/cgi-bin/income-survey.py') # the URL of t he CGI program survey = [] survey.append(['City', Input(name='city', size=30), 'Required']) survey.append(['State', Input(name='state', size=20), 'Required']) survey.append(['Sex', RadioButtons(['Male', 'Female', 'Other']), ''] ) incomes = [('$0 - $10K', 0), ('$10K - $25K', 1), ('$25K - $40K', 2), ('$40K +', 3)] survey.append(['Income', Select(incomes, name='income', size=1), 'Required'] ) F.append(InputTable(survey)) doc.append(F) doc.write('survey.html')

You might also like