PyjamasWithJSONAndPyramid

https://www.google.com/accounts/o8/id?id=AItOawkhTYyg047a2uTPhhYI33i399h-SVY3Cv4 edited this page Nov 27, 2011 · 1 revision

This is the exact text of an email from Jim Washington about using Pyramid with Pyjamas and JSON-RPC. I'll test it out and make any edits based on my experience. -Steve Spicklemire

Hi, Steve

To start, use the pyjamas app as a set of static files and use JSON-RPC (only) to interact with the server.

1. Copy the jsonrpc example folder from pyjamas to somewhere in your pyramid app's source tree. A "client" folder in the main folder beside "static" and "templates" is a good location.

2. Modify the build.sh script so that pyjsbuild uses the full path to bin/pyjsbuild in your pyjamas source tree. Use the --no-compile-inplace directive to keep the output local. Set --output to something like "`pwd`/../../static/" so the pyjamas compiled output goes into the pyramid "static" folder. -m (optional) will make a "lib" directory in the static folder so you have lots of little .js files instead of huge .html files with the javascript in them.

3. Make an EchoService.py file in the pyramid app's root, next to models.py. Note that this particular formulation of the service is for version 0.3 of pyramid_rpc.

from pyramid_rpc.jsonrpc import jsonrpc_method

@jsonrpc_method(endpoint='echoservice') def echo(request, msg): return msg @jsonrpc_method(endpoint='echoservice') def reverse(request, msg): return msg[::-1] @jsonrpc_method(endpoint='echoservice') def uppercase(request, msg): return msg.upper() @jsonrpc_method(endpoint='echoservice') def lowercase(request, msg): return msg.lower()

This registers each of the methods as JSON-RPC methods available at an endpoint named 'echoservice' on the pyramid server. We'll configure the server url at '/jsonrpcexample' later.

For grins, you may compare this file with the EchoService.py file in the jsonrpc example's "services" folder.

4. Edit JSONRPCExample.py in your copy of pyjamas's jsonrpc example to match the above server url. The EchoServicePython class should look like

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

5. Install pyramid's json-rpc plugin into the virtualenv for the pyramid app. easy_install pyramid_rpc.

6. Configure the pyramid app for json-rpc.

Edit __init__.py and add the following to main():

config.include('pyramid_rpc.jsonrpc') config.add_jsonrpc_endpoint('echoservice', '/jsonrpcexample')

And, since we used the @jsonrpc_method decorator, we need

config.scan()

7. Build the pyjamas app, using its build.sh script.

8. Start the pyramid app: bin/paster serve development.ini

9. Point a web browser to /static/JSONRPCExample.html in the pyramid web site. Twiddle the "method to call" list and push the "Send to Python Service" button.

This should get you started with pyjamas and pyramid using JSON-RPC. It's only a start. You may obtain templated pyramid files by using iframes in the pyjamas app and setting the "src" attribute. You may have widgets in the pyjamas app using your pyramid database data through JSON-RPC methods. You may also put pyjamas apps in iframes in your pyramid templates. I'm also pretty sure that, without too much trouble, you can also get the rendered content of pyramid templates through JSON-RPC and use that HTML code within the pyjamas app. There are lots of options. Have fun!