views:

75

answers:

2

I basically want to be able to:

  • Write a few functions in python (with the minimum amount of extra meta data)
  • Turn these functions into a web service (with the minimum of effort / boiler plate)
  • Automatically generate some javascript functions / objects for rpc (this should prevent me from doing as many stupid things as possible like mistyping method names, forgetting the names of methods, passing the wrong number of arguments)

Example

python:

def hello_world():
    return "Hello world"

javascript:

...
<!-- This file is automatically generated (either dynamically or statically) -->
<script src="http://myurl.com/webservice/client_side_javascript"&gt; </script> 
...
<script>
$('#button').click(function () {
     hello_world(function (data){ $('#label').text(data)))
}
</script>

A bit of research has shown me some approaches that come close to this:

  • Automatic generation of json-rpc services from functions with a little boiler plate code in python and then using jquery and json to do the calls (still easy to make mistakes with method names - still need to be aware of urls when calling, very irritating to write these calls yourself in the firebug shell)

  • Using a library like soaplib to generate wsdl from python (by adding copious type information). And then somehow convert this into javascript (not sure if there is even a library to do this)

But are there any approaches closer to what I want?

+3  A: 

Yes there is, there is Pyjamas. Some people bill this as the "GWT for Python"

fuzzy lollipop
I'd heard of pyjamas - but can this actually generate javascript functions rather than requiring you to code the entire application in python (or rather python like language with subtly different semantics). I was kind of looking some something lightweight.
Yes, it can. You don't have to use the whole thing; there's a python to javascript compiler, and it outputs javascript that does the same thing as your python functions. You also need to do something on the server side to make jsonrpc or plain json URIs for your data, but that's not at all hard.
Andrew McGregor
Hmm. Still not quite sure pyjamas is fit-for-purpose. It seems that you need to jump through quite a lot of hoops to actually carry out this task.
Relevant information:(i) When using the full pyjamas compiler you can't access the javascript from the template html page (the one living in shared), because the javascript produced is loaded within an iframe. You can however access this iframe and pull out variables using window.frames[0].<variablename> and then execute these.(ii)If you define a module test.py then any functions within it live on object with the name 'test' within this iframes scope.
The standalone compiler (pyjampiler) doesn't seem to produce code that can be executed until you work out which javascript modules the compiled code depends upon and include them yourself - I didn't try doing this but if feels like this could be quite painful to do. When you use the full compiler the these modules are inserted into the html as script tags.
A: 

It looks like using a javascript XML RPC client (there is jquery plugin for this) together with an XML RPC server is a good way to go.

The jquery plugin will introspect your rpc service and will populate method names make it impossible to mis type the name of a method call without getting early warning. It will not however test the number of arguments that you pass, or their type.

There doesn't seem to be the same support for introspection on json rpc (or rather there doesn't seem to be a consistent standard). This approach can also be used with django.

I've put together some example code and uploaded it here (I hope that linking to one's blog posts isn't considered terrible form - a brief search of the internet didn't seem to suggest it was)...