Alex Brooks
Alex Brooks

Reputation: 5363

Calling Python from JavaScript

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

Answers (7)

Tim
Tim

Reputation: 13048

It is 2023, and right now https://github.com/pyodide/pyodide is the typical solution.

Upvotes: 0

Jorge Niedbalski R.
Jorge Niedbalski R.

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

Alan Franzoni
Alan Franzoni

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:

  • Pyjamas
  • Skulpt
  • PyPy

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

Upvotes: 4

Jamey Hicks
Jamey Hicks

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

Mark Mayo
Mark Mayo

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

jldupont
jldupont

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:

  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.

Upvotes: 0

Toji
Toji

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

Related Questions