views:

600

answers:

5

Is there a simple way to call Python libraries from JavaScript? Especially 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.

+3  A: 

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

Toji
A: 

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.

jldupont
+3  A: 

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

Mark Mayo
+3  A: 

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/

Jamey Hicks
+2  A: 

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/

Alan Franzoni