Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Web development with Python 3.1

Reply
Thread Tools

Re: Web development with Python 3.1

 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      10-28-2009
Dotan Cohen a écrit :
>>> I have no idea what reverse url generation is.

>> It's a mean to avoid hard-coding urls in your application code - the url is
>> fully generated by the 'url mapping' system.
>>

>
> I don't need that, see below.
>
>
>>> I assume that the user
>>> will call http://example.com/path/to/script.py...llo&var2=world
>>>
>>> script.py would live in /home/user/site-name/public_html/path/to/
>>>

>> Now try to use apache url rewrites to use "nice urls" (like,
>> "/<section-name>/<sub-section-name>/<article-name>" instead of
>> "path/to/script-serving-articles.wtf?id=42"). Good luck.
>>

>
> Actually, currently in the example url
> http://example.com/path/to/script.py...llo&var2=world the script
> is /home/user/site-name/public_html/path/ (with no filename extension)
> and the script.py is actually page.html which is an arbitrary,
> descriptive string and not part of the script filename.


Better than the first example wrt/ SEO, but this still ties your "url
space" to implementation AFAICT.

>
>>> I would prefer to output everything from <html> to </html> with print
>>> statements. I don't want some framework wrapping my output in tags, I
>>> want the tags to be part of the output.

>> You're confusing the framework and the templating system. The framework by
>> itself won't wrap your "output" in anything, nor even forces you to "output"
>> HTML.

>
> This is what I was hoping to hear. I don't remember the details, but I
> had a hard time outputting the HTML myself in Django. I will play with
> it again soon if I get the chance.
>
>
>> Also FWIW, it's HTTP, so we don't have "outputs", we have HTTP
>> responses.
>>

>
> Clearly. I was referring to stdout being send to the browser as the
> http response.


s/browser/web server/ - it's the web server that reads your app's stdout
and send it (as is or not FWIW) back to the client. And this "output to
stdout" thingie is just the ipc scheme used for CGI - there are other
possible interfaces between the application code and the web server.

> I think I mentioned that, but I apologize for being
> unclear.


It's not that it was unclear, but that it's innaccurate. "outputting to
stdout" is an implementation detail, and should not be exposed at the
applicative code level. Dealing with appropriate abstraction - here, an
HttpResponse object - is far better (well, IMHO of course... - standard
disclaimers, YMMV etc).

>>> Yes, why not?

>> Let's say I want to reuse your app with a different presentation. Do I have
>> to fork the whole application and send my HTML coder dig into your
>> applicative code just to rewrite the HTML parts ?
>>

>
> No, just replace two files. In that manner it is templating. Again, an
> example from the current PHP code:
>
> <?php
> $variables_for_header="blah";
> include "/path/to/header.inc";
>
> // content here
>
> include "/path/to/footer.inc";
> ?>



Sorry, but all I can "replace" here are the header and footer - if I
want to generate a different markup for the "content here" part, I have
to modify your applicative code. I've written web apps that way myself
(some 7 years ago), and still have to maintain some web apps written
that way, you know...



>
>>> Like I said before, I don't want to have to maintain the functions
>>> that turn the HTTP environment into Python variables,

>> "HTTP environment" ???
>>
>> Oh, you mean HTTP requests etc... Well, the cgi module already provides some
>> basic support here.
>>

>
> Does it? I will look into that. I assume that basic support means
> making the cookie, GET and POST variables easily accessible.


GET and POST at least - I don't exactly remember how it works for cookies.

> I google,
> but cannot find any exapmles of this online.


Well, no one in it's own mind would still use CGI (except perhaps for
very trivial stuff) when you have way better solutions.

>
>>> or the code that
>>> manages database connections. Functions that escape data to be sent to
>>> MySQL (to prevent sql injection) would be nice.

>> Any DB-API compliant module already provide this - granted you use it
>> correctly.
>>

>
> I know. This is will use correctly, I promise!
>
>
>>> Other than that, it's
>>> all on me.

>> Your choice...
>>

>
> Hey! Since when am I not under attack?


