| Name |
Space |
Section |
Page |
Version |
Status |
Reviewed |
Author(s) |
| Getting Started With ToscaWidgets and Pylons |
Pylons CookBook |
Home |
Getting Started With ToscaWidgets and Pylons |
1.0 |
Draft |
False |
James Gardner |
Introduction
ToscaWidgets
is the forms package which is likely to be the recommended default for Pylons 1.0. In this article we'll describe how to get a simple ToscaWidgets example working with Pylons. See also An Alternative ToscaWidgets Setup with Mako.
Installation
The first step is to install ToscaWidgets. To do this you will need easy install. If you haven't got easy install on your system you can install it like this:
wget http://peak.telecommunity.com/dist/ez_setup.py
python ez_setup.py
Next you need to install two packages, ToscaWidgets itself and the twforms package:
easy_install ToscaWidgets
easy_install twForms
If you have a GCC 2.95 build environment this code should install perfectly without any intervention otherwise you may find that there is a problem compiling the speedup.c extension to RuleDispatch. If you encounter this problem there are three things you can do:
1. See if there is a pre-compiled version of RuleDispatch on your platform
If there is you should install it first before re-running the two commands above. The TurboGears
project might be a good place to look for help since they use RuleDispatch too.
2. Compile RuleDispatch Yourself
Install the necessary build environment so that RuleDispatch can be compiled.
eg. on Debian Etch you can do this:
sudo apt-get install gcc-2.95 python-dev libc6-dev
You'll need to ensure that the gcc command is linked to gcc-2.95 not some other version and you will have to change the gcc link if it is.
Run this command:
ln -s /usr/bin/gcc-2.95 /usr/bin/gcc
Then check the command is correctly linked:
ls -l /usr/bin/gcc
lrwxrwxrwx 1 root root 17 2007-03-05 12:44 /usr/bin/gcc -> /usr/bin/gcc-2.95
You should now be able to run the two easy_install commands and despite some warnings, RuleDispatch should compile.
3. Remove the RuleDispatch C Extension
RuleDispatch doesn't even need the C Extension which is causing all the problems. If you download the source distribution and edit the setup.py file to comment out the C extensions part then run this command RuleDispatch should install correctly:
You can then re-run the easy_install commands and all should be good.
Testing the WSGI Example
It's always advisable to test the simple example before trying anything more complicated so get hold of the wsgi_app.py example and run it to check everything is OK.
wget http://svn.turbogears.org/projects/ToscaWidgets/trunk/examples/wsgi_app.py
python wsgi_app.py
If everything goes to plan the example should popup your browser so you can test the application. If you get an error along the lines of webbrowser.Error: could not locate runnable browser you can either install a command line web-browser or better still, remove the line which attempts to launch the web browser from the example.
 |
At this point if you are running the example from the console running the example might popup whichever command line browser you have installed and this might not be what you wanted! Just close the command line browser and use your desktop browser to test the example. There is quite a lot of nice JavaScript involved which your command line browser won't show you. |
 |
You might find the example runs very slowly, in which case issue this command to install the Paste 0.1.3 or above.
easy_install "Paste>=0.1.3"
then restart the example. |
If all goes to plan you should see:
serving on 0.0.0.0:8000 view at http://127.0.0.1:800
You can test the example from your browser.
Pylons Integration
Now that we have the WSGI version working we can try full Pylons integration.
First install Pylons:
easy_install "Pylons>=0.9.4"
Then create a project:
paster create --template=pylons TWTest
Now we need to integrate the ToscaWigets middleware. Edit TWTest/twtest/config/middleware.py to add this to the imports at the top:
1
2 | from toscawidgets.middleware import TGWidgetsMiddleware
from toscawidgets.mods.pylonshf import PylonsHostFramework
|
and then immediately after these lines:
1
2
3
4
5
6
7
8 | app = pylons.wsgiapp.PylonsApp(config, helpers=packyears.lib.helpers,
g=app_globals.Globals)
g = app.globals
app = ConfigMiddleware(app, conf)
# YOUR MIDDLEWARE
# Put your own middleware here, so that any problems are caught by the error
# handling middleware underneath
|
add this:
1
2 | # Setup ToscaWidgets
app = TGWidgetsMiddleware(app, PylonsHostFramework)
|
That's all there is to it! You should now be setup to use ToscaWidgets.
 |
ToscaWidgets also has a handy Paste Deploy plugin that allows you to add this middleware via the config file. Ordinarily since your application will rely on ToscaWidgets it is better to keep this middleware in the application itself so that you don't give your end users the chance to remove it when they alter the config file. |
Your First Form
Now that Pylons is setup you can create your first form. Lets add a new controller:
 |
For some reason the paste controller command isn't working for me at the moment. If it doesn't work for you either you should just create the file manually from the description below. |
The TWTest/twtest/controllers/form.py file looks like this::
1
2
3
4
5 | from twtest.lib.base import *
class FormController(BaseController):
def index(self):
return Response('')
|
Add the following after the import:
1
2
3
4
5
6
7
8
9
10
11 | from formencode import Invalid
from toscawidgets.widgets.forms.samples import AddUserForm
from toscawidgets.mods.pylonshf import validate, render_response
from toscawidgets.api import WidgetBunch
form = AddUserForm('form')
class Person(object):
name = "Peter"
email = "peter@example"
age = 2
|
This sets up a sample form based on AddUserForm and a Person class that will be use to create a person to populate that form.
Change the two actions so they look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | def index(self):
# Could factor crating c.w to BaseController's __init__
c.w = WidgetBunch()
c.w.form = form
c.value = Person()
c.action = h.url_for(action='save')
c.title = "Sample Pylons app"
return render_response('home.myt')
@validate(form, "index")
def save(self):
if not hasattr(self, 'form_result'):
return self.index()
c.input_values = self.form_result
c.title = "Validated data"
return render_response('home.myt')
|
 |
The @validate decorator used here is not the same as Pylons' @validate decorator and the render_response function is also an altered version. |
Now we need to setup the templates. First make the templates/autohandler look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | <% c.title %>
% for rsrc in c.resources.get('head', []):
<% rsrc.display() %>
% #end for
% for rsrc in c.resources.get('bodytop', []):
<% rsrc.display() %>
% #end for
% m.call_next()
% for rsrc in c.resources.get('bodybottom', []):
<% rsrc.display() %>
% #end for
|
Then add a file called templates/home.myt which looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13 | % if c.input_values:
Validated data:
% for k,v in c.input_values.iteritems():
<% k %>
<% repr(v) %>
% # end for
% else:
% if c.w.form and c.action:
<% c.w.form.display(c.value, action=c.action) %>
% #end if
% #end if
|
That's it! You can now test your application:
paster serve --reload development.ini
You should find the Pylons version you just developed behaves identically to the WSGI sample.
Any questions add a comment or drop me an email. James Gardner.
gcc-2.95 is veeery old
most servers have GCC3, rest GCC4. I've compiled RuleDispatch 0.5_pre2115 on amd64 Gentoo with GCC 4.1.1 without problems so it should compile on GCC3, as GCC4 is more "I compile only C/C++ which is ok with ISO standards" than GCC3 