Section 2.2. Creating a Hello World Application


2.2. Creating a Hello World Application

Okay, now that you have TurboGears and SQLite installed (or another database if you chose that route), you are ready to start your first TurboGears application. This is easier than it looks.

TurboGears provides a command-line administrative interface to help you with common development tasks, from creating a new project to opening up a database and creating all the tables necessary to hold your data. But more about all of that later, for now let's just use the tg-admin command to create a new application.

This couldn't be simpler, you just open up a command prompt window and type the following:

tg-admin quickstart


You are then asked for the name of your project. For this first project, just call it hello. The next thing you must provide is your package name. TurboGears does its best to turn your project name into a reasonable package name, omitting spaces and other characters that are illegal in package names. So, in this case, you can just press Enter to accept the default package name, which will also be hello. You also want to answer no to the next question because we do not plan to password protect this little "hello world" application.

Enter project name: hello Enter package name: [hello]


Then it spits out a bunch of text like this:

Enter project name: hello Enter package name [hello]: Do you need Identity (usernames/passwords) in this project? [no] Selected and implied templates:   TurboGears#tgbase      tg base template   TurboGears#turbogears  web framework Variables:   identity:  none   package:   hello   project:   hello Creating template tgbase Creating directory .\hello   Recursing into +einame+.egg-info     Creating .\hello\hello.egg-info/     Copying PKG-INFO to .\hello\hello.egg-info\PKG-INFO     Copying paster_plugins.txt to .\hello\hello.egg-info\paster_plugins.txt     Copying sqlobject.txt_tmpl to .\hello\hello.egg-info\sqlobject.txt   Recursing into +package+     Creating .\hello\hello/     Copying __init__.py_tmpl to .\hello\hello\__init__.py     Copying release.py_tmpl to .\hello\hello\release.py     Recursing into static       Creating .\hello\hello\static/       Recursing into css         Creating .\hello\hello\static\css/         Copying empty to .\hello\hello\static\css\empty       Recursing into images         Creating .\hello\hello\static\images/         Copying favicon.ico to .\hello\hello\static\images\favicon.ico         Copying tg_under_the_hood.png to .\hello\hello\static\images\tg_under_the_ hood.png       Recursing into javascript         Creating .\hello\hello\static\javascript/         Copying empty to .\hello\hello\static\javascript\empty     Recursing into templates       Creating .\hello\hello\templates/       Copying __init__.py_tmpl to .\hello\hello\templates\__init__.py Creating template turbogears   Recursing into +package+     Recursing into config       Creating .\hello\hello\config/       Copying __init__.py_tmpl to .\hello\hello\config\__init__.py       Copying app.cfg_tmpl to .\hello\hello\config\app.cfg       Copying log.cfg_tmpl to .\hello\hello\config\log.cfg     Copying controllers.py_tmpl to .\hello\hello\controllers.py     Copying json.py_tmpl to .\hello\hello\json.py     Copying model.py_tmpl to .\hello\hello\model.py     Recursing into sqlobject-history       Creating .\hello\hello\sqlobject-history/       Copying empty to .\hello\hello\sqlobject-history\empty     Recursing into templates       Copying login.kid to .\hello\hello\templates\login.kid       Copying master.kid to .\hello\hello\templates\master.kid       Copying welcome.kid to .\hello\hello\templates\welcome.kid     Recursing into tests       Creating .\hello\hello\tests/       Copying __init__.py_tmpl to .\hello\hello\tests\__init__.py       Copying test_controllers.py_tmpl to .\hello\hello\tests\test_controllers.py       Copying test_model.py_tmpl to .\hello\hello\tests\test_model.py   Copying README.txt_tmpl to .\hello\README.txt   Copying dev.cfg_tmpl to .\hello\dev.cfg   Copying sample-prod.cfg_tmpl to .\hello\sample-prod.cfg   Copying setup.py_tmpl to .\hello\setup.py   Copying start-+package+.py_tmpl to .\hello\start-hello.py Running C:\Python24\python.exe setup.py egg_info Adding TurboGears to paster_plugins.txt running egg_info writing requirements to hello.egg-info\requires.txt writing hello.egg-info\PKG-INFO writing top-level names to hello.egg-info\top_level.txt reading manifest file 'hello.egg-info\SOURCES.txt' writing manifest file 'hello.egg-info\SOURCES.txt'


This quickstart command creates a directory structure, and a couple of template files for you to put your code into. Don't worry about the details just yet, however; we come back to it. For now, we just want to get started doing something that works!

You can now navigate into the hello directory that we just created. In it you will find start-hello.py. Run this by entering the following:

python start-hello.py


You should then see this:

mark@ubuntu:~/tg0.9/hello$ python start-hello.py 2005/12/26 13:03:58 CONFIG INFO Server parameters: 2005/12/26 13:03:58 CONFIG INFO   server.environment: development 2005/12/26 13:03:58 CONFIG INFO   server.logToScreen: True 2005/12/26 13:03:58 CONFIG INFO   server.logFile: 2005/12/26 13:03:58 CONFIG INFO   server.protocolVersion: HTTP/1.0 2005/12/26 13:03:58 CONFIG INFO   server.socketHost: 2005/12/26 13:03:58 CONFIG INFO   server.socketPort: 8080 2005/12/26 13:03:58 CONFIG INFO   server.socketFile: 2005/12/26 13:03:58 CONFIG INFO   server.reverseDNS: False 2005/12/26 13:03:58 CONFIG INFO   server.socketQueueSize: 5 2005/12/26 13:03:58 CONFIG INFO   server.threadPool: 0 2005/12/26 13:03:58 HTTP INFO Serving HTTP on http://localhost:8080/


If you get an error, the most likely cause is that you already have a service running on port 8080. You can fix this easily by running TurboGears on another port. To change the port your TurboGears application runs on, just open dev.cfg in the root directory of your application and change the line that reads

# server.socket_port=8080


to

server.socket_port=3000


(Remember to remove the # sign!!!)

At this point, you should be able to run the start-hello.py program.

If everything works, you should next be able to open a browser and go to http://localhost:8080/ and see the "Are you ready to Gear Up?" page (as shown in Figure 2.1). Of course, if you are running on another port, you must put that in the URL!!

Figure 2.1. TurboGears welcome page


If this worked, your application is up and running.

Decorators in Python 2.3

TurboGears makes extensive use of "decorators." Decorators are discussed in more detail later. The examples in this book are written in this form:

@expose() def some_method(self, someval):     return "Hi"


The @expose() is a decorator, and the @ syntax was introduced in Python 2.4. Luckily, the @ is just syntax sugar. If you're using Python 2.3, you can translate the above into the following:

def some_method(self, someval):     return "Hi" some_method = expose()(some_method)


That is the standard way to do decoration in Python 2.3. It's more verbose, but it works. Lucky for us, however, TurboGears offers a special convenience syntax for decoration:

[expose()] def some_method(self, someval):     return "Hi"


The preceding code works in both Python 2.3 and 2.4. We don't use this syntax in the book examples because it is nonstandard. However, it is definitely the easiest and most pleasant option for Python 2.3 users.





Rapid Web Applications with TurboGears(c) Using Python to Create Ajax-Powered Sites
Rapid Web Applications with TurboGears: Using Python to Create Ajax-Powered Sites
ISBN: 0132433885
EAN: 2147483647
Year: 2006
Pages: 202

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net