ADD HTML to the code

Steve Holden steve at holdenweb.com
Mon Dec 5 10:03:19 CET 2005


Fredrik Lundh wrote:
> "Little" <cookiecandyred at yahoo.com> wrote:
> 
> 
>>Could someone tell me how to add some HTML tags to this program. I want
>>to be able to change the background color, add some headers, and put a
>>table below the map that will be displayed. Could someone please tell
>>me how to add this to the current program.
> 
> 
> the code reads the first part of the HTML from the googleHead.html file,
> and the rest is added in the section that starts with the
> 
>     # then add trailing html to finish page
> 
> comment.
> 
> assuming that you know how you want the HTML to look (styles and headers
> should go into the googleHead file, the table into the "trailing html" part), a
> text editor and the string chapters from the python tutorial should be enough
> to get you going:
> 
>     http://docs.python.org/tut/tut.html
> 

You might also want to think about the version below, which (though 
untested) uses rather commoner ways of expressing the same ideas:

""" 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)
     # Did I say two? Obviously there are six now ...
     # We just build the list as a single literal value
     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")
     ]
     st = [t]  #  in case no buildings match, use empty string
     for a, b, c, d in buildings:
        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
           st.append('var bldg%s = new GPoint( %s, %s);\n' % (a, c, d)])
           st.append('var mrk%s = new GMarker( bldg%s );\n' % (a, a))
           st.append('var htm%s = "%s";\n' % (a, b))
           st.append('GEvent.addListener(mrk%s,"click",function() {' % a)
           st.append('mrk%s.openInfoWindowHtml(htm%s); });\n' % (a, a))
           st.append('map.addOverlay(mrk%s);\n' % a)
     # output markers, if any
     # then add trailing html to finish page
     trail = "//]]> </script> </body> </html>"
     return "".join(st)+trail

Note that "str" is the name of a built-in data type, so I changed it to 
"st".

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list