Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Making a pass form cgi => webpy framework

Reply
Thread Tools

Making a pass form cgi => webpy framework

 
 
Νίκος
Guest
Posts: n/a
 
      06-23-2013
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.

thank you.
--
What is now proved was at first only imagined!
 
Reply With Quote
 
 
 
 
rurpy@yahoo.com
Guest
Posts: n/a
 
      06-23-2013
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.
 
Reply With Quote
 
 
 
 
Νίκος
Guest
Posts: n/a
 
      06-23-2013
Στις 23/6/2013 5:57 μμ, ο/η http://www.velocityreviews.com/forums/(E-Mail Removed) *γραψε:
> 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.

This is a part of my index.html template:

===================================
<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> %s </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/%s" type="audio/mp3">
</audio>
</div><br><br>
===================================

and this being the part of the code of metrites.py that substitute the
'%s' special variables with the actual data.

======================================
# 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/' ) )
======================================


Now with the help of an .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^/?(.+\.html) /cgi-bin/metrites.py?file=%{REQUEST_FILENAME}
[L,PT,QSA]

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.


Thank you.

--
What is now proved was at first only imagined!
 
Reply With Quote
 
rurpy@yahoo.com
Guest
Posts: n/a
 
      06-23-2013
On 06/23/2013 09:15 AM, Νίκος wrote:
> Στις 23/6/2013 5:57 μμ, ο/η (E-Mail Removed) *γραψε:
>> 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/Templati...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.
 
Reply With Quote
 
Νίκος
Guest
Posts: n/a
 
      06-24-2013
Στις 24/6/2013 1:29 πμ, ο/η (E-Mail Removed) *γραψε:
> 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.


I see. if for example i take my files.py where it acts like a template
and python script all together and its in fact displaying not usernames
but constructs an html table form extracted from database info, why you
mako's approach and not keep it as i have it now which is:


#
================================================== ================================================== =============
# Display ALL files, each with its own download button
#
================================================== ================================================== =============
print('''<body background='/data/images/star.jpg'>
<center><img src='/data/images/download.gif'><br><br>
<table border=5 cellpadding=5 bgcolor=green>
''')

try:
cur.execute( '''SELECT * FROM files ORDER BY lastvisit DESC''' )
data = cur.fetchall()

for row in data:
(filename, hits, host, lastvisit) = row
lastvisit = lastvisit.strftime('%A %e %b, %H:%M')

print('''
<form method="get" action="/cgi-bin/files.py">
<tr>
<td> <center> <input type="submit" name="filename" value="%s"> </td>
<td> <center> <font color=yellow size=5> %s </td>
<td> <center> <font color=silver size=4> %s </td>
<td> <center> <font color=orange size=4> %s </td>
</tr>
</form>
''' % (filename, hits, host, lastvisit) )
print( '''</table><br><br>''' )
except pymysql.ProgrammingError as e:
print( repr(e) )

Why use mako's approach which requires 2 files(an html template and the
actual python script rendering the data) when i can have simple print
statements inside 1 files(my files.py script) ?
After all its only one html table i wish to display.


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


And if we wanted to to compare an html template method to a web
framework solution?

What are the benefits of one over the other?

I know you dont use the latter but this questios is for averybody that does.


--
What is now proved was at first only imagined!
 
Reply With Quote
 
Michael Torrie
Guest
Posts: n/a
 
      06-24-2013
On 06/23/2013 07:44 PM, Νίκος wrote:
> Why use mako's approach which requires 2 files(an html template and the
> actual python script rendering the data) when i can have simple print
> statements inside 1 files(my files.py script) ?
> After all its only one html table i wish to display.


Sooner or later your needs grow to the point where your single python
CGI file is mixing so much html and python code that it becomes unwieldy.

> And if we wanted to to compare an html template method to a web
> framework solution?


A web framework gives you database abstractions so you can easily target
most SQL servers without changing code (MySQL, MS SQL, PostgreSQL,
Oracle, etc), and they also provide frameworks for doing authentication
and authorization. Authorization in particular becomes unwieldy quickly
if you have to do it all in CGI. With a framework it's relatively easy
to decorate a view with a wrapper that can only allow the view to be
displayed if a web client has logged in with a particular set of
permissions (stored in your user database of coures).

> What are the benefits of one over the other?


If you need to do user logins, a framework is going to become rather
essential, in my opinion. At least if you need to support fine-grained
permissions, which is what most enterprise web apps require (at least
the ones I worked on).

> I know you dont use the latter but this questios is for averybody that does.


Personally I feel that a templating engine is essential for any web
development, CGI or not. Mixing html and code leads to an
unmaintainable mess.

A framework is not essential, but often desired. If you don't use a
framework, eventually you'll find yourself creating your own framework
in essence, though often not nearly as robustly or as flexibly.
 
Reply With Quote
 
rusi
Guest
Posts: n/a
 
      06-24-2013
On Monday, June 24, 2013 10:07:57 AM UTC+5:30, Michael Torrie wrote:
> On 06/23/2013 07:44 PM, Νίκος wrote:
>
> > Why use mako's approach which requires 2 files(an html template and the
> > actual python script rendering the data) when i can have simple print
> > statements inside 1 files(my files.py script) ?
> > After all its only one html table i wish to display.


