[英]How to get a return variable from a Python Script through JQuery?
Currently I am trying to call a simple python script to test if I can receive output into my javascript function.目前,我正在尝试调用一个简单的 python 脚本来测试我是否可以将 output 接收到我的 javascript ZC1C425268E6393D1356681DB398F14CE6DZ 中。 However, when I do this ajax request但是,当我执行此 ajax 请求时
function testPhy()
{
$.ajax({
url: "./Phython/test.py",
data: {param: text},
success: function(dataR){
console.log("LK: " + dataR)
},
error: function(request, status, error) {
console.log("Error: " + error)
}
});
}
testPhy();
I get the content's of the script and not the return variable我得到脚本的内容而不是返回变量
print("script running")
text = "Why hi there"
return text
Is there a way I can grab the contents of text from the python file using this Ajax query?有没有办法使用这个 Ajax 查询从 python 文件中获取文本内容?
You need to create Server Node.js for example, i suggest you to read this tutorial例如,您需要创建服务器 Node.js,我建议您阅读本教程
1- you create a basic server with a basic client web 1-您使用基本客户端 web 创建一个基本服务器
2- use "child_process" package which comes packaged with node. 2-使用与节点一起打包的“child_process”package。
const spawnpy = require("child_process").spawn;
const pythonProcess = spawnpy('python',["path/script.py", arg1, arg2, .and so on]);
3- To send data to node just do that in the python script: 3-要将数据发送到节点只需在 python 脚本中执行此操作:
print(dataToSendToNode)
sys.stdout.flush()
4- node server can listen for data using: 4-节点服务器可以使用以下方式监听数据:
pythonProcess.stdout.on('data', (data) => {
// Do something with the data returned from python script
});
5- you answer to the ajax request (send data to your client browser) 5-您回答 ajax 请求(将数据发送到您的客户端浏览器)
You have lot of solutions to execute a python script with node.js, you could use python-shell, so when your server will be activa, search on google python node.js
... You have lot of solutions to execute a python script with node.js, you could use python-shell, so when your server will be activa, search on google python node.js
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.