免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1231 | 回复: 0
打印 上一主题 下一主题

一个python 的AJX 东东! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-11-20 17:49 |只看该作者 |倒序浏览
一个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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP