简体   繁体   中英

Executing a Python script from static html

I have created a static html page and intend to call a python script on button click. I am relatively new to front end technologies like ajax ,flask hence not able to try many things to execute the script. I have attached my html and python code.

<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.

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. you want the same with php where you can just use apache and it will execute the php script. And you have som errors in your code,

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.

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.

hope it helps and gives some kind of understanding of what an interpreter is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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