Less talk, more code

The blog of Iskandar Soesman

Make Your web.py Python Framework more MVC Style

So its python time. Lets talk a bit about web.py. It is a extremely simple Python web developement framework. I show you the reason by creating some "Hello World" application below.

First, you have to import the core modules in your python file. Then create some routing rule for your web's url. Next, create a web.py instance. After that, create a class that reprisent your application. And last, run the web.py object. So here the sampe code from my index.py file:

import web

urls = (
    '/(.*)', 'hello'
)
app = web.application(urls, globals())

class hello:        
    def GET(self, name):
        if not name: 
            name = 'World'
        return 'Hello, ' + name + '!'

if __name__ == "__main__":
    app.run()

If you have enother calss all you have to do just routit on urls variable just like:

urls = (
    '/index', 'index',
    '/home', 'home',
    '/user' 'user'
)

...

class index:

...

class home:

...

class user:

This is good for small number class. But what if you have lot of class, are your still wanna put it in the single file? I thinks is a bad idea. Then I have an idea. What if we put each class in a single file and we called it a controller, like lot of other framework? So the application urls would be look like:

http://www.myapplication.com/my_controller/my_method/my_arguments

Before we continue, you may try the demo that I hosted in Google App Engine.

http://kandarpy.appspot.com/

For the view system, web.py already have a templating so we don't have to wory for this.

First, install web.py. Check http://webpy.org/install for detail installation. Then set your default python page become like this:

import web
import web_controller

urls = (
  '/(.*)', 'Gear'
)

hendler = web_controller.Handler()

class Gear:
    def GET(self, args = False):
        return hendler.control(args)

app = web.application(urls, globals(), autoreload=False)
main = app.cgirun()

The class name is optional, so you can named it with anythings you like. The important part is:

import web_controller
111

and
```python
urls = (
  '/(.*)', 'Gear'
)

This parameter mean that all request will goes to 'Gear' class.

Download the controller modul form http://kandarpy.appspot.com/sources/web_controller. Create "controllers", "views" and "modules" folder in your root application folder.

Create index.html file in your views folder. Its look like:

$def with ()

Hello World!

Create default controller file called home.py in your controllers folder.

import web

views = web.template.render('views/')

class Home:
    def index(self, *args):
        return views.index()

Oke now is time to test your applications. If nothing's wrong, your web browser will display "Hello World!".

How to load a model class? Its easy. Create a new file in your model folder. Lets we named it data_sample.py. In this file you wanna import db moduls from web.py so it will look some things like this:

import web

db = web.database(dbn='mysql', user='username', pw='password', db='database')

class Data_sample:

    def get_data(self):
        data = db.select('my_table')
        return data

now, import your model file into controller:

import web
import models.data_sample

views = web.template.render('views/')
my_data = models.data_sample.Data_sample()

class Home:
    def index(self, *args):
        posts = my_data.get_data()
        return views.index(posts)

And last, your view file will look:

$def with (posts)
    Welcome
<ul>
$for post in posts:
    <li id="t$post.id">$post.title</li>
</ul>

Thats it, now your web.py become more MVC style. You can copy all this samples in http://kandarpy.appspot.com/ any questions?