Mmm... yes, I tend to be a bit offensive sometimes - please don't take
it personnaly and forgive me for my lack of social skills. What I
express are of course _my_ opinions (most of them based on experience
but that's not the point), and I've never killed anyone for having a
different POV - even if I'm pretty sure they are wrong !-)
 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      10-28-2009
Dotan Cohen a écrit :
>>>>> While I know that to be true in the general sense, from what I've
>>>>> looked at Django and other frameworks it seems that the web frameworks
>>>>> push the coder to use templates, not letting him near the HTML.
>>>> Well... there must be a reason, for sure... No ?
>>>>
>>> ֹYes, but I don't like it.

>> Why so ? What's your problem with templating systems, exactly ?
>>

>
> They don't give me the control over the HTML that I would like. I'm
> probably stuck in 1998 when they didn't exist, but I like to write the
> HTML myself.


<once-again>

*Why* on earth do you think using a templating system will give you any
less control on the generated HTML ?

</once-again>


>
>>>>> Django and the other frameworks seem to
>>>>> force the user to use templates.
>>>> Not at all. Nothing prevents you from shooting yourself in the foot and
>>>> generating HTML "by hand" in your view functions (or request handler
>>>> methods
>>>> etc, depending on the framework).
>>> How is this done in Django, then?

>> def index(request):
>> unmaintanable_html = """
>> <html>
>> <head>
>> <title>Index</title>
>> </head>
>> <body>
>> <h1>Embedded HTML is a PITA</h1>
>> <p>but some like pains...</p>
>> </body>
>> </html>
>> """
>> return HttpResponse(unmaintanable_html)
>>

>
> And if I need to add a variable or three in there?


- first solution : use string formatting

python 2.x example:

unmaintanable_html = """
<html>
<head>
<title>%(title)s</title>
</head>
<body>
<h1>%(h1)s</h1>
<p>%(text)s</p>
</body>
</html>
"""

data = dict(
title="index",
h1="Poor man's templating",
text="Won't get you very far..."
)

return HttpResponse(unmaintanable_html % data)


- second solution: do basically the same thing with a template system -
which will give you much more power and options...


> Static HTML I can
> do without Python.


Sorry, I forgot to setup a database etc for this example !-)


 
Reply With Quote
 
 
 
 
Dotan Cohen
Guest
Posts: n/a
 
      10-28-2009
> *Why* on earth do you think using a templating system will give you any less
> control on the generated HTML ?
>


Because I am wrong. I have already come to that conclusion.

I know that Python is far enough developed that if I feel that I am
fighting it, then _I_ am in the wrong, not Python. However, I still
need to see that Python can do what I need in a comfortable fashion.


>> And if I need to add a variable or three in there?

>
> - first solution : use string formatting
>
> python 2.x example:
>
> unmaintanable_html = """
> *<html>
> *<head>
> * *<title>%(title)s</title>
> *</head>
> *<body>
> * *<h1>%(h1)s</h1>
> * *<p>%(text)s</p>
> *</body>
> *</html>
> """
>
> data = dict(
> *title="index",
> *h1="Poor man's templating",
> *text="Won't get you very far..."
> *)
>
> return HttpResponse(unmaintanable_html % data)
>


That's fine for single variables, but if I need to output a table of
unknown rows? I assume that return means the end of the script.
Therefore I should shove the whole table into a variable and then copy
that variable to the array "data"?


> - second solution: do basically the same thing with a template system -
> which will give you much more power and options...
>


I will look further into the Django templates, I promise. But I would
still like to know how to work with Python proper.


>> Static HTML I can
>> do without Python.

>
> Sorry, I forgot to setup a database etc for this example !-)
>


I meant that I would like to see how to output variables, which you have shown.

Thanks. I am stubborn, but I am willing to learn.


--
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      10-28-2009
Dotan Cohen a écrit :
>>> I insist on handling the HTML myself.

>> I just don't get how embedding HTML in applicative code - a well-known
>> antipattern FWIW - relates to "handling the HTML yourself". Can you

*please*
>> explain how a templating system prevents you from "handling the HTML"
>> yourself.
>>

>
>>From the little that I played with Django, it looked like I would (for

> the sake of argument I am just using the term output, but the
> intention should be clear) output "Hello, world!" and Django would
> mangle that into <html><body><p>Hello, world!</p></body></html>.


I don't know how you got that result - but it's certainly *not* what I
get here.

> It
> did not look like I had any control over the tags at all.


I can tell you you have full control over the whole HTTP response's
content and headers. And FWIW, this has nothing to do with templating
systems.

