Imagine a bunch of really smart programmers who disliked PHP and
wanted to do things a different way.  Now imagine that 30 of them each
went and did things their own way.  The ways would all be completely
different.  They would each have interesting approaches.  That's the
Python Web world.  Imagine the way you think Web development should
work.  There's probably someone out there already doing it that way.
More comments inline...

On 3/5/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> It seems that the most important concept in Python web development is the 
> least explained and hardest to understand (which makes it very hard for 
> anyone that wants to learn Python for web development).  I wonder if you all 
> might be able to help me understand it.
> I posted a thread about it here:
> http://python-forum.org/py/viewtopic.php?t=3310
> I'll also include the post here:
> I have looked at a lot of stuff related to web programming in Python, 
> including the various frameworks like Pylons. However, I am still completely 
> lost as to how the whole website development on Python works.

Pylons + Paste is pretty straight forward.  They're built on top of an
API called WSGI so that you can deploy your application any number of
ways on any number of different servers.  You're not limited to just
Apache.

This flexibility comes at the cost of having to make a decision of
which you're going to use.  In production, I simply use Apache as a
reverse proxy in front of my Pylons applications which runs under
Paste's server.  Recently, some people reported really good
performance using Nginx instead of Apache in front of the proxied
application.

> With PHP, the concept is relatively simple to understand:
> 1. You install a web server like Apache.
> 2. You install PHP, which basically outputs text files that are then served 
> to the web by Apache.
> 3. The logic of how they interact is rather simple too:
> 3a. Apache sends the GET or POST request to the PHP file and PHP has built-in 
> methods for reading the GET or POST data.
> 3b. PHP then outputs text, which Apache serves up to the web for all to see.
>
> Simple, and easy 'eh?

You can do much the same with Apache + CGI.  Remember, Python
programmers aren't always interested in the "simplest thing".  Rather,
some of us are fully committed to finding the "most powerful" thing.
That is, the thing that lets us get the most functionality with the
fewest lines of (still readable) code.

> But, I simply do not understand the concept of how Python web development 
> works.
>
> *. Reading around, I see that there are different methods for handling URL 
> requests, which implies that things work at a much lower level (and in a much 
> more complex way) than what I understand.

These days, "user-friendly" URLs are in style.  For instance, URLs
such as "http://myblog.com/2007/January/1";.  It takes code to point
these URLs at the right code.  There isn't actually a file called
1.php ;)

> *. There are many web development frameworks, but I am simply not sure what 
> they are for.

It's easy to write a Web framework, so a lot of people have done it.
I'm sure that many people will disagree, but here's a quick summary
from my point of view:

Plone - Use this if you need a content management system.

Twisted / Nevow - Use this if you need *extremely* scalable Web sites,
at the cost of having the code be harder to understand and write.  For
instance, if it's a very dumb application that has to talk to 30,000
people at *exactly the same time* on the same server, Twisted is good.
 Most "user friendly" applications probably shouldn't be written in
Twisted because the code is harder to write.

Django - Use Django if you want a simple, content-oriented Web site,
like a newspaper.

Pylons or TurboGears - Use these if you want a more flexible Web "application".

> In PHP, you can use the frameworks to more easily code PHP files to put on 
> your server; however, in the end the PHP concept is still the basic and 
> simple one described earlier.

Yes, simple, but constrained.  "If all you have is a hammer,
everything looks like a nail."

> On the other hand, with the Python frameworks it almost seems like they do 
> more, as some include a web server and stuff like ways to handle URLs. I am 
> not sure what to make of this or what it all means.

Yes, it takes some time.

> *. As I understand it, Python is not anything like PHP in that it does not 
> embed code into HTML, but works differently?

There are some projects that let you embed Python in HTML exactly like
PHP.  Most don't.  There are many approaches to the problem of
embedding some application logic in HTML.

> How do you code html pages then?

Each of the frameworks has a different approach.  Pick the one that
you like the best.

> Or do you code things differently? Do you need to use some kind of template 
> language?

Many people use templating languages.  Some people embed HTML in their
Python source files.  Some people read flat HTML files and then alter
the DOM in memory on the serve side!  It's a matter of taste.
Personally, I really like Genshi.

> If so, how do you know which one to pick?

The way the code is tied to the templating language is part of how
each of the frameworks works.  In Pylons:

Web server -> Routes -> Some controller -> Some template -> The user

> How do you use them and how do they fit into what appears to be an 
> increasingly complex design for website programming?

Increasingly complex means you may be able to get more functionality
in less code.

