An Overview of TurboGears
TurboGears
Rapid Web Development Framework
What is TurboGears?
• TurboGears is a Python Open Source web application framework
based on a Model View Controller (MVC) architecture.
• TurboGears aggregates and integrates several other Open Source
packages to provide the functionality of an MVC web application
framework:
– The model is provided by SQLObject or SQLAlchemy
– The controller is provided by CherryPy
– The View is provided by the Kid templating system
– In addition, MochiKit is used to provide a client-side JavaScript
library (slogan: “MochiKit makes JavaScript suck less”)
August 29, 2007 NRAO Green Bank – TurboGears Tutorial (TurboGears Overview) 2
What is TurboGears? (cont.)
TurboGears
SqlAlchemy
CherryPy
MochiKit Kid
August 29, 2007 NRAO Green Bank – TurboGears Tutorial (TurboGears Overview) 3
TurboGears Application
August 29, 2007 NRAO Green Bank – TurboGears Tutorial (TurboGears Overview) 4
Getting Started
• Starting a TurboGears project is easy. TurboGears provides the
tg-admin utility to automate a number of tedious tasks. The most
commonly used are:
– tg-admin quickstart
• Creates a project; lays out the directory structure, provides
configuration files with the most commonly used values
defaulted, and provides startup scripts to test your web app.
– tg-admin sql create
• Creates the actual database from your model.
– tg-admin shell
• Brings up a Python or iPython shell, with your project’s model
already imported. Very handy to experiment with your model.
– tg-admin help
• Prints out a list of options to tg-admin.
August 29, 2007 NRAO Green Bank – TurboGears Tutorial (TurboGears Overview) 5
Getting Started (cont.)
August 29, 2007 NRAO Green Bank – TurboGears Tutorial (TurboGears Overview) 6
TurboGears project directory structure
hello
dev.cfg
start-hello.py
hello hello.egg-info
models.py
controllers.py
config sqlobject- static templates tests
history master.kid
welcome.kid
css images javascript
August 29, 2007 NRAO Green Bank – TurboGears Tutorial (TurboGears Overview) 7
Project Development Configuration File
[global] # Set to True if you'd like to abort execution if a controller gets an
# This is where all of your settings go for your development environment # unexpected parameter. False by default
# Settings that are the same for both development and production tg.strict_parameters = True
# (such as template engine, encodings, etc.) all go in
# hello/config/app.cfg # LOGGING
# Logging configuration generally follows the style of the standard
# DATABASE # Python logging module configuration. Note that when specifying
# log format messages, you need to use *() for formatting variables.
# driver://username:password@host:port/database # Deployment independent log configuration is in hello/config/log.cfg
[logging]
# pick the form for your database
# sqlalchemy.dburi="postgres://username@hostname/databasename" [[loggers]]
# sqlalchemy.dburi="mysql://username:password@hostname:port/databasename" [[[hello]]]
# sqlalchemy.dburi="sqlite://%(current_dir_uri)s/devdata.sqlite" level='DEBUG'
qualname='hello'
# If you have sqlite, here's a simple default to get you started handlers=['debug_out']
# in development
sqlalchemy.dburi="sqlite:///devdata.sqlite" [[[allinfo]]]
level='INFO'
# set to 1 to make SQLAlchemy echo SQL commands on the console handlers=['debug_out']
sqlalchemy.echo = 0
[[[access]]]
# SERVER level='INFO'
qualname='turbogears.access'
# Some server parameters that you may want to tweak handlers=['access_out']
# server.socket_port=8080 propagate=0
# Enable the debug output at the end on pages.
# log_debug_info_filter.on = False
server.environment="development"
autoreload.package="hello"
# session_filter.on = True
August 29, 2007 NRAO Green Bank – TurboGears Tutorial (TurboGears Overview) 8
Running your new app
• CherryPy provides a development HTTP server that you can use to run
your TurboGears project
• tg-admin quickstart provides a python script to get this server
going with your project:
August 29, 2007 NRAO Green Bank – TurboGears Tutorial (TurboGears Overview) 9
New Application!
August 29, 2007 NRAO Green Bank – TurboGears Tutorial (TurboGears Overview) 10
Obligatory Hello World Example
To display “Hello World” in our application, all we need to do is to modify the view, since no data
processing is going on. We modify “welcome.kid” to look like this:
August 29, 2007 NRAO Green Bank – TurboGears Tutorial (TurboGears Overview) 11
Hello World Application
After saving “welcome.kid”, all we need to do is to refresh our browser,
to see the results:
August 29, 2007 NRAO Green Bank – TurboGears Tutorial (TurboGears Overview) 12