javascript 嵌入python_从Javascript代码调用Python函数
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_从Javascript代码调用Python函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 火狐浏览器Firefox如何使用插件,火
- 下一篇: python web框架 多线程_Dja