Creating websites using Python and TurboGears
Swaroop C H
Yahoo! Bangalore
www.SwaroopCH.info
What is TurboGears?
[any material that should appear in print but not on the slide]
Parts of TurboGears
- SQLObject - database (model)
- Kid - templates (view)
- CherryPy - web handling (controller)
- MochiKit - Javascript/Ajax (web 2.0 goodness)
- TurboGears (One glue that binds all together)
[any material that should appear in print but not on the slide]
How it all fits
Image courtesy of Kevin Dangoor
[any material that should appear in print but not on the slide]
Introduction to SQLObject
- Model your database
- Use Python classes and objects
- Look ma, no SQL!
- Can use MySQL, PostgreSQL, SQLite, MS-SQL, Sybase, MaxDB, Firebird
[any material that should appear in print but not on the slide]
Example of SQLObject
from sqlobject import *
db = connectionForURI('sqlite:///tmp/test.db?debug=1')
sqlhub.processConnection = db
class Article(SQLObject):
title = StringCol(length=50)
body = StringCol()
Article.createTable(ifNotExists=True)
[any material that should appear in print but not on the slide]
SQLObject objects
a = Article(title='Hello World', body='Some stuff here')
print a
a.body = 'New stuff here'
print a
[any material that should appear in print but not on the slide]
SQLObject selects
# select
hello_articles = Article.select(
Article.q.title.startswith('Hello'))
for article in hello_articles:
print article.title, article.body
# selectBy
hello_world_article = Article.selectBy(title='Hello World')
- Joins, AND/OR, Custom SQL
[any material that should appear in print but not on the slide]
Kid
- XML-based templating engine
- Can produce XHTML strict to HTML 4
- Can make use of full Python expressions
- Inspired by XSLT, TAL and PHP
[any material that should appear in print but not on the slide]
Kid Example
<?xml version="1.0"?>
<?python
import datetime
now = datetime.datetime.now()
?>
<html xmlns:py="http://purl.org/kid/ns#">
<head>
<title>Welcome to foss.in/2005</title>
</head>
<body>
<h1>foss.in/2005</h1>
<p>
The time is now ${now}
</p>
</body>
</html>
[any material that should appear in print but not on the slide]
CherryPy
- Pythonic web development framework
- Handles URLs and passing on the calling to Python methods
- Can work behind Apache
[any material that should appear in print but not on the slide]
CherryPy example
import cherrypy
class HelloWorld:
def index(self):
return 'Hello World'
index.exposed = True
cherrypy.root = HelloWorld()
cherrypy.server.start()
[any material that should appear in print but not on the slide]
TurboGears steps
- Edit model.py for class Post
- Edit dev.cfg
- Run `tg-admin sql create`
- Edit controllers.py
- Change master.kid for header and footer
- Change welcome.kid or create a new one for other views
- Done!
Full demo here to show creating a project, adding functionality and displaying a (semi-)working blog
engine.
[any material that should appear in print but not on the slide]
Toolbox
-
tg-admin toolbox
-
CatWalk
-
Widget Browser, WebConsole, etc. coming soon...
[any material that should appear in print but not on the slide]
Where to start?
[any material that should appear in print but not on the slide]
Resources
- www.turbogears.org/docs/
- http://sqlobject.org/SQLObject.html
- http://kid.lesscode.org/language.html
- http://formencode.org/docs/Validator.html
- http://planet.turbogears.org
[any material that should appear in print but not on the slide]
Questions?
- http://groups.google.com/group/turbogears/
- irc.freenode.net#turbogears
- You can reach me at swaroop@swaroopch.info
[any material that should appear in print but not on the slide]