ADD HTML to the code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Little

    #1

    ADD HTML to the code

    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. Thanks in advance.

    Red.py

    """ 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.addList ener(mrk%s,"cli ck",function( ) {' % a
    str += 'mrk%s.openInfo WindowHtml(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

    htaccess

    SetHandler python-program
    PythonHandler mod_python.publ isher
    PythonDebug On

    googleHead.html

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <TITLE>Universi ty of Iowa Cambus</TITLE>
    <script
    src="http://maps.google.com/maps?file=api&v =1&key=ABQIAAAA oq-X4K0kg9eYo1EhqR tUSxRepKXVB5XJt iHJ0W4XJYyDQ5hN nxQKi9AqI9J1B9u wt8TVJptjDwSqPA "
    type="text/javascript"></script>
    </head>
    <body>
    <form action="query" method="get">
    Street Address in Iowa City:
    <input type="text" name="address">
    </form>
    <p>
    <div id="map" style="width: 800px; height: 600px"></div>
    <script type="text/javascript">
    //<![CDATA[

    var map = new GMap(document.g etElementById(" map"));
    map.addControl( new GSmallMapContro l());
    map.centerAndZo om(new GPoint(-91.527786,41.66 1114), 2);

  • Fredrik Lundh

    #2
    Re: ADD HTML to the code

    "Little" <cookiecandyred @yahoo.com> wrote:
    [color=blue]
    > 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.[/color]

    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:

    Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax an...


    </F>



    Comment

    • Steve Holden

      #3
      Re: ADD HTML to the code

      Fredrik Lundh wrote:[color=blue]
      > "Little" <cookiecandyred @yahoo.com> wrote:
      >
      >[color=green]
      >>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.[/color]
      >
      >
      > 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
      >[/color]

      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('GEve nt.addListener( mrk%s,"click",f unction() {' % a)
      st.append('mrk% s.openInfoWindo wHtml(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)+tra il

      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/

      Comment

      • bruno at modulix

        #4
        Re: ADD HTML to the code

        Little wrote:[color=blue]
        > 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. Thanks in advance.
        >[/color]
        (snip)
        Do yourself a favor : check the various templating tools available,
        choose one that fits you taste, and stop embedding half-baked html in
        your Python code.


        --
        bruno desthuilliers
        python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
        p in 'onurb@xiludom. gro'.split('@')])"

        Comment

        Working...