[nSLUG] Re: Python and HTML

Mike Spencer mspencer at tallships.ca
Sat Jan 10 04:24:26 AST 2015

I may be missing something here.  I don't know what your
programming background is, Frank.  But you originally wrote:

> Has anyone experience with Python generating HTML?  I have been
> attempting to learn Python by working my way through a book called
> HeadFirstPython...

I have no knowledge of Python, not even why it might be desirable to
learn it in particular.  But if I were to set about to learn it, I
would try to simplify my early efforts as much as possible.

Your proposed plan here involves both Python code to be
executed when called by the cgi-bin capability of an httpd server and
Python code implementing the the server itself.

Since you're at Chapter 7 of the book, I infer that you've already
written a bunch of elementary Python code.  Is that right?

Assuming that Python has some kind of plain print statement, you
should be able to write code that generates HTML and verify the HTML
visually.  That would separate out HTML generation from cgi-bin and

Then you could write some simple scripts in shell code, perl or any
other language you're already comfortable with and try to get the
cgi-bin capacity of the web server you're using to execute that.
Accessing http://localhost:8080/cgi-bin/some-script should execute the
script and return its output, requiring only that the script outputs
minimal valid HTTP headers and a blank line.

Frank wrote:

> ...Chapter 7 purports to create a HTML application with a main form
> ("webapp/index.html") calling a subsidiary form
> ("webapp/cgi-bin/generate_list.py"), which in turn will call
> additional subsidiary forms or alternatively will return to the main
> form.

So is this one of those deals where an HTML file contains executable
code which the server is expected to detect and execute, possibly
inserting the output from that code inline in place of the code when
rendering the HTML document?

I don't quite get why you're referring to index.html as a "form".  Is
that Python terminology?  For anything that contains Python code?
Does index.html contain Python code that supposed to be executed
before index.html is served to the requesting host?

I would try to separate the operation of cgi-bin on the server from
correct execution of the code you want it to run.  Maybe that's not
possible (see preceding 2 paragraphs.)

Hatem wrote:

> ...writing cgi scripts is a way of the past ... way way back. I
> would advise you to pick up a web framework ... there are simple
> ones (like bottle <http://bottlepy.org/>) or bigger ones (like
> django <https://docs.djangoproject.com/en/1.7/>),

Frank did say that he was trying to *learn* Python.  A quick look at
those web sites leads me to believe that you need to be a Python whiz
before starting with them.

Cgi-bin may be obsolete for production web sites but, assuming that
the server does something predictable -- the right thing -- with it,
it has the advantage of being simple and straightforward, not
requiring a lot of knowledge of serious web site management.  For
something running on localhost or on a lightly used, non-critical,
non-commercial site, it seems to me just fine.

Well, my electric toaster was 100 years old in 2013 so I'm detectably
in favor of old, simple, KISS, rather than the opposite.

- Mike

Michael Spencer                  Nova Scotia, Canada       .~. 
mspencer at tallships.ca                                     /( )\
http://home.tallships.ca/mspencer/                        ^^-^^

More information about the nSLUG mailing list