[nSLUG] Python and HTML

Frank Geitzler frank.geitzler at ns.sympatico.ca
Sat Jan 10 23:34:29 AST 2015


On Sat, 2015-01-10 at 04:24 -0400, Mike Spencer wrote:

> I may be missing something here.  I don't know what your
> programming background is, Frank. 

Over 25 years of IBM mainframe programming, and about 15 years of
Windows Foxpro programming, as well as many versions of Basic, etc.  I
support a Visual Foxpro database application for a non-profit
organization, but Microsoft has not supported VFP for some years, and I
am looking for a open-source solution.  Python may (or may not) be a
workable solution.

>  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?
> 

Right -the first few chapters provided some elementary experience in
working with Python and the Integrated Development Environment (IDLE),
and them moved into several long chapters which gave some experience in
transforming Comma-Separated Variable (CSV) files into strings and
lists.  That data was then used as input to a simple web application
which a hypothetical track-and-field coach used to display each of his
runners' top 3 times over a given distance.  I have now, with the
suggestions and help of a number of the club members, succeeded in
completing that portion of the project.
The next section of the book involves obtaining an Android emulator
(source suggested by the book) and writing and testing a further portion
of the application which would enable Android devices (phones, tablets,
and the test emulator) to access the same data.
My stumbling block was a line of code printed in the book, as the first
line of a program, where it states "This line is needed on Unix-based
systems only':
    #! /usr/local/bin/python3
with no explanation that any change might be necessary for individual
systems.  It turns out that on my system, the line must be changed to:
    #! /usr/bin/python3
Otherwise my browser accessing the localhost HTML page (which I called a
form) cannot properly process the CGI code to run additional Python
sub-program.

> 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
> server.
> 
> 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
> 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://nslug.ns.ca/pipermail/nslug/attachments/20150110/d9b6a6b9/attachment-0001.html>


More information about the nSLUG mailing list