Monday, May 4, 2009

Python to build websites

Why don't more people create their websites using python? This is a question I would love to get answered.

On my break I was playing around with the python's web capabilities and it seems as strong as php and ruby. There also seems to be full support for SQL calls. Additionally, there is a really cool side effect of creating your website using python.

Instead of index.html you use index.py.
Inside of the index.py file you can create many functions. It is possible to execute that function by going to the url. Additionally, all global variables are viewable by default (you prefix the variable with an underscore to hide it.)

Say I have the following index.py file.


# My index.py file
_page = """ <html><body><h2> This is the page of <i>%s</>, Hello World! </h2></body></html>
"""
owner = """Christan Grant """

def index():
""" This is called by default ""
return _page % owner

def copy_right():
"" This is the copy right page?? """
return ' Do you really want to copy this code? '

##########

So displaying this page: http://www.example.com/index.py ( or http://www.example.com/index or http://www.example.com/) will display:

This is the page of Christan Grant, Hello World!

the url http://www.example.com/index/copy_right with display:

Do you really want to copy this code?

the url http://www.example.com/index/owner will display:

Christan Grant

the url http://www.example.com/index/_page will return a 403 error.

This is a very nice method of addressing uri's. I think this has some applications into the semantic web and displaying RDF information.