[Zope] Experiment: Pyjamas with Zope
Jim Washington
jwashin at vt.edu
Sat Dec 20 07:51:19 EST 2008
So, I have a need to put together a more elaborate web page. Lots of
pop-up dialog boxes and dynamically-updated choice lists. How to do that?
I take a look at Pyjamas http://pyjs.org. Pretty cool.
Here's the theory. Write fancy application web page in python using the
Pyjamas/GWT API, then "compile" it to javascript. Serve it through
zope, which will handle auth/auth. Client-server communication flows
through json-rpc calls. What could possibly go wrong?
First-off, just a simple test-of-concept. Edit the JSONRPCExample.py
file in the examples/jsonrpc folder that came with pyjamas.
I'll locate the jsonrpc service in zope's containment root, so I change
the uri in EchoServicePython from "/services/EchoService.py" to "/".
That's all I need to change. Compile it using the handy "build.sh"
that's in the same folder. Look in the "output" folder and see what we got.
Wow. a bunch of files.
JSONRPCExample.IE6.cache.html
Mozilla.cache.html
OldMoz.cache.html
Opera.cache.html
Safari.cache.html
JSONRPCExample.html
JSONRPCExample.nocache.html
corner_dialog_bottomleft.png
corner_dialog_bottomleft_black.png
corner_dialog_bottomright.png
corner_dialog_bottomright_black.png
corner_dialog_edge.png
corner_dialog_edge_black.png
corner_dialog_topleft.png
corner_dialog_topleft_black.png
corner_dialog_topright.png
corner_dialog_topright_black.png
history.html
pygwt.js
tree_closed.gif
tree_open.gif
tree_white.gif
I'm lazy, so instead of doing a bunch of resource or view directives in
ZCML, I let my paste.ini do the handling.
[app:pyjs1]
use=egg:Paste#static
document_root=/home/jwashin/projects/pyjamas/pyjamas-0.4/examples/jsonrpc/output
then, I link that in with the composite app.
[composite:Paste.Main]
use = egg:Paste#urlmap
/ = zope
/images = images
/pyjs1 = pyjs1
Now, to get to JSONRPCExample.html, I need to go to
http://website/pyjs1/JSONRPCExample.html
But first, I need to actually handle the json-rpc requests in zope.
Make a servicetest.py. Almost exactly similar to EchoService.py in the
output/services folder.
from zif.jsonserver.jsonrpc import MethodPublisher
class Services(MethodPublisher):
def echo(self, msg):
return msg
def reverse(self, msg):
return msg[::-1]
def uppercase(self, msg):
return msg.upper()
def lowercase(self, msg):
return msg.lower()
Now, a ZCML incantation.
<jsonrpc:view
for="zope.app.folder.interfaces.IRootFolder"
permission="zope.Public"
methods="echo reverse uppercase lowercase"
class = ".servicetest.Services"
/>
Start zope, and go to http://website/pyjs1/JSONRPCExample.html
Nice page. Click the "Send to Python Service" button.
Server Error or Invalid Response: ERROR 0 - Server Error or Invalid Response
Damn.
Pull up tcpwatch and see what we are getting. Aha. pyjamas app is
sending jsonrpc with a content-type of
application/x-www-form-urlencoded, so zope is not handing it off to
zif.jsonserver for handling.
Fix pyjamas or let zif.jsonserver handle this content-type?
In zif.jsonserver's configure.zcml, in the "publisher" directive, add
application/x-www-form-urlencoded to mimetypes.
Restart zope. Go to http://website/pyjs1/JSONRPCExample.html
It works.
Change permissions in the jsonrpc:view directive. Restart zope.
Go to the page. Page loads. Push the button, and I get a Basic HTTP
Authentication dialog. Nice.
Overall Results: So far, so good. :)
- Jim Washington
More information about the Zope
mailing list