开发者

Calling Python from JavaScript

Is there a simple way to call Python libraries from JavaScript? Especi开发者_运维知识库ally from the inside of a Firefox extension.

A good option to compile a pure Python library would also be great. I looked at Pyjamas, but it seems to offer only partial support for Python.


You may wish to look at the Titanium Desktop. It has a great API that allows you to call Python easily from within Javascript.

Eg:

<script type="text/python" src="myfile.py"/>

It uses the Kroll Python Bridge to extend and call the functions with simple <script> tags.

You can easily pass integers, floating point numbers, booleans, objects, it's all there.

The aforementioned link has several examples of its simple usage, and naturally it's available for download. :)


You would either need a browser with builtin python support (but this is not a "standard", hence it would defeat the purpose of using a webapp - if you need a special client, you can better use a classic gui), or a way to translate Python to Javascript; there three options for this at the time:

  • Pyjamas
  • Skulpt
  • PyPy

On the contrary, if you just want to extend Firefox, take a look at this: http://pyxpcomext.mozdev.org/


This wouldn't be done in an extension but more likely a plugin (a sometimes obscure but notable difference.)

I'm not aware of any plugins right now that expose Python directly, but there are a couple of ways I've heard of to make it work indirectly. For example: you can get IronPython (a .Net driven version) running through Silverlight. There's also a slashdot article about the subject you may find interesting.

Otherwise, no. I don't believe there's a direct way to do it today. Not that I wouldn't love one. :)


You could build a native Firefox extension that uses libpython to run your python code. From that, export an interface that enables your Javascript code to call python running in your extension.

It appears that there is a Firefox python extension that may enable you to do what you want: http://pyxpcomext.mozdev.org/


Take a look to the https://github.com/niedbalski/slurpy Slurpy project, it implements two way communications as RPC mechanism.

        python = new Slurpy();
        python.on('loaded', function(evt) {
            python.sum(10.5,1000, function(response) {
                alert(response);    
            });
        });


I am not 100% sure, but I do know for a fact that on Google Chrome you can't: you would need either:

  1. an HTTP "gateway" of some sort
  2. an NPAPI plugin

The situation is likely the same on the Firefox case because of security concerns.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