>
>>>> And
>>>> even if not, what you will do is ... code your own webframework.
>>> That is why I am looking for a class that handles the backend stuff,
>>> but lets _me_ handle the HTML.

>> For God's sake : *why on earth do you believe that using a

templating system
>> will make you loose _any_ control on the HTML code ???*
>>

>
> Because that's what I've seen of them.


I use Django's templates to generate any kind of textual content - not
only HTML. I could even use them to generate Python source code. And
that's true for quite a few other templating systems around (Mako,
Jinja, Cheetah, just to name a few).

>
>>>> And at least pylons/TG2 lets you return whatever you want instead,

as a
>>>> string. Not via "print" though - which is simply only for CGI, and no
>>>> other
>>>> means (e.g. mod_wsgi) of python-web-programming.
>>>>
>>> I am trying to follow you here. What is "return whatever you want"?
>>> Return HTML to stdout to be sent to the browser?

>
>> please leave stdout out of here - it's HTTP, so what we have are HTTP
>> requests and HTTP responses - not stdin nor stdout. Using these

streams as a
>> mean of communication between the web server and the applicative code is
>> just plain old low-level GCI stuff. Your application code shouldn't

have any
>> knowledge of this implementation detail - it should receive (a suitable
>> representation of) an HTTP request, and generate (a suitable

representation
>> of) an HTTP response.

>
> Can you provide a code example of printing the variable "torvalds" in
> the GET request?
> http://example.com/page.py?torvalds=tux
> Should return this:
> <html><body><p>tux</p></body></html>
>
> And something similar for a POST request?



Using which framework ? If it's about Turbogears, I don't use it so I
can't answer. If it's using Pylons, you can use almost whatever
templating system you want - or not use any. If it's with Django, do you
want it with or without the templating system ?

> I hate to ask for code, but this conversation would be easier that way.


Mmmm.... May I suggest that you first read the relevant documentation
for the aforementionned frameworks ?

Ok, here's a Django example without a template:

# views.py
def index(request):
torvalds = request.GET.get("torvalds", "No torvalds here")
return HttpResponse("<html><body>%s</body></html>" % torvalds)


and here's an exemple using a template:

# templates/index.html

<html>
<body>
{{ torvalds }}
</body>
</html


# views.py
def index(request):
torvalds = request.GET.get("torvalds", "No torvalds here")
return render_to_response("index.html", dict(torvalds=torvalds))



 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      10-28-2009
Dotan Cohen a écrit :
>> *Why* on earth do you think using a templating system will give you any less
>> control on the generated HTML ?
>>

>
> Because I am wrong. I have already come to that conclusion.
>
> I know that Python is far enough developed that if I feel that I am
> fighting it, then _I_ am in the wrong, not Python. However, I still
> need to see that Python can do what I need in a comfortable fashion.
>
>
>>> And if I need to add a variable or three in there?

>> - first solution : use string formatting
>>
>> python 2.x example:
>>
>> unmaintanable_html = """
>> <html>
>> <head>
>> <title>%(title)s</title>
>> </head>
>> <body>
>> <h1>%(h1)s</h1>
>> <p>%(text)s</p>
>> </body>
>> </html>
>> """
>>
>> data = dict(
>> title="index",
>> h1="Poor man's templating",
>> text="Won't get you very far..."
>> )
>>
>> return HttpResponse(unmaintanable_html % data)
>>

>
> That's fine for single variables, but if I need to output a table of
> unknown rows?


What do you think templates are for ?

> I assume that return means the end of the script.


It means that this function returns an HttpResponse object.

> Therefore I should shove the whole table into a variable and then copy
> that variable to the array "data"?


If you really love feeling the pain, that's a possible solution. I for
one would rather use a template... But once again, your choice !-)

>
>> - second solution: do basically the same thing with a template system -
>> which will give you much more power and options...
>>

>
> I will look further into the Django templates, I promise. But I would
> still like to know how to work with Python proper.


Using the appropriate tool for the job is "proper Python". The
appropriate tool for generating formatted text with static and dynamic
parts is a template system. Good news, there are quite a few available
in Python.

 
Reply With Quote
 
Dotan Cohen
Guest
Posts: n/a
 
      10-28-2009
>> It
>> did not look like I had any control over the tags at all.

>
> I can tell you you have full control over the whole HTTP response's content
> and headers. And FWIW, this has nothing to do with templating systems.
>


