Pyjamas and web2py

14 views
Skip to first unread message

mdipierro

unread,
Feb 6, 2009, 2:57:17 PM2/6/09
to web2py Web Framework
This is a draft and I have not tried it yet. Perhaps you can help me
debug it.

http://mdp.cti.depaul.edu/AlterEgo/default/show/203

Timothy Farrell

unread,
Feb 6, 2009, 3:22:57 PM2/6/09
to web...@googlegroups.com
Sweet!

It doesn't seem to like line 34 of todoApp.py which looks like this:

|if id<0: id =" self.remote.deleteTask(sender.getValue(sender.getSelectedIndex()),self)" method ="=" method ="=" method ="=">|

what should that be instead?

-tim
--
Timothy Farrell <tfar...@swgen.com>
Computer Guy
Statewide General Insurance Agency (www.swgen.com)

Timothy Farrell

unread,
Feb 6, 2009, 3:37:21 PM2/6/09
to web...@googlegroups.com
Well after a short but valiant effort, I'm going back to business as
usual. I'll wait util someone else figures this out better than I.

Basically, there's a discrepancy between the location of the static
Pyjamas files and the data service (and that's after you fix the typos).

Typos to fix are:
pyjamas.py:
line 3: def __init__(self):
line 15: self.method_map[func.__name__]=func

todoApp.py needs to be indented correctly.

build.sh should be named build.bat in Windows and should point to your
installation of Pyjamas.

-tim

dhmorgan

unread,
Feb 6, 2009, 5:12:30 PM2/6/09
to web2py Web Framework
Fantastic idea! I've just been getting into the Pyjamas and would love
to help.

Tomorrow. ;-)


Danny

Ernesto Savoretti

unread,
Feb 6, 2009, 10:20:14 PM2/6/09
to web2py Web Framework


On Feb 6, 8:12 pm, dhmorgan <dharrimanmor...@gmail.com> wrote:
> Fantastic idea! I've just been getting into the Pyjamas and would love
> to help.
>

+1

I've been out to Pyjamas the last couple of months :) and because of
that I missed completely some important web2py trends (T2, T3 and so
on). The trade-off was that I got deeply involved in Pyjamas stuff,
including developmente of some widgets (calendar, etc) and I'm
fascinated with it. By the way, Timothy, Pyjamas is here to stay and
it's mature enough. Try it yourself rather than holding your breath
(this could severely damage your health :)
So, I think that a Pyjamas client side along Web2Py serverside would
be the coolest thing.
Please count me in. ;-P

lkcl

unread,
Feb 7, 2009, 8:08:08 AM2/7/09
to web2py Web Framework

http://groups.google.com/group/pyjamas-dev/browse_thread/thread/f051f537ca9ee734#

okaaaay, so i posted a test procedure there, which gets you to the
"first stage". it should turn up any day soon on web2py cos i cross-
posted but hey i'll link it here as well.

it is worthwhile emphasising that the first stage has _nothing_ to do
with pyjamas, and everything to do with jsonrpc.

making web2py jsonrpc-capable should be a real high-priority and those
simple, simple 16 lines of code (once corrected - well spotted
timothy :) should, ideally, be made a standard web2py module (or
JSONRPCService put straight into the web2py namespace, which seems to
be the way that things are done in web2py).

then, you can start writing jsonrpc services which can be talked to by
ohh, whatever, really. extjs. php. pure AJAX. whatever-you-choose.

once that's fixed and tested _then_ you can move on to utilising
pyjamas to compile some JSONRPC-client code that will talk to the
controllers/default.py functions, confident in the knowledge that
default.py does its job correctly.

l.

mdipierro

unread,
Feb 7, 2009, 8:42:53 PM2/7/09
to web2py Web Framework
Almost there.... this

http://groups.google.com/group/web2py/web/web2py.app.pyjamas.tar

works but pyjamas does not understand the json response from the
server.
I am not sure what I am doing wrong. The json response if built in
models/pyjamas.py

Massimo