> Taking a look at Pylons, I see several things:
> "Models" - referring to databases
> "Templating"
> "AJAX"
> "Request Dispatching"

Pylons lets you mix and match separate libraries for all of these things.

> Ajax is not key to web programming (since it's client side javascript), so I 
> don't even have to ask about that.

Agreed.

> You can just as easily download and install ajax libraries on your own or use 
> them on static html pages.
> Models (in reference to databases) appears to just refer to the various 
> database abstraction libraries.

Agreed.

> Again, this is not part of the basic concept of web development, though it is 
> very important to have when making sites. From SQLAlchemy's site, actually 
> using such an abstraction library to access databases appears very easy and 
> straightforward.

Yep.  I use it.

> It appears that you can do this in any Python app, and is thus not unique to 
> web development.

Agreed.  Although, there are some hooks in Pylons to make using
SQLAlchemy easier.

> So, we are left with:
> "Templating"

It's a matter of taste.

> "Request Dispatching"

What do you want your URLs to look like?  How do URLs map to actual
code?  That's what these libraries solve.

> and I would like to add:
> Server

These days, fortunately, you have a lot of flexibility.  You can pick
something safe like I mentioned above, or you can do something
extreme.  You can try various things out.

> What is "Templating"?

Templating is basically any way to embed some application logic in an
otherwise static file.

> What is "Request Dispatching"?
> How do these fit into the simple web development design I gave earlier?

If you're outputting HTML, you're going to want to make some parts of
the HTML dynamic.  How do you want to do that?  Pretty much everyone
has an opinion on this one, and they all vary greatly.  I think the
two most popular approaches are summarized in Genshi and Mako.  Have a
look at some examples of each, and you will know right away which you
like better.  Mako is simpler and faster.  Genshi has more interesting
tricks that may make you more productive.

> Are they key components, or optional stuff like Ajax and DB Abstraction?

They're optional.  You can always just embed Python in your Python
modules using triple quoted strings.  Of course, many people might not
like you if you do that ;)

> It seems the key parts that I am trying to understand are least explained?

It's difficult to get the big picture of the Python Web world.

>Basically it is this?

You are close to "getting it."

> *. How do you setup a webserver for Python web development (I am talking 
> about a robust, high capacity, high user system like the Apache/PHP setup I 
> described earlier -- in other words, one that you would use for a high 
> profile website; not some test server)?

There are many answers to that.  Like I said, I'm using Paste's Web
server.  If you need extreme speed, there are other options.  Most of
the time, you care a lot more about how fast your coders get things
done than how fast your Web servers work.

> *. How do you create webpages in Python -- specifically the processing of 
> GET,POST,COOKIES, and then the displaying of HTML, XML, etc...?

Each of the frameworks does things differently.  Using the choices I
gave above (Plone, Twisted, Pylons, etc.), pick the one that matches
your application, and then learn how that framework works.

> Do you have to use the CGI method, as opposed to PHP's mixing of HTML and 
> code?

CGI is actually an API that a Web server uses to talk to a script.  It
is not the same thing as a script that contains HTML.  You can use CGI
in Python, but CGI is inherently slow because it means the Web server
forks a copy of the script.  Anyway, CGI is an implementation detail.
Embedding Python in HTML is a "style" of templating language.  You can
do that too if you want, although it's considered somewhat unpopular
these days.  If that's what you want, Mako is probably closer to what
you're looking for.

> Are there are streamlined and better ways of doing this?

Of course ;)  There are many.

> I forsee Python's use of whitespace to be a problem in PHP like coding.

Yes.  Different templating languages handle this in different ways.

> Does creating webpages mean I have to use some kind of library to make it 
> easier.

Yes, that's what the frameworks are for.

> If so, what are these libraries? Are they the templating languages?

Templating languages are just one part.  Most frameworks let you pick
whichever templating language you like these days.

> How do you know which one to choose?

You ask questions.  You research.  You pick one.  You try out a sample
application.  You see if it met your needs.  Then you write your real
application.

> * What is this "Request Dispatching" that I hear about?

It's figuring out which URL gets mapped to which piece of code.

> Does Python web development not handle file names in the same way as PHP or 
> Perl? Can you explain?

In most projects these days, you have more control over how your URLs work.

Whew!  That was a lot!  I'm not even going to bother editing my
comments, so please excuse any typos!

Happy Hacking!
-jj

-- 
http://jjinux.blogspot.com/

_______________________________________________
Paste-users mailing list
[email protected]
http://webwareforpython.org/cgi-bin/mailman/listinfo/paste-users

Reply via email to