Making a pass form cgi => webpy framework

  • Thread starter Îίκος
  • Start date
Î

Îίκος

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

rurpy

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

Îίκος

Στις 23/6/2013 5:57 μμ, ο/η (e-mail address removed) έγÏαψε:
On 06/23/2013 07:01 AM, Îίκος wrote:> Hello, as you all know i'am using cgi method for my web script.

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

rurpy

Στις 23/6/2013 5:57 μμ, ο/η (e-mail address removed) έγÏαψε:
On 06/23/2013 07:01 AM, Îίκος wrote:
Hello, as you all know i'am using cgi method for my web script.
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.
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=(templates)

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

Îίκος

Στις 24/6/2013 1:29 πμ, ο/η (e-mail address 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.
 
M

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

rusi

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

Îίκος

Στις 24/6/2013 7:37 πμ, ο/η Michael Torrie έγÏαψε:
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.
 
R

rusi

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/latest/syntax.html#control-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
 
R

rurpy

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

Joel Goldstick

On 06/23/2013 07:44 PM, Îίκος wrote:> Why use mako's approach which
requires 2 files(an html template and the

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.

I haven't tried webpy but I have used django. django has a tutorial that
takes a couple of hours to set up and go through completely. Its not just
reading, its hands on trying out a small website. It gives a very good
understanding of what the framework offers, and how difficult (or easy!) it
is to use. If webpy has a similar tutorial, I would start there, or try
django. After the tutorial, the discussion becomes a lot more concrete and
less theoretical as to whether that platform would be helpful
 
Î

Îίκος

Στις 25/6/2013 9:53 μμ, ο/η Joel Goldstick έγÏαψε:
I haven't tried webpy but I have used django. django has a tutorial
that takes a couple of hours to set up and go through completely. Its
not just reading, its hands on trying out a small website. It gives a
very good understanding of what the framework offers, and how difficult
(or easy!) it is to use. If webpy has a similar tutorial, I would start
there, or try django. After the tutorial, the discussion becomes a lot
more concrete and less theoretical as to whether that platform would be
helpful

Yes, because at the time being i really dont seem to even have a lcue of
what i web framework is and how it si compred to python cgi or to other
templating systems(mako)

I mean if its htere to make my life and scripts easier to maintain then
i should use it, but if its gonna make my scripts more compilcated then
i dont wish to use it. i tired to read some examples of webpy and django
but i didnt understand them at all.

Also i saw some youtube videos but i just dont grasp their philosophy
and why they are being used.

Thats why i hate docs they always leave me messed up and my basic
questions unanswered.



What is now proved was at first only imagined!
 
Î

Îίκος

Στις 25/6/2013 9:00 μμ, ο/η (e-mail address removed) έγÏαψε:
On 06/23/2013 07:44 PM, Îίκος wrote:> Why use mako's approach which requires 2 files(an html template and the

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.

I still cant make up my mind what i'm to use.

a) keep my existing Python cgi way that embed print ''' statements
within python code to displays mostly tables?

b) use a mentioned html templating schema like i.e. mako?

c) use a micro web framework like flask?

Can someone please provide simple example of (b) and (c) to help me
realizes the differences of html tamplates compared to frameworks?

Because the latter is still unclear to me as to what exactly is and
behave so i cant tell if i'm to use it or not.
 
C

Cameron Simpson

| a) keep my existing Python cgi way that embed print ''' statements
| within python code to displays mostly tables?

I'd argue against this approach. Like hand constructing SQL, this
is rife with opportunity to make syntax errors, either outright by
mistyping HTML or accidentally by badly escaping content that is
not supposed to be HTML but happens to contain HTML marker characters
like "<".

When I generate HTML I usually make a structure of lists and dicts
that gets converted to HTML. Someone doing a nontrivial amount of
HTML would use a templating system of some kind I imagine; I don't
generate HTML very often.

Pick a simple framework or templating engine and try it. I have no
recommendations to make in this area myself.

Cheers,
--
Cameron Simpson <[email protected]>

I'm behind a corporate Exchange Server which seems to have
changed recently to converting everything it sees to HTML.
How embarrassing. - Dan Espen <[email protected]>
 
Î

Îίκος

Στις 28/6/2013 2:08 πμ, ο/η Cameron Simpson έγÏαψε:
| a) keep my existing Python cgi way that embed print ''' statements
| within python code to displays mostly tables?

I'd argue against this approach. Like hand constructing SQL, this
is rife with opportunity to make syntax errors, either outright by
mistyping HTML or accidentally by badly escaping content that is
not supposed to be HTML but happens to contain HTML marker characters
like "<".

When I generate HTML I usually make a structure of lists and dicts
that gets converted to HTML. Someone doing a nontrivial amount of
HTML would use a templating system of some kind I imagine; I don't
generate HTML very often.

Pick a simple framework or templating engine and try it. I have no
recommendations to make in this area myself.

Can you explain to me the difference of the former and latter?
 
R

Robert Kern

Στις 28/6/2013 2:08 πμ, ο/η Cameron Simpson έγÏαψε:

Can you explain to me the difference of the former and latter?

A templating engine takes your data and applies it to templates that you have
written to generate the final HTML that is sent to the web browser.

A web framework is a library that provides tools and a way of structuring your
that makes it easier to write a web application. Web frameworks typically
include a templating engine or provide support for working with external
templating engines. In addition, web frameworks provide many other services,
like routing URLs to specific parts of your code, managing pools of database
connections, handling web sessions, validating form data, and connecting your
code to many different ways of deploying web applications without having to
rewrite your code.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
Î

Îίκος

Στις 28/6/2013 12:35 μμ, ο/η Robert Kern έγÏαψε:
A templating engine takes your data and applies it to templates that you
have written to generate the final HTML that is sent to the web browser.

A web framework is a library that provides tools and a way of
structuring your that makes it easier to write a web application. Web
frameworks typically include a templating engine or provide support for
working with external templating engines. In addition, web frameworks
provide many other services, like routing URLs to specific parts of your
code, managing pools of database connections, handling web sessions,
validating form data, and connecting your code to many different ways of
deploying web applications without having to rewrite your code.

I see, your explanation started to make things clearer to me.
What is the easiest and simplest web framework you advise me to use?

Also please provide a well written and simple tutorial i can read upon.
Thank you.
 
R

Robert Kern

Στις 28/6/2013 12:35 μμ, ο/η Robert Kern έγÏαψε:

I see, your explanation started to make things clearer to me.
What is the easiest and simplest web framework you advise me to use?

Also please provide a well written and simple tutorial i can read upon.
Thank you.

I will not advise you to do anything. I will point you to a web framework that I
happen to like, but you will need to read its documentation to decide if it is
right for you. I cannot provide any help in installing or using it.

http://flask.pocoo.org/
http://flask.pocoo.org/docs/tutorial/

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
R

rusi

Στις 28/6/2013 12:35 μμ, ο/η Robert Kern έγÏαψε:
I see, your explanation started to make things clearer to me.
What is the easiest and simplest web framework you advise me to use?

Here's a picture of the web-development scene as I see it:

At one extreme there are mega-frameworks like django.
At the other one can write everything as you have been trying.

Both these can be unsatisfactory (as you are discovering!)

In between is a line which follows from Robert Kern's suggestions.

So my advise is to understand and follow these suggestions. IOW take each of the items mentioned here (starting templating):

1. Now run a web-search for that.
2. See if it looks of any use to you
3. Check the available libraries
4. Install and try out* something that looks simple
5. When stuck -- either concepts or details -- come back here and ask

* Not on a live server!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top