--- author: '' category: '' date: 2009/03/20 15:57 description: '' link: '' priority: '' slug: BB798 tags: programming, python title: Pyjamas and Juno type: text updated: 2009/03/20 15:57 url_type: '' --- I am currently in dire need of creating a nice admin page for a LDAP addressbook, which should do some slightly unusual things with the data (like manipulating Postfix's virtual table). So, what the heck, let's also learn something new while I'm at it. The victims: Pyjamas_ and Juno_. Pyjamas is sort of a Python version of GWT_ and is bliss. Finally I can code HTML and JS in python ;-) And Juno is refreshingly simple. Since the whole frontend is done by Pyjamas, all I needed is a way to route JSONRPC_ calls to python code and operate in the backend. So, here are two useful snippets: .. code-block:: python # This decorator decodes JSONRPC arguments as sent by Pyjamas to # Juno, and calls the target function with the decoded version. def JSONRemote(target): def wrapper(web,*args,**kwargs): postdata=web.input().keys()[0] data = json.loads(postdata) id, method, params = data["id"],data["method"],[web,]+data["params"][1] kwargs['method']=method return target(*params,**kwargs) return wrapper Using this, any plain juno method works as a JSONRPC method! For example: .. code-block:: python @route('/user') @JSONRemote def list(web,startwith='*',method=None): try: result=search(filter='(&(uid=*@bigclient.ar)(sn=%s*))'%startwith) resp=JSONResponse(result) except ldap.LDAPError, e: resp=JSONResponse(None,e.desc) return resp See? No JSON decoding. And no encoding either, because I am creating the response using this: .. code-block:: python # This class creates a correct JSON response from Juno as required by Pyjamas class JSONResponse(JunoResponse): def __init__(self,result=None,error=None,id=None,code=200): JunoResponse.__init__(self) self.headers['Content-Type']='text/javascript' self.append(json.dumps({'result':result,'error':error,'id':id,'code':code})) This is probably not a great implementation but it's good enough for me right now. .. _Pyjamas: http://code.google.com/p/pyjamas/ .. _Juno: http://brianreily.com/project/juno/ .. _GWT: http://code.google.com/webtoolkit/ .. _JSONRPC: http://en.wikipedia.org/wiki/JSON-RPC