К сожалению, то, что вы пытаетесь сделать, будет невозможно. Чтобы запустить скрипт 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({
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);
});