On Feb 7, 7:08 am, lkcl <luke.leigh...@googlemail.com> wrote:
> http://groups.google.com/group/pyjamas-dev/browse_thread/thread/f051f...

chris p

unread,
Feb 8, 2009, 12:09:12 PM2/8/09
to web2py Web Framework
I got this working today, and it seems fine with the Pyjamas
"JSONRPCExample" from the pyjamas distribution.

Here's the code (originally based on some of the samples above, with
several fixes and some error handling added). I changed the pyjamas
object to 'jsonrpc' instead, since this is a generic jsonrpc server,
and not really tied to pyjamas.

#### step 1, create models/jsonrpc.py #################
"""
original code from : http://trac.pyworks.org/pyjamas/wiki/DjangoWithPyJamas
"""
import gluon.contrib.simplejson as simplejson
import types

class JSONRPCService:
def response(self, id, result):
return simplejson.dumps({'version': '1.1', 'id':id,
'result':result, 'error':None})
def error(self, id, code, message):
return simplejson.dumps({'id': id,
'version': '1.1',
'error': {'name': 'JSONRPCError',
'code': code,
'message': message
}
})

def __init__(self):
self.methods={}

def serve(self):
import sys
data = simplejson.loads(request.body.read())
id, method, params = data["id"], data["method"], data
["params"]
if method in self.methods:
try:
result =self.methods[method](*params)
return self.response(id, result)
except BaseException:
etype, eval, etb = sys.exc_info()
return self.error(id, 100, '%s: %s' %(etype.__name__,
eval))
except:
etype, eval, etb = sys.exc_info()
return self.error(id, 100, 'Exception %s: %s' %(etype,
eval))
else:
return self.error(id, 100, 'method "%s" does not exist' %
method)

def __call__(self,func):
self.methods[func.__name__]=func
return func

def listmethods(self):
return self.methods.keys()

jsonrpc=JSONRPCService()


########## step 2) create controllers/rpc.py #############

def index():
return jsonrpc.serve()

@jsonrpc
def echo(text):
return 'echoing: %s' % text

@jsonrpc
def reverse(text):
return text[-1::-1]

@jsonrpc
def uppercase(text):
return text.upper()

@jsonrpc
def lowercase(text):
return text.lower()

#### step 3) edit JSONRPCExample service location in pyjamas
#######################################

now in the pyjamas demo, copy JSONRPCExample.py and it's public folder
over to your web2py static directory.
In JSONRPCExample.py, change the EchoServicePython class to point to
your app (below, my app is named "pyjamas_2").

class EchoServicePython(JSONProxy):
def __init__(self):
JSONProxy.__init__(self, "/pyjamas_2/rpc", ["echo", "reverse",
"uppercase", "lowercase"])


#### step 4) build pyjamas code

#### step 5) browse to localhost:8000/<yourapp>/static/output/
JSONRPCExample.htm


Chris

chris p

unread,
Feb 8, 2009, 12:35:53 PM2/8/09
to web2py Web Framework
I'm reposting as my first didn't seem to go through....

I got this working with the pyjamas JSONRPCExample. Most of the
changes were in the jsonrpc server examples posted above. Here's the
code

---------------- controllers/rpc.py ----------------------
# try something like
def index():
return jsonrpc.serve()

@jsonrpc
def echo(text):
return 'echoing: %s' % text

@jsonrpc
def reverse(text):
return text[-1::-1]

@jsonrpc
def uppercase(text):
return text.upper()

@jsonrpc
def lowercase(text):
return text.lower()

------------- models/jsonrpc.py -------------------------
-----------------------------------------------------------------------------------------

Then you need to change the server location in the pyjamas code to
something like this

class EchoServicePython(JSONProxy):
def __init__(self):
JSONProxy.__init__(self, "/pyjamas_2/rpc", ["echo", "reverse",
"uppercase", "lowercase"])

where you would change "pyjamas_2" for your app name

I put the "output" folder from the pyjamas build into the web2py
static directory



Chris

mdipierro

unread,
Feb 8, 2009, 1:24:54 PM2/8/09
to web2py Web Framework

