[Tutor] NEWBIE is it possible to use HTML and Python together?

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Thu, 21 Mar 2002 18:13:45 -0800 (PST)


On Thu, 21 Mar 2002, Robert Garber wrote:

> With help from many people i was able to fix my last problem ( rolling
> dice). I was even able to add a while loop to keep rolling. NOW comes
> the part I am in the dark about. My friend has written dome HTML stull
> that he uses to play a baseball simulation game. The dice scrit is for
> him. We both want to know if it possible to use the dice script with in
> th HTML.

Hi Robert,

Yes, this is very possible.  This is where "CGI", or "Common Gateway
Interface" comes in: if your web page lives on a hosting server that
supports CGI, then you should be able to mix HTML around with Python.

In many cases, your Python programs just need to bend their output to have
HTML formatting codes, like this:

###
def helloWorld():
    print "<html><body>"
    print "Hello world!"
    print "</body></html>"

if __name__ == '__main__':
    print "Content-type: text/html\n"
    helloWorld()
###

is a simple way of getting a Python program to build an HTML page.  When
the server executes a Python program with CGI, whatever is printed out
actually goes back to the browser window.  CGI also defines how to grab
variable values from the browser, and there's a 'cgi' module in Python
that'll be helpful when you learn more about it.



> the HTMl is sett up as a baseball diamond with checkboxes to keep track
> of base runners. he has a fram set up that he would like to use for the
> roll of the dice ( hit a roll button and have the script roll the dice,
> then display the roll).

Very cool.  Yes, this is very doable, and sounds like a fun project.



> I have no idea about HTML. I am just now learning Python. Is there a way
> to "merge" the two so they can work as one?

There's some good tutorials on doing this here:

    http://www.devshed.com/Server_Side/Python/CGI/page1.html

and you're always welcome to ask questions here; we'd be glad to show some
examples of doing this.


HTML is actually not too hard; you can pick up the basics in an afternoon.
I've found that Phil Greenspun's tutorial on HTML is very useful:

    http://www.arsdigita.com/books/panda/html

and he gives his (very) personal justification of why someone would want
to mix HTML with some sort of programming language.


Please feel free to ask more questions; we'd like to hear about your
progress.  Good luck!