Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #49023
| From | Νίκος <nikos@superhost.gr> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Making a pass form cgi => webpy framework |
| Date | 2013-06-24 04:44 +0300 |
| Organization | GRNET - Greek Research and Technology Network |
| Message-ID | <kq889f$vnl$1@news.grnet.gr> (permalink) |
| References | <kq6rip$51m$1@news.grnet.gr> <2978d13b-ca0d-4f3b-bd62-a2fcf346c46a@googlegroups.com> <kq73fg$51m$2@news.grnet.gr> <6624335b-0db7-48d8-897c-be7c960a253b@googlegroups.com> |
Στις 24/6/2013 1:29 πμ, ο/η rurpy@yahoo.com έγραψε:
> 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!
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Making a pass form cgi => webpy framework Νίκος <nikos@superhost.gr> - 2013-06-23 16:01 +0300
Re: Making a pass form cgi => webpy framework rurpy@yahoo.com - 2013-06-23 07:57 -0700
Re: Making a pass form cgi => webpy framework Νίκος <nikos@superhost.gr> - 2013-06-23 18:15 +0300
Re: Making a pass form cgi => webpy framework rurpy@yahoo.com - 2013-06-23 15:29 -0700
Re: Making a pass form cgi => webpy framework Νίκος <nikos@superhost.gr> - 2013-06-24 04:44 +0300
Re: Making a pass form cgi => webpy framework Michael Torrie <torriem@gmail.com> - 2013-06-23 22:37 -0600
Re: Making a pass form cgi => webpy framework rusi <rustompmody@gmail.com> - 2013-06-23 21:49 -0700
Re: Making a pass form cgi => webpy framework Νίκος <nikos@superhost.gr> - 2013-06-24 10:32 +0300
Re: Making a pass form cgi => webpy framework rusi <rustompmody@gmail.com> - 2013-06-24 08:51 -0700
Re: Making a pass form cgi => webpy framework rurpy@yahoo.com - 2013-06-25 11:00 -0700
Re: Making a pass form cgi => webpy framework Joel Goldstick <joel.goldstick@gmail.com> - 2013-06-25 14:53 -0400
Re: Making a pass form cgi => webpy framework Νίκος <nikos@superhost.gr> - 2013-06-26 08:31 +0300
Re: Making a pass form cgi => webpy framework Νίκος <nikos@superhost.gr> - 2013-06-27 16:32 +0300
Re: Making a pass form cgi => webpy framework Cameron Simpson <cs@zip.com.au> - 2013-06-28 09:08 +1000
Re: Making a pass form cgi => webpy framework Νίκος <nikos@superhost.gr> - 2013-06-28 06:38 +0300
Re: Making a pass form cgi => webpy framework Robert Kern <robert.kern@gmail.com> - 2013-06-28 10:35 +0100
Re: Making a pass form cgi => webpy framework Νίκος <nikos@superhost.gr> - 2013-06-28 13:15 +0300
Re: Making a pass form cgi => webpy framework Robert Kern <robert.kern@gmail.com> - 2013-06-28 11:30 +0100
Re: Making a pass form cgi => webpy framework rusi <rustompmody@gmail.com> - 2013-06-28 07:28 -0700
csiph-web