That's good to know. Actually, it's more that good: I am looking into
Django again.


>> Can you provide a code example of printing the variable "torvalds" in
>> the GET request?
>> http://example.com/page.py?torvalds=tux
>> Should return this:
>> <html><body><p>tux</p></body></html>
>>
>> And something similar for a POST request?

>
>
> Using which framework ? If it's about Turbogears, I don't use it so I can't
> answer. If it's using Pylons, you can use almost whatever templating system
> you want - or not use any. If it's with Django, do you want it with or
> without the templating system ?
>


I'd love to see the non-templating Pylons example, and/or the Django
example. Better yet, a link to example code or the appropriate chapter
of TFM would be welcome. So far as I can tell, the fine manual does
not cover non-template output.


>> I hate to ask for code, but this conversation would be easier that way.

>
> Mmmm.... May I suggest that you first read the relevant documentation for
> the aforementionned frameworks ?
>


Yes, please! Where in the Django docs is the relevant chapter?


> Ok, here's a Django example without a template:
>
> # views.py
> def index(request):
> * torvalds = request.GET.get("torvalds", "No torvalds here")
> * return HttpResponse("<html><body>%s</body></html>" % torvalds)
>
>


Allright, but what if I need to output in the middle of the page? Say,
one row of a table that is neither the end nor beginning of the code?
I ask because the construct "return" makes me believe that this is not
good for this purpose.


> and here's an exemple using a template:
>
> # templates/index.html
>
> <html>
> <body>
> {{ torvalds }}
> </body>
> </html
>
>
> # views.py
> def index(request):
> * torvalds = request.GET.get("torvalds", "No torvalds here")
> * return render_to_response("index.html", dict(torvalds=torvalds))
>


Again, how could I output something in the middle of the page with the
templates?

--
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
 
Reply With Quote
 
Dotan Cohen
Guest
Posts: n/a
 
      10-28-2009
> I've already given you that for TG:
>
> class RootController(BaseController):
>
> * @expose()
> * def page(self, torwalds=None):
> * * * return "<html><body><p>%s</p></body></html>" % (torwalds if torwalds
> else ""
>


Does return mean that this could not be used in the middle of a page?


> Of course nobody would do it that way. Instead, you'd define a template
>
> <html
> xmlnsy='http://genshi.edgewall.org/'><body><p>${torvalds}</p></body></html>
>
>
> And then your code becomes:
>
>
>
> * @expose("your.template.path")
> * def page(self, torwalds=None):
> * * * return dict(torwalds=torwalds)
>
>
> Separation of code from HTML-code. Which most people agree is a good thing.
> And no worries about having to make torwalds a string.
>


Yes, I like to separate HTML from code. However, often I need to
output something in the middle of the page. How could I do that with a
template?


> Now let's say you want torwalds to be not-empty, and a number:
>
> * @expose("your.template.path")
> * @validate(dict(torwalds=Int(not_empty=True)),
> error_handler=some_error_handler_controller_action )
> * def page(self, torwalds=None):
> * * * return dict(torwalds=torwalds)
>


That is nice, I will read more about the error_handler functions. Thanks.


--
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
 
Reply With Quote
 
Dotan Cohen
Guest
Posts: n/a
 
      10-28-2009
>> Actually, currently in the example url
>> http://example.com/path/to/script.py...llo&var2=world the script
>> is /home/user/site-name/public_html/path/ (with no filename extension)
>> and the script.py is actually page.html which is an arbitrary,
>> descriptive string and not part of the script filename.

>
> Better than the first example wrt/ SEO, but this still ties your "url space"
> to implementation AFAICT.
>


Yes, but for the purposes of my needs this is fine and preferable. SEO
was in fact the reason that I started doing this.


>> Clearly. I was referring to stdout being send to the browser as the
>> http response.

>
> s/browser/web server/ - it's the web server that reads your app's stdout and
> send it (as is or not FWIW) back to the client.


As is, in my case. Actually, what use case is there for having Apache
reprocess the HTML output of the script?


> *And this "output to stdout"
> thingie is just the ipc scheme used for CGI - there are other possible
> interfaces between the application code and the web server.
>


Other possible interfaces between the application code and the web
server? Maybe you are referring to the interface with the database
server? Enlighten me, please! I am not a programmer by trade, only by
hobby.


>> I think I mentioned that, but I apologize for being
>> unclear.

>
> It's not that it was unclear, but that it's innaccurate. "outputting to
> stdout" is an implementation detail, and should not be exposed at the
> applicative code level. Dealing with appropriate abstraction - here, an
> HttpResponse object - is far better (well, IMHO of course... - standard
> disclaimers, YMMV etc).
>


I see. I believe that is called Dotan's Razor: a slight inaccuracy
saves a lengthy explanation.


> Sorry, but all I can "replace" here are the header and footer - if I want to
> generate a different markup for the "content here" part, I have to modify
> your applicative code. I've written web apps that way myself (some 7 years
> ago), and still have to maintain some web apps written that way, you know....
>


