Bringing the Web to Python (again)
Hmm, now that was a nice hiatus from blogging. Apologies to any readers were disappointed with my absence (and lost their source of life force).
So, since this blog totally needs more Python, and since I have recently been playing with a cool web tech, why not present it?
The web2py Enterprise Web Framework (found at web2py.com) is a pretty cool web framework that includes everything you need to make a good interactive web site using Python. What about Zope 3 or other similar solutions? And why am I talking about web frameworks when I built this blog from scratch using PythonPaste?
Well, at least so far as comparing web2py to Zope 3, they are completely different things. For starters, web2py differs in that, unlike Zope, it contains an "applications" folder, which can contain a series of applications that all run under the same process. This process can be hooked into a separate server using WSGI or CGI, or by simple URL rewriting.
Second, web2py requires the programmer to be much less of a "control freak" than Zope does. To exemplify, let's compare the methods of writing a "Hello, world!" applications in both.
Zope 3
First, create your package you will want to run: a helloworld directory containing an empty __init__.py to mark it as a Python package. Then, create the hello.pt page template, containing some arbitrary HTML text.
Hello, world!
So far, so good. Now, open up a file called configure.zcml (ZCML standing for Zope Content Markup Language, an XML dialect), and write the browser configuration for the particular view you're writing here.
<configure xmlns="http://namespaces.zope.org/browser"> <page name="hello" template="helloworld.pt" for="zope.app.container.interfaces.IContainer" permission="zope.Public" /> </configure>
Then, go into the Zope package-includes folder, and place an import for your project in there, in a file called, say helloworld-configure.zcml.
<include package="helloworld" />
Then, run the Zope server, and your pretty, simple, "Hello, world!" message will appear when the server is queried with the /hello URL.
Note how even for this simple example, we had to worry about the Zope interface that this view applies for, then about view permissions. Then, as soon as we start writing our own interfaces and data structures, it is recommended to separate our configuration into multiple configure.zcml files, or it becomes unmanageable.
web2py
We start by creating a helloworld directory inside web2py's applications directory. Then, create a controllers directory inside the helloworld directory. Then, create the hello.py file, which contains:
def hello(): return "Hello, world!"
Now, run the web2py server, and navigate to the /helloworld/hello/hello URL. You're done!
On top of that, web2py runs on more platforms than Zope. Plus, because of its "zero configuration" approach, the application folder can simply be packaged and distributed in order to distribute applications, without any further worries. Database, you say? web2py features an awesome DAL (Database Abstraction Layer) which allows your program native Python interfaces to whatever the underlying database is (be it SQLite, MySQL, or Oracle).
Well then, why ever use something more heavyweight, like Zope? For the same reason you would use something like web2py as opposed to a raw PHP or a simple Python scripting solution, such as mod_python. As project complexity increases, increasing tool complexity can simplify much of the possibly mistifying "pipe work" and let you devote more time on the fun stuff.
Now...
Please?
I guess I will? You can expect a more in-depth discussion of the web2py DAL (which I really love) next time.