Reputation: 5363
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.
Upvotes: 12
Views: 26010
Reputation: 13048
It is 2023, and right now https://github.com/pyodide/pyodide is the typical solution.
Upvotes: 0
Reputation: 604
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);
});
});
Upvotes: 2
Reputation: 3169
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:
On the contrary, if you just want to extend Firefox, take a look at this: http://pyxpcomext.mozdev.org/
Upvotes: 4
Reputation: 2370
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/
Upvotes: 3
Reputation: 12585
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. :)
Upvotes: 4
Reputation: 96716
I am not 100% sure, but I do know for a fact that on Google Chrome you can't: you would need either:
The situation is likely the same on the Firefox case because of security concerns.
Upvotes: 0
Reputation: 34498
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. :)
Upvotes: 3