chris p

unread,
Feb 8, 2009, 7:02:42 PM2/8/09
to web2py Web Framework
Massimo,

Your AlterEgo post is probably the best place to maintain the example
until you put it into the web2py distribution. Please take a look at
my code above however, as it handles errors according to the JSONRPC
spec. In particular, errors are not just a string in the 'error'
element of the response, but they are an error object containting
name, code, & message. The pyjamas JSONRPCExample won't work correctly
with the current AlterEgo posting if an error needs to be returned. I
think it's also important to wrap a try arountd the dispatched method
call so that you can always return valid, decodable JSON content back
to the caller.

This is a cool development for web2py & pyjamas (I just recently
started working w/ both) and it will be interesting to see what
developments come out of it. Keep up the good work on web2py, it seems
really nice to work with so far.

Chris


chris p

unread,
Feb 8, 2009, 7:04:33 PM2/8/09
to web2py Web Framework
Here's the reference I was using for the error return protocol:
http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html

On Feb 8, 1:24 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
> Thank you chris. I also just posted this:
>
> http://groups.google.com/group/web2py/browse_thread/thread/96b5a94552...

mdipierro

unread,
Feb 8, 2009, 7:47:54 PM2/8/09
to web2py Web Framework
Chris,

would you fix my todo example and email it back to me? I will repost
it.

I can also give access to the AlterEgo entry if you email me
personally.

Massimo

lkcl

unread,
Feb 9, 2009, 4:59:48 AM2/9/09
to web2py Web Framework
chris -

On Feb 9, 12:47 am, mdipierro <mdipie...@cs.depaul.edu> wrote:
> Chris,
>
> would you fix my todo example and email it back to me? I will repost
> it.

chris, hi,

i'd like to know how that works, because there are now three
JSONRPCservice classes kicking about: one for web2py, one for django
and one for .... ohh, what was it... web.py that was it.

also, this:
http://lkcl.net/jsonrpclib.tgz

i added exception handling to it but it would be nice if you could
check it over and perhaps use it to do some regression tests of
web2py's jsonrpc service.

l.

Timothy Farrell

unread,
Feb 9, 2009, 8:59:41 AM2/9/09
to web...@googlegroups.com
While not relevant to web2py, I would like to see some example sites
that use Pyjamas. Luke, I've seen your homepage. Are there other
examples that we can see?

-tim

chris p

unread,
Feb 9, 2009, 10:06:24 AM2/9/09
to web2py Web Framework
Massimo, I just emailed a complete web2py app with the working code.

chris p

unread,
Feb 9, 2009, 10:13:22 AM2/9/09
to web2py Web Framework
Here's what I ended up with on the server side (in web2py, it goes in
models/jsonrpc.py):

-----------------------------
There's going to have to be different server classes because of the
different api's of django, web2py, web.py. The one I posted above is
not truly complete (it doesn't do service description for example),
but it seems to do the basics correctly when tested with the pyjamas
JSONRPC client. The client from http://lkcl.net/jsonrpclib.tgz also
seems to work fine. I haven't tried any other clients.

Unfortunately I won't have any more time to spend on this for a while,
but I'm hoping this helps get things in the right direction. I sent
massimo a complete version of the working pyjamas-based ToDo app that
he started, so he should be able to post that or incorporate changes
into the next release.

Chris

lkcl

unread,
Feb 9, 2009, 11:51:10 AM2/9/09
to web2py Web Framework
> There's going to have to be different server classes because of the
> different api's of django, web2py, web.py. The one I posted above is
> not truly complete (it doesn't do service description for example),
> but it seems to do the basics correctly when tested with the pyjamas
> JSONRPC client. The client fromhttp://lkcl.net/jsonrpclib.tgzalso
> seems to work fine. I haven't tried any other clients.

that's brilliant, chris, thank you very much.

mdipierro

unread,
Feb 9, 2009, 2:57:39 PM2/9/09
to web2py Web Framework
Thank you Chris.

Massimo
Reply all
Reply to author
Forward
0 new messages