Quite so, I though that is what you wanted. Yes, the HTML is
hard-coded into the script. I am learning to abstract and even use
object-oriented approaches, though.


>> Does it? I will look into that. I assume that basic support means
>> making the cookie, GET and POST variables easily accessible.

>
> GET and POST at least - I don't exactly remember how it works for cookies..
>


That much I should be able to google when I get to it.


>> I google,
>> but cannot find any exapmles of this online.

>
> Well, no one in it's own mind would still use CGI (except perhaps for very
> trivial stuff) when you have way better solutions.
>


What I am doing _is_ trivial. However, I thank you for explaining my
options and I will take a second look at them.


>> Hey! Since when am I not under attack?

>
> Mmm... yes, I tend to be a bit offensive sometimes - please don't take it
> personnaly and forgive me for my lack of social skills. What I express are
> of course _my_ opinions (most of them based on experience but that's not the
> point), and I've never killed anyone for having a different POV - even if
> I'm pretty sure they are wrong !-)
>


Be offensive! I take no offence, I understand that your goal is to
help me. I am a hard learner: I recognize that you are teaching me the
better way, but I need convincing as to why it is better. "Bruno said
so" is good, but "saves you coding time down the line" is a lot more
convincing!


--
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
 
Reply With Quote
 
Rami Chowdhury
Guest
Posts: n/a
 
      10-28-2009
On Wed, 28 Oct 2009 12:42:17 -0700, Dotan Cohen <(E-Mail Removed)>
wrote:

>> I've already given you that for TG:
>>
>> class RootController(BaseController):
>>
>> * @expose()
>> * def page(self, torwalds=None):
>> * * * return "<html><body><p>%s</p></body></html>" % (torwalds if
>> torwalds
>> else ""
>>

>
> Does return mean that this could not be used in the middle of a page?
>

[snip]
> Yes, I like to separate HTML from code. However, often I need to
> output something in the middle of the page. How could I do that with a
> template?
>


What do you mean by "in the middle of the page"? Do you mean, for
instance, the behavior of "middle.php" in the following PHP example:

<?php

include_once("beginning.inc.php");

include_once("middle.php");

include_once("end.inc.php");

?>

Is that what you are after?



--
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --
Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
 
Reply With Quote
 
Dotan Cohen
Guest
Posts: n/a
 
      10-28-2009
> What do you mean by "in the middle of the page"? Do you mean, for instance,
> the behavior of "middle.php" in the following PHP example:
>
> <?php
>
> include_once("beginning.inc.php");
>
> include_once("middle.php");
>
> include_once("end.inc.php");
>
> ?>
>
> Is that what you are after?
>


Yes, that is what I am after. For instance, if one were to look at the
source code of http://dotancohen.com they would see "<!-- / HEADER
-->". All the HTML up to that point was output by bigginin.inc.php.
Similarly, near the end exists "<div class="bottomfiller">", all the
code from there is generated by end.inc.php. These two files are
included in every page of the site.

--
Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
 
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
Web Application development vs windows client development cabernet123@hotmail.com ASP .Net 0 11-17-2005 12:09 AM
Lisp development with macros faster than Python development?.. seberino@spawar.navy.mil Python 37 07-11-2005 02:10 PM
Web Clients, the role of ASP.NET and the Future of Web Development and Web Standards Guadala Harry ASP .Net 9 11-06-2004 03:05 AM
development environment architecture for ASP.NET development team Akhlaq Khan ASP .Net 4 09-27-2004 01:33 PM
Re: Development best practices and knowing when to exercise control over development Kevin Spencer ASP .Net 2 08-06-2003 09:33 PM



Advertisments