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

[any material that should appear in print but not on the slide]

How it all fits

How it all fits
Image courtesy of Kevin Dangoor
[any material that should appear in print but not on the slide]

Introduction to SQLObject

[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')


[any material that should appear in print but not on the slide]

Kid

[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

[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 Quickstart

TurboGears steps

  1. Edit model.py for class Post
  2. Edit dev.cfg
  3. Run `tg-admin sql create`
  4. Edit controllers.py
  5. Change master.kid for header and footer
  6. Change welcome.kid or create a new one for other views
  7. 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

[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



[any material that should appear in print but not on the slide]

Questions?



[any material that should appear in print but not on the slide]