简体   繁体   English

从静态html执行Python脚本

[英]Executing a Python script from static html

I have created a static html page and intend to call a python script on button click. 我创建了一个静态html页面,打算在按钮点击时调用python脚本。 I am relatively new to front end technologies like ajax ,flask hence not able to try many things to execute the script. 我对ajax,flask这样的前端技术比较陌生,因此不能尝试很多东西来执行脚本。 I have attached my html and python code. 我附加了我的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

I want to see the print output on the screen. 我想在屏幕上看到打印输出。 i am also trying to accept user input from user and the printing back the output. 我也试图接受用户的用户输入并打印输出。 Currently the html page is not displaying anything. 目前,html页面没有显示任何内容。

Thanks 谢谢

Hello for this to work you need som kind of interpretor on the http server that can execute a python script, if you were try to do this while serving the files static you would just get the content of the file. 你好这个工作你需要在http服务器上的som类型的解释器,可以执行python脚本,如果你在提供文件静态时尝试这样做,你只需要获取文件的内容。 you want the same with php where you can just use apache and it will execute the php script. 你想要与php一样,你可以只使用Apache,它将执行PHP脚本。 And you have som errors in your code, 你的代码中有som错误,

async:asynchronous, 
dataType:json

you should have asynchronous and json in quotes and in the python code unless the code is called from a function you cannot return anything. 你应该在引号和python代码中使用异步和json,除非从函数调用代码,你不能返回任何东西。

A solution for this could be to set up the http server with python or node.js or any other programable http servers and whenever a request was called for a .py file it would execute it as a child process and you might want to use sys.stdout.write() in the python code for that to work. 解决方案可能是使用python或node.js或任何其他可编程的http服务器设置http服务器,每当为.py文件调用请求时,它都会将其作为子进程执行,您可能希望使用sys.stdout.write() python代码中的sys.stdout.write()用于工作。

hope it helps and gives some kind of understanding of what an interpreter is. 希望它有所帮助,并对解释器的含义有所了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM