Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何通过HTML按钮运行Python脚本?_Javascript_Python_Html - Fatal编程技术网

Javascript 如何通过HTML按钮运行Python脚本?

Javascript 如何通过HTML按钮运行Python脚本?,javascript,python,html,Javascript,Python,Html,我想通过HTML按钮运行Python脚本。 我没有使用任何服务器 我尝试使用本地服务器,但出现了以下错误:jquery-3.3.1.min.js:2 GEThttp://127.0.0.1:8080/home/techm/Documents/labelImg-master/labelImg.py 404(未找到) 我试用了服务器,但也出现了以下错误:jquery-3.3.1.min.js:2无法加载file:///home/techm/Documents/labelImg-master/labe

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

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

我试用了服务器,但也出现了以下错误:
jquery-3.3.1.min.js:2无法加载file:///home/techm/Documents/labelImg-master/labelImg.py: 跨源请求仅支持协议方案:http、数据、chrome、chrome扩展、https。

这是我的HTML代码:

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

函数goPython(){
$.ajax({
url:“/home/techm/Documents/labelImg master/run.sh”,
上下文:document.body
}).done(函数(){
警报('finished python script');;
});
}

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

由于安全原因,您无法通过javascript函数访问文件系统

您唯一能做的就是禁用浏览器的安全性

如果使用Chrome,请按如下方式启动Chrome:

C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-web-security --user-data-dir="C:/tempFolderForChromeThings

由于安全原因,无法通过javascript函数访问文件系统

您唯一能做的就是禁用浏览器的安全性

如果使用Chrome,请按如下方式启动Chrome:

C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-web-security --user-data-dir="C:/tempFolderForChromeThings

不幸的是,你想做的是不可能的。要运行位于文件系统上的Python脚本,您必须在终端上运行它,而这不可能通过导航器实现

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

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
运行服务器(与该文件位于同一目录中)

那么您的ajax应该是这样的

$.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);
});

不幸的是,你想做的是不可能的。要运行位于文件系统上的Python脚本,您必须在终端上运行它,而这不可能通过导航器实现

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

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
运行服务器(与该文件位于同一目录中)

那么您的ajax应该是这样的

$.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);
});
用布莱顿怎么样? 它是用于客户端的python解释器

这里是网站

你可以这样使用它

<div id="editor">
    print(123)
</div>
<button id="btn">Run Python</button>

...

<script type="text/javascript" src="https://cdn.rawgit.com/brython-dev/brython/stable/www/src/brython.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/brython-dev/brython/stable/www/src/brython_stdlib.js"></script>
<script type="text/python">
    from browser import doc, window
    from browser import html

    def exec_python():
        exec(doc['editor'].value)

    doc['btn'].bind('click', exec_python)
</script>

印刷品(123)
运行Python
...
从浏览器导入文档,窗口
从浏览器导入html
def exec_python():
exec(文档['editor'].value)
doc['btn'].bind('click',exec\u python)
如何使用布莱顿? 它是用于客户端的python解释器

这里是网站

你可以这样使用它

<div id="editor">
    print(123)
</div>
<button id="btn">Run Python</button>

...

<script type="text/javascript" src="https://cdn.rawgit.com/brython-dev/brython/stable/www/src/brython.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/brython-dev/brython/stable/www/src/brython_stdlib.js"></script>
<script type="text/python">
    from browser import doc, window
    from browser import html

    def exec_python():
        exec(doc['editor'].value)

    doc['btn'].bind('click', exec_python)
</script>

印刷品(123)
运行Python
...
从浏览器导入文档,窗口
从浏览器导入html
def exec_python():
exec(文档['editor'].value)
doc['btn'].bind('click',exec\u python)

我发现了一种非常简单的方法,可以从浏览器按钮启动python脚本,但要在浏览器之外运行。您不必安装任何其他软件。没有服务器,没有网络权限,没有新的协议注册表,不需要任何东西

  • 编译python脚本以获得.pyc文件。在python shell上:
  • 在html中定义一个按钮:
  • 确保路径信息完整。为了简单起见,这里假定html和python脚本位于同一目录中。如果不同,请提供myScript.pyc文件的完整路径信息

    单击该按钮时,将弹出“使用打开文件/保存文件”对话框。默认python exe列为打开的应用程序。所以,再次点击。就在那里。Python脚本开始执行

    不幸的是,它不是很容易消除第二个按钮点击;因为“始终对此类文件执行相同操作”选项已禁用。您在选项->文件下载首选项中所做的设置也没有任何区别。您必须在浏览器的profiles文件夹中编辑MimeTypes.rdf文件;我没有试过


    上面的场景在Python3.6.5和Firefox 65.0上进行了测试,我发现了一种非常简单的方法,可以从浏览器按钮启动Python脚本,但在浏览器之外运行。您不必安装任何其他软件。没有服务器,没有网络权限,没有新的协议注册表,不需要任何东西

  • 编译python脚本以获得.pyc文件。在python shell上:
  • 在html中定义一个按钮:
  • 确保路径信息完整。为了简单起见,这里假定html和python脚本位于同一目录中。如果不同,请提供myScript.pyc文件的完整路径信息

    单击该按钮时,将弹出“使用打开文件/保存文件”对话框。默认python exe列为打开的应用程序。所以,再次点击。就在那里。Python脚本开始执行

    不幸的是,它不是很容易消除第二个按钮点击;因为“始终对此类文件执行相同操作”选项已禁用。您在选项->文件下载首选项中所做的设置也没有任何区别。您必须在浏览器的profiles文件夹中编辑MimeTypes.rdf文件;我没有试过


    上面的场景在使用Firefox 65.0的Python 3.6.5上进行了测试

    您的代码似乎打开了URL
    /home/techm/Documents/labelImg master/run.sh
    ——但是错误消息说找不到
    /home/techm/Documents/labelImg master/labelImg.py
    。。。那是不可能的嗨