- 论坛徽章:
- 0
|
一个python 的AJX 东东!
网站是:
http://pyjamas.pyworks.org/
Many people, when first finding out about
Google Web Toolkit
, wonder "why can't I use Python instead of Java?". pyjamas is designed to make that possible. And we're drawing heavily from Google's work.
It's in its early stages but I invite anyone who is interested to join the mailing list and check out what's in the Subversion repository.
You'll see py-gwt referenced. That's just because that's what I was calling it before a better name came along. jorjun came up with pyjamas. We're still working on backworking an acronym for it that I like :-)
for django :
Using pyjams with Django is very straightforward, this is how I did it.
PyJamas code for the UI:
from ui import RootPanel, TextArea, Label, Button, HTML, VerticalPanel
from JSONService import JSONProxy
class DjangoTest:
def onModuleLoad(self):
self.TEXT_WAITING = "Waiting for response..."
self.TEXT_ERROR = "Server Error"
self.remote = EchoService()
self.status=Label()
self.text_area = TextArea()
self.button = Button("Send text to Echo Service", self)
self.button2 = Button("Send text to Echo Service 2", self)
panel = VerticalPanel()
panel.add(HTML("JSON-RPC Example"))
panel.add(self.text_area)
panel.add(self.button)
panel.add(self.button2)
panel.add(self.status)
RootPanel().add(panel)
def onClick(self, sender):
self.status.setText(self.TEXT_WAITING)
if sender == self.button:
id = self.remote.echo(self.text_area.getText(), self)
else:
id = self.remote.echo2(self.text_area.getText(), self)
if iddef onRemoteResponse(self, response, request_info):
self.status.setText(response)
def onRemoteError(self, code, message, request_info):
self.status.setText("Server Error or Invalid Response: ERROR " + code + " - " + message)
class EchoService(JSONProxy):
def __init__(self):
JSONProxy.__init__(self, "/gtd/test-jsonrpc/", ["echo", "echo2"])
This code is highly derived from
JSONRPCExamle.py
.
In appropriate urls.py I added:
('test-jsonrpc/', 'test_jsonrpc'),
The view maps to an instance of my JSONRPCService class, lets first see how the view is created and initialized [in the appropriate view.py, I have the following]:
test_jsonrpc = JSONRPCService()
def echo(d): return d.upper()
def echo2(d): return d.lower()
test_jsonrpc.add_method("echo", echo)
test_jsonrpc.add_method("echo2", echo2)
Finally the JSONRPCService class I wrote:
class JSONRPCService:
def __init__(self, method_map={}):
self.method_map = method_map
def add_method(self, name, method):
self.method_map[name] = method
def __call__(self, request, extra=None):
#assert extra == None # we do not yet support GET requests, something pyjams do not use anyways.
data = simplejson.loads(request.raw_post_data)
id, method, params = data["id"], data["method"], data["params"]
if method in self.method_map:
result = self.method_map[method](*params)
return HttpResponse(simplejson.dumps({'id': id, 'result': result}))
else:
return HttpResponse(simplejson.dumps({'id': id, 'error': "No such method", 'code': -1}))
The code here is licensed under BSD if you care to use. Happy pyjaming django!
以上资料来自:
http://trac.pyworks.org/pyjamas/wiki/DjangoWithPyJamas
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/6088/showart_203745.html |
|