javascript 嵌入python_从Javascript代码调用Python函数

1586010002-jmsa.png

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)");

~/pythoncode.py

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)

解决方案

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

$.ajax({

type: "POST",

url: "~/pythoncode.py",

data: { param: text}

}).done(function( o ) {

// do something

});

你可能感兴趣的:(javascript,嵌入python)