Thats a well-known very widely used technology. Its called php

>
> Sooner or later your needs grow to the point where your single python
> CGI file is mixing so much html and python code that it becomes unwieldy.


Some people find even a 2-way separation messy.
Here's 3-way http://www.kuwata-lab.com/kwartz/ (or is it 4? Dunno...)
 
Reply With Quote
 
Νίκος
Guest
Posts: n/a
 
      06-24-2013
Στις 24/6/2013 7:37 πμ, ο/η Michael Torrie *γραψε:
>> Why use mako's approach which requires 2 files(an html template and the
>> actual python script rendering the data) when i can have simple print
>> statements inside 1 files(my files.py script) ?
>> After all its only one html table i wish to display.

> Sooner or later your needs grow to the point where your single python
> CGI file is mixing so much html and python code that it becomes unwieldy.


Yes Michale indeed, my pelatologio.py is filled with print html statemnt
and actual python code. it lloks like this:

#print all client profiles
print('''
<body background="/data/images/bluetiles.jpg">
<center><h3><font color=yellow size=5>~ *ελατολόγιο ~<br>
<font color=white size=5>
*ελάτες: <font color=red> %s </font>
Επισκευ*ς: <font color=silver> %s </font>
Χρήματα: <font color=violet> %s € </font><br><br>
<table border=5 cellpadding=5 bgcolor=black>
<th><font color=lime size=5> *ελάτης </th><th><font color=orange
size=5> Τηλ*φωνο </th>
<th><font color=tomato size=5> Επισκευ*ς </th><th><font color=cyan
size=5> Σύνολο </th>
''' % (clients, hits, money) )

for row in data:
(name, phone, hits, money) = row

print('''
<tr>
<td><center><a
href="http://superhost.gr/cgi-bin/pelatologio.py/?log=%s"> <font
color=lime size=4> %s </a></td>
<td><center><font color=orange size=4> %s </td>
<td><center><font color=tomato size=4> %s </td>
<td><center><font color=cyan size=4> %s </td>
</tr>
''' % (name, name, phone, hits, money) )
===========================================

Apart form the fact that i will need to separate the design form the
code, if i didn't is there an easiest way to print tables and forms by
using python's html generation functions and not plain print statements
that will make the code more clear?


And also in my pelatologio.py and other script i use if statements to
check if user submitted data or not so to print them on screen and then
exit, like modularization.

foe example:

if( log ):
name = log
print this and this

if( seek ):
other statements
print this and this

How will i be able to do this if i use an html template
every time an if() runs all the html will run and not a part of it.

Thanks.




--
What is now proved was at first only imagined!
 
Reply With Quote
 
rusi
Guest
Posts: n/a
 
      06-24-2013
On Monday, June 24, 2013 1:02:51 PM UTC+5:30, Νίκος wrote:

> And also in my pelatologio.py and other script i use if statements to
> check if user submitted data or not so to print them on screen and then
> exit, like modularization.
>
>
>
> foe example:
>
> if( log ):
> name = log
> print this and this
>
> if( seek ):
> other statements
> print this and this
>
>
> How will i be able to do this if i use an html template
> every time an if() runs all the html will run and not a part of it.


Every significant templating system has something for this
eg in mako
http://docs.makotemplates.org/en/lat...rol-structures

The main thing to understand is this:
- When you use straight python, by default you are 'inside python' except when inside strings (inside prints) then you are 'inside html'

- When using a templating engine, by default you are inside html, except when you escape from html into python
 
Reply With Quote
 
rurpy@yahoo.com
Guest
Posts: n/a
 
      06-25-2013
On 06/23/2013 07:44 PM, Νίκος wrote:> Why use mako's approach which requires 2 files(an html template and the
> actual python script rendering the data) when i can have simple print
> statements inside 1 files(my files.py script) ?
> After all its only one html table i wish to display.


Good question. Sometimes your way is best.

The main advantage of using templates is that the template contains
only html (mostly) and the cgi code contains only python (mostly).

The idea is that you can look at the template and see only the
kind of code (html) that affects how the page looks. With some
template systems you can edit the template files with a html
editor and do the page design visually. Even in a text editor
it is usually easier to see the how the html "works" without
spurious stuff like code.

And when you look at the cgi code, you see only the Python code
that is needed to get the variable data that is displayed in the
page without the distraction of a lot of html stuff.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CGI (read multipart form): Accept-Charset encoding error (CGI::InvalidEncoding) Stefan Fischer Ruby 2 02-23-2010 08:17 AM
Webpy and UnicodeDecodeError Oscar Del Ben Python 3 12-19-2009 03:18 AM
Recommendation for a small web framework like Perl's CGI::Applicationto run as CGI? excord80 Python 17 01-29-2009 06:02 PM
Webpy vs Django? circularfunc@yahoo.se Python 4 06-03-2008 09:11 PM
psyco+webpy a Python 3 06-26-2006 09:35 PM



Advertisments