如何通过HTML按钮运行Python脚本? [英] How to run Python script through HTML button?

查看:93
本文介绍了如何通过HTML按钮运行Python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过HTML按钮运行Python脚本.我没有使用任何服务器.

I wanted to Run Python script through HTML button. I am not using any server.

我尝试使用本地服务器,但出现此错误: jquery-3.3.1.min.js:2 GET http://127.0.0.1:8080/home/techm/Documents/labelImg-master/labelImg.py 404(未找到)

I tried using local server but it's giving this error : jquery-3.3.1.min.js:2 GET http://127.0.0.1:8080/home/techm/Documents/labelImg-master/labelImg.py 404 (Not Found)

我尝试了没有服务器也得到此错误: jquery-3.3.1.min.js:2无法加载文件:///home/techm/Documents/labelImg-master/labelImg.py:跨协议请求仅支持以下协议方案:http,数据,chrome,chrome扩展名,https.

And i tried with out server also I get this error : jquery-3.3.1.min.js:2 Failed to load file:///home/techm/Documents/labelImg-master/labelImg.py: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

这是我的HTML代码:

this is my HTML code :

<!DOCTYPE html>
<html>
  <head>    
  </head>
  <body>
    <input type="button" id='script' name="scriptbutton" value=" Run Script " onclick="goPython()">

    <script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>


    <script>
        function goPython(){
            $.ajax({
              url: "/home/techm/Documents/labelImg-master/run.sh",
             context: document.body
            }).done(function() {
             alert('finished python script');;
            });
        }
    </script>
  </body>
</html>

还有其他方法可以单击HTML页面中的按钮并自动运行python脚本.

Is there any other way I can click the button in HTML page and automatically it run python script.

推荐答案

不幸的是,您要尝试执行的操作无法实现.要运行位于文件系统上的Python脚本,您必须在终端中运行它,而这不可能通过导航器来完成.

Unfortunately, what you're trying to do won't be possible. To run your Python script that is located on your file system, you would have to run it in your terminal and this is not possible to do via your navigator.

我建议您设置一个快速服务器来为您执行此操作.

I recommend you set up a quick server that does this for you.

node.js服务器:

const http = require('http');
const { exec } = require('child_process');
const fs = require('fs');

//create a server object:
http.createServer(function (req, res) {

  //if the request url is: localhost:8080
  if(req.url === "/"){
    fs.readFile("index.html", function(err, data){
      res.writeHead(200, {'Content-Type': 'text/html'});
      res.write(data);
      res.end();
    });
  //if the request url is: localhost:8080/run
  }else if(req.url === "/run"){


    exec('./ /home/techm/Documents/labelImg-master/run.sh', (error, stdout, stderr) => {
     if (error) {
       console.error(`exec error: ${error}`);
       res.write('Command has failed'); //write a response to the client
       res.end(); //end the response
       return;
     }
     console.log(`stdout: ${stdout}`);
     console.log(`stderr: ${stderr}`);

     res.write('Command has been run'); //write a response to the client
     res.end(); //end the response
    });
  }

}).listen(8080); //the server object listens on port 8080

将以上内容放入(例如) server.js 文件中后,通过在终端中键入 node server.js 来运行服务器(与该文件位于同一目录中

Once you've put the above content in a file called (for example) server.js, run the server by typing node server.js in your terminal (in the same directory as that file)

然后您的ajax应该是这样的.

Then your ajax should be something like this.

$.ajax({
  url: "/run",
  context: document.body
}).done(function(data) {
  //data should contain what the server responds, in this case "Command has been run"
  console.log(data);
});

这篇关于如何通过HTML按钮运行Python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