Making a pass form cgi => webpy framework

rurpy at yahoo.com rurpy at yahoo.com
Sun Jun 23 18:29:15 EDT 2013


On 06/23/2013 09:15 AM, Νίκος wrote:
> Στις 23/6/2013 5:57 μμ, ο/η rurpy at yahoo.com έγραψε:
>> On 06/23/2013 07:01 AM, Νίκος wrote:
> Hello, as you all know i'am using cgi method for my web script.
>>>
>>> I have been told that webpy is the way to go sor script simple enough as
>>> mines.
>>>
>>> Can you please show me the simplest example you can think of utilizing a
>>> templates (index.html) and a python script (metrites.py) ?
>>>
>>> I want to see if it is easy to ditch cgi and pass to the world of web
>>> frameworks.
>>
>> I don't know anything about webpy so I can help with that.
>>
>> But I did want to say that you can use templates without
>> using a web framework like webpy.  They are very useful
>> even with cgi code and can simplify the cgi code a lot.
>>
>> For my web server stuff (relatively small sites with low
>> traffic) cgi is fine (simple, easy to debug, lots of online
>> documentation and help) so I don't buy the "cgi is dead"
>> comments seen frequently here.
> 
> Exactly my sentiments. As you know my webiste is http://superhost.gr
> I write my html templates in notepad++ and then i use 'metrites.py' 
> script as a means of opening my html templates and substitute spcial 
> variables of html files with the onces being calculated within my python 
> script, foe xample every html file of mine bottom down has its own page 
> counter.
> 
> Ill give an example.
>[...snip...]
> Every request of an html file on superhost.gr is being redirected into 
> metrites.py script which make the substitution insde the html 
> templates(yes in one python script but it can handle all my html files 
> form within)
> 
> This is the simplest idea of web frameworking (if i can call it like that)
> 
> Now, if you can find me a better way of what i have actually descri bed 
> i will ditch cgi and use your method. Personally i believe a more easy 
> method from what i demonstrated cannot possibly .exist.

Firstly, I wrote:
>> I don't know anything about webpy so I can help with that.
I meant, I don't know anything about webpy so I *can't* help 
with that.  (Just in case it wasn't obvious.)

The option I mentioned was continue using cgi (but using a web 
templates package instead of your own), not to ditch cgi.  You 
would be ditching cgi if you switch to a web framework.  I was
saying that you don't have to use a web framework to use a web 
template package.

The advantage of using an already-developed web template package
is that it will have a lot more features and capabilities than 
your current way.  The disadvantage is that it will take some time
to learn to use it; you already know how to use your own template 
system.

If you do want to look into using a template package you'll need 
to pick one.  There is list of them with some info in each, here:
  http://wiki.python.org/moin/Templating?highlight=%28templates%29

Mako templates is a very popular package and since I've played
with it a little, I'll use it as an example.

Your template would look almost the same.  Instead of using Python's
%s string formatting, you would use Mako's rules for substitutions.
For simple variable they use ${variablename}.

> ===================================
> <div align=center><br><br><br>
> 	<font color=white size=4>
> 		Οι servers τρέχουν Linux και ο χειρισμός του λογαριασμού χρήστη 
> γίνεται μέσω του cPanel.<br>
> 		Eδρεύουν στο Houston του Texas, σε ένα απο τα πιο αξιόπιστα - ταχύτατα 
> DataCenters της Αμερικής.<br>
> 		Ζητείστε να σας ανοίξω δοκιμαστικό λογαριασμό μιας εβδομάδας προς 
> αξιολόγηση της ποιότητας υπηρεσιών!<br>
> 	<font color=gold size=4> <b><i> ${thequote} </i></b> <br><br><br>
> 
> 	<img src="/data/images/hosting.png">
> 	<img src="/data/images/design.png"><br>
> 
> 	<audio controls autoplay="autoplay">
> 		<source src="/data/music/${mp3file}" type="audio/mp3">
> 	</audio>
> </div><br><br>
> ===================================

and your code that gets values to substitute would be the same:

> ======================================
> # pick a random mp3 from the the music directory
> quote = random.choice( list( open( 
> '/home/nikos/www/data/private/quotes.txt', encoding='utf-8' ) ) )
> # pick a random line from the quote text file
> music = random.choice( os.listdir( '/home/nikos/www/data/music/' ) )
> ======================================

But instead of opening and reading index.html yourself, you use 
Mako to read it:

  template = mako.template.Template (filename='templates/index.html')
    
and then use Mako to do the substitutions:

  html = template.render (thequote=quote, mp3file=music)
  print (html)

"thequote" and "mp3file" are the variable names used inside the 
template.  "quote" and "music" are of course the variable names
yuo use in your python code.

In this simple example, there is not much advantage of Mako
over your templates.  But with more complicated cases, for
instance, when you have tables or forms that you want to 
dynamically construct from external data (info extracted
from a database for example) then a template system like Mako 
makes your Python code simpler because all the table and form '
stuff is in the template file, and all the Python code has to 
do is get the data and pass it to the template's render method.

For example, for a web page that shows a table of users and
their last login dates, your template might contain something
like

   <table> 
      <tr><th>Username</th>  <th>Last Login</th></tr>
      %for user,llog in userrecs:
        <tr><td>${user}</td>  <tr><td>${llog}</td></tr>
      %endfor
   </table>

And your Python cgi code

  userdata = mysqlcursor.execute (
    "SELECT username,MAX(logintime) FROM userlogins GROUP BY username", [])
  html = template.render (userrecs=userdata)
  print (html)

When you call template.render() above, Mako and the template 
will create an html table row for each user in userdata.

All code above is untested and from memory so there is probably 
errors in it.  I was just trying to give you an idea of how
template systems can make cgi code simpler without moving 
everything to a full web framework.



More information about the Python-list mailing list