Is there a good way of automatically generating javascript client code from server side python

StackOverflow https://stackoverflow.com/questions/2541954

  •  23-09-2019
  •  | 
  •  

문제

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"> </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?

올바른 솔루션이 없습니다

다른 팁

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

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)...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top