Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Putting in an html table

Reply
Thread Tools

Putting in an html table

 
 
Little
Guest
Posts: n/a
 
      12-04-2005
Could someone start me on putting in a table into this code, and some
HTML tags. I would to make the table below the map and have a header at
the top. Thanks for the help.

""" Publisher example """

def query(req, building=""):
# NOTE: best way to understand this is to see the output,
# that is, to "view source" on the generated web page
# read common header for any google mapping
f = file('/home/ta/public_html/maplish/googleHead.html','r')
t = f.read()
f.close()
# define the two buildings we know (because no SQL is done here)
buildings = [ ("cb", "Cambus Office", "-91.552977", "41.659655")
]
buildings += [ ("va/hardlib", "VA/Hardin Library", "-91.549501",
"41.662348") ]
buildings += [ ("hancher", "Hancher Auditorium", "-91.538214",
"41.669529") ]
buildings += [ ("currier", "Currier Hall", "-91.534996",
"41.666163") ]
buildings += [ ("schaeffer", "Schaeffer Hall", "-91.535296",
"41.660969") ]
buildings += [ ("shospital", "South Hospital", "-91.548900",
"41.658885") ]
str = '' # in case no buildings match, use empty string
for x in buildings:
a,b,c,d = x # isolate all the tuple components into a,b,c,d
if building.lower() == a:
# construct javascript, using Python %s to substitute names
and data
# see http://docs.python.org/lib/typesseq-strings.html if
needed
str = 'var bldg%s = new GPoint( %s, %s);\n' % (a, c, d)
str += 'var mrk%s = new GMarker( bldg%s );\n' % (a, a)
str += 'var htm%s = "%s";\n' % (a, b)
str += 'GEvent.addListener(mrk%s,"click",function() {' % a
str += 'mrk%s.openInfoWindowHtml(htm%s); });\n' % (a, a)
str += 'map.addOverlay(mrk%s);\n' % a
# output markers, if any
t = t + str
# then add trailing html to finish page
trail = "//]]> </script> </body> </html>"
t = t + trail
return t

 
Reply With Quote
 
 
 
 
bruno at modulix
Guest
Posts: n/a
 
      12-05-2005
Little wrote:
> Could someone start me on putting in a table into this code, and some
> HTML tags. I would to make the table below the map and have a header at
> the top. Thanks for the help.
>
> """ Publisher example """
>
> def query(req, building=""):
> # NOTE: best way to understand this is to see the output,
> # that is, to "view source" on the generated web page
> # read common header for any google mapping
> f = file('/home/ta/public_html/maplish/googleHead.html','r')


Never hardcode a path.

> t = f.read()
> f.close()
> # define the two buildings we know (because no SQL is done here)
> buildings = [ ("cb", "Cambus Office", "-91.552977", "41.659655")
> ]
> buildings += [ ("va/hardlib", "VA/Hardin Library", "-91.549501",
> "41.662348") ]
> buildings += [ ("hancher", "Hancher Auditorium", "-91.538214",
> "41.669529") ]
> buildings += [ ("currier", "Currier Hall", "-91.534996",
> "41.666163") ]
> buildings += [ ("schaeffer", "Schaeffer Hall", "-91.535296",
> "41.660969") ]
> buildings += [ ("shospital", "South Hospital", "-91.548900",
> "41.658885") ]


# avoid useless operations
buildings = [
("cb", "Cambus Office", "-91.552977", "41.659655"),
("va/hardlib", "VA/Hardin Library", "-91.549501", "41.662348"),
("hancher", "Hancher Auditorium", "-91.538214", "41.669529"),
("currier", "Currier Hall", "-91.534996", "41.666163") ,
("schaeffer", "Schaeffer Hall", "-91.535296", "41.660969"),
("shospital", "South Hospital", "-91.548900", "41.658885")
]


> str = '' # in case no buildings match, use empty string

# don't use 'str' as an identifier, it will shadow the builtin str type.


> for x in buildings:
> a,b,c,d = x # isolate all the tuple components into a,b,c,d
> if building.lower() == a:


If what you want is to find the building matching the one given as
argument, you should build a dict, not a list of tuples.

buildings = {
"cb" : ("Cambus Office", "-91.552977", "41.659655"),
"va/hardlib" : ("VA/Hardin Library", "-91.549501", "41.662348"),
"hancher" : ("Hancher Auditorium", "-91.538214", "41.669529"),
"currier" : ("Currier Hall", "-91.534996", "41.666163") ,
"schaeffer" : ("Schaeffer Hall", "-91.535296", "41.660969"),
"shospital" : ("South Hospital", "-91.548900", "41.658885")
}

Now you don't need to loop and compare, a simple :
buildings.get(building.lower())
should be enough

(NB : Anyway, this dict should not be hardcoded. Put this in a separate
conf file, in a db, or whatever, but keep it out of the code.)


> # construct javascript, using Python %s to substitute names
> and data
> # see http://docs.python.org/lib/typesseq-strings.html if
> needed
> str = 'var bldg%s = new GPoint( %s, %s);\n' % (a, c, d)
> str += 'var mrk%s = new GMarker( bldg%s );\n' % (a, a)
> str += 'var htm%s = "%s";\n' % (a, b)
> str += 'GEvent.addListener(mrk%s,"click",function() {' % a
> str += 'mrk%s.openInfoWindowHtml(htm%s); });\n' % (a, a)
> str += 'map.addOverlay(mrk%s);\n' % a


string concatenation is not an efficient idiom. In a loop, the common
idiom is to use a list and join it, but here I guess a triple quoted
string and simple templating trick would be better.


> # output markers, if any
> t = t + str
> # then add trailing html to finish page
> trail = "//]]> </script> </body> </html>"
> t = t + trail
> return t


This is definitevely not how I would do HTML/js in Python. There are a
lot of templating / HTML generation tools available, that allow to
cleanly separate logic from presentation.

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Article putting forward a case for table layout patrick j HTML 23 11-03-2006 09:28 PM
putting asp:Textbox in a asp:Table cell? Ed West ASP .Net 1 08-09-2004 08:43 AM
Difference between putting code in constructor and putting code in static{} Saurabh Java 6 05-30-2004 02:44 PM
Putting a server side control inside an html element attribute John Lau ASP .Net 4 05-05-2004 12:42 AM
Re: putting a form in a html email Adrienne HTML 5 07-18-2003 10:25 AM



Advertisments