Take the 2-minute tour ×

I'd like to call a Python function from Javascript code, because there isn't an alternative in Javascript for doing what I want. Is this possible? Could you adjust the below snippet to work?

Javascript part:

var tag = document.getElementsByTagName("p")[0];
text = tag.innerHTML;
// Here I would like to call the Python interpreter with Python function
arrOfStrings = openSomehowPythonInterpreter("~/pythoncode.py", "processParagraph(text)");


contains functions using advanced libraries that don't have an easy to write equivalent in Javascript

import nltk # is not in Javascript
def processParagraph(text):
  nltk calls
  return lst # returns a list of strings (will be converted to `Javascript` array)
share|improve this question
No, browsers (fortunately) won't execute arbitrary Python code. You'll want to run that in a server. –  larsmans Nov 1 '12 at 10:54
Javascript runs on the client. I assume the python runs on the server. You could send an ajax request to the server. It won't be fast. –  Jan Dvorak Nov 1 '12 at 10:55
Using ajax, send text to a python script on your server. Set up the script to return data in an easy to parse (for js) notation (like JSON) and assign the result to arrOfStrings in the success handler. –  Asad Nov 1 '12 at 10:56
Also, finding a python engine for javascript won't be easy either –  Jan Dvorak Nov 1 '12 at 10:57
You can run the official Python interpreter in the browser by compiling it using clang and Emscripten. This has been done before. –  user142019 Nov 1 '12 at 11:02
show 1 more comment

3 Answers

All you need is to make an ajax request to your pythoncode. You can do this with jquery http://api.jquery.com/jQuery.ajax/, or use just javascript

  type: "POST",
  url: "~/pythoncode.py",
  data: { param: text}
}).done(function( o ) {
   // do something
share|improve this answer
It looks interesting. Where could be the call of processParagraph(text) so that the return values ends in the variable arrOfStrings? –  xralf Nov 1 '12 at 11:45
processParagraph(text) will be inside of the pythoncode.py file, and actually this function will return arrOfStrings. You will need to parse your arrOfStrings in // do something part in javascript –  Salvador Dali Nov 1 '12 at 11:54
I'm running this code in firebug, but it logs [] –  xralf Nov 1 '12 at 12:00
processParagraph(text) is a python code, so it should be inside a python file, and you putted it inside a js callback. –  Salvador Dali Nov 1 '12 at 12:12
OK, so how is it right? My Python file contains the correct function. Should I call the function in Python and the argument will be sys.argv[1]? –  xralf Nov 1 '12 at 12:25
show 2 more comments

From the document.getElementsByTagName I guess you are running the javascript in a browser.

The traditional way to expose functionality to javascript running in the browser is calling a remote URL using AJAX. The X in AJAX is for XML, but nowadays everybody uses JSON instead of XML.

For example, using jQuery you can do something like:

    function(data, textStatus, jqXHR) {

You will need to implement a python webservice on the server side. For simple webservices I like to use Flask.

A typical implementation looks like:

def my_webservice():
    return jsonify(result=some_function(**request.args)) 

You can run IronPython (kind of Python.Net) in the browser with silverlight, but I don't know if NLTK is available for IronPython.

share|improve this answer
add comment

Typically you would accomplish this using an ajax request that'll look similar to the below.

var xhr = new XMLHttpRequest();
xhr.open("GET", "pythoncode.py?text=" + text, true);
xhr.responseType = "JSON";
xhr.onload = function(e) {
  var arrOfStrings = JSON.parse(xhr.response);
share|improve this answer
add comment

Your Answer


