admin管理员组文章数量:1327661
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=""> </script>
...
<script>
$('#button').click(function () {
hello_world(function (data){ $('#label').text(data)))
}
</script>
A bit of research has shown me some approaches that e 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?
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./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 e 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?
Share Improve this question asked Mar 29, 2010 at 23:35 user47741user47741 3,0154 gold badges23 silver badges14 bronze badges 3- 1 yeah you need to go and accept some answers to previous questions! – user177800 Commented Mar 29, 2010 at 23:39
- 1 Perhaps other people just aren't very good at answering questions :). – user47741 Commented Mar 30, 2010 at 1:02
- The reason for wanting to write python code and execute it remotely easily isn't to write python rather than javascript. But rather to run things on the server (and hence modify the state on the server) as a result of actions on the client side. Pyjamas is probably quite useful if what you want to do is write python instead of javascript (and also if you want easy access to to a set of widgets) – user47741 Commented Apr 2, 2010 at 13:25
2 Answers
Reset to default 6Yes 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)...
本文标签:
版权声明:本文标题:Is there a good way of automatically generating javascript client code from server side python - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742227409a2436582.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论