[英]Executing a Python script from static html
我创建了一个静态html页面,打算在按钮点击时调用python脚本。 我对ajax,flask这样的前端技术比较陌生,因此不能尝试很多东西来执行脚本。 我附加了我的html和python代码。
<div class="container">
<p> Click to Run the script </p>
<button type="button" class="btn" onclick="myFunction()">RUN SCRIPT</button>
</div>
<p id="demo"></p>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Button clicked";
$.ajax({
type:'get',
url:'C:\Users\XYX\Desktop\projects\abc.py',
async:asynchronous,
success: function(data) {
},
error: function(request, status, error) {
}
});
}
</script>
-----------------------------
Python Code
a=4
print (a)
b=12+5
print (b)
c=b%a
print (c)
return
我想在屏幕上看到打印输出。 我也试图接受用户的用户输入并打印输出。 目前,html页面没有显示任何内容。
谢谢
你好这个工作你需要在http服务器上的som类型的解释器,可以执行python脚本,如果你在提供文件静态时尝试这样做,你只需要获取文件的内容。 你想要与php一样,你可以只使用Apache,它将执行PHP脚本。 你的代码中有som错误,
async:asynchronous,
dataType:json
你应该在引号和python代码中使用异步和json,除非从函数调用代码,你不能返回任何东西。
解决方案可能是使用python或node.js或任何其他可编程的http服务器设置http服务器,每当为.py文件调用请求时,它都会将其作为子进程执行,您可能希望使用sys.stdout.write()
python代码中的sys.stdout.write()
用于工作。
希望它有所帮助,并对解释器的含义有所了解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.