building web pages with python introduction

Introduction

About This Article

Building Web Pages with Python is just that -- a series about how to create web pages using the Python language. It's something I've working on off and on for a few months now, and it's finally done. Frameworks are a total buzz on the Internet right now. Within the past two years numerous ones in several different languages been created. I've tried a lot of them and many are very nice. (I'll even go further to say that I think Django is one of the best things happening on the Internet right now. The creators have such a great attitude toward the web, programming, and ethics -- and that makes their project all the better. Django is a little lacking in documentation, though. I'm totally stuck on custom Form Manipulators... update: I'm getting better with the custom manipulators. They're not so bad.) However, just because several hundred frameworks have sprung up recently, that doesn't mean I have to use one to write web pages.

There are several different libraries and modules written in Python for use in making web pages. I wanted to experiment with as many of these as I could and see if I could find my own method of using Python to make web pages.

I wouldn't call what I've come up with a framework -- there's not enough glue between the components to make everything work seamlessly together. I just like to think of this as an old fashioned method to create web pages.

What I'll be Building

Rather than just rattle off what tools and libraries I've chose to use, I'll be creating a simple web application. The application is called Notebook, which based off of Writeboard. If you've never heard, or used, Writeboard, it's a simple application that allows a user to write ideas down. After you've saved your notes, you can go back and edit them later. The changes and revisions are kept on file so you can go back and compare later.

Notebook will be extremely simple. It'll have an index page that lists the Notes and Categories that've been created, allow you to enter and revise new Notes and Categories, and finally, delete those items. Each time you save a note, a Revision is created where you can view and compare your past changes. The text will support Markdown format.

Tools and Components to Use

Some of the tools and components I've chose to use are pretty unconventional, but in the end, they were just what I was looking for. I wanted to use simple pieces that gave the application little or no overhead and kept the code as simple as possible.

For the database, I chose SQLite. I was originally using MySQL, but I wanted to try SQLite after having just finished reading Apress's SQLite book.

I chose SQLAlchemy for the Python database module. I was using SQLObject originally, but SQLAlchemy had much, much more documentation. Plus, I thought its ORM features were a lot better. Speaking of ORM -- I started using an ORM abstraction layer for this application, but realized that simple SQL queries and functions were much more easier to maintain and flexible. Also by using a package SQLAlchemy rather than a language specific library I have the ability to go back to MySQL or even PostgreSQL with little or no changes to the code.

I ended up going with FormEncode for forms. This was the hardest component to pick. I looked at FormEncode, FormKit, and one other that started with an A but I seem to have lost the link... FormKit has good documentation, but not very flexible with the code. FormEncode is horribly documented, but easy to work with. Unfortunately, the way I'm using FormEncode has my application spit out a Warning that it's deprecated and to use htmlfill directly. I've found no examples or documentation on how to use htmlfill, so if anyone could supply this, I would be very grateful.

When I worked with PHP, I loved Smarty. I was glad to see Django use a similar method with their templates. It seems someone else equally loves Django templates as much as I -- even enough to make their own standalone version called Jinja. Once I saw this, it instantly became my choice for a template.

Finally, the piece that ties it all together. Originally, I used Web.py. However, after a while I began using less and less of Web.py's features. First it was the database layer (It doesn't play nice with SQLite at this time), then the form library, and then the template library. The Storify class started to get in my way and I never liked the Iterbetter datatype. Basically, all I was using was the URL mapping feature.

The same guys who created Jinja also have a project called Colubrid. They describe it as a "WSGI publisher that simplifies python web development". Basically, they provide a bunch of different ways to map URLs to Python classes. One way is by using the Web.py method. There's also a bunch of others.

Moving my code from Web.py to Colubrid was fairly simple. Learning the application had some ups and downs and even a bit of stupidity and embarassment. But in the end, Colubrid is a great, minimal Python library for mapping URLs to code.

To publish the application on the web, I used standard Apache, FastCGI, and Flup. I really don't like the idea of using FastCGI, so I'm going to see if I can get it to work with mod_python sometime soon.

So there they are. These are the tools I'll be using throughout this article to make Notebook.

Please Give Feedback

I'm in no way an expert in either Web Programming or Python. I've taught myself both through trial and error. If you see I've made a mistake or know of a better way to accomplish something, please let me know. I'd really appreciate it. Not only would it help my knowledge of programming, but anyone else who reads this article.