Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.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 节点如何在使用pug和express节点网站单击按钮时运行python脚本_Javascript_Python_Node.js_Pug_Pugjs - Fatal编程技术网

Javascript 节点如何在使用pug和express节点网站单击按钮时运行python脚本

Javascript 节点如何在使用pug和express节点网站单击按钮时运行python脚本,javascript,python,node.js,pug,pugjs,Javascript,Python,Node.js,Pug,Pugjs,我正在尝试使用我在pug中创建的网页运行python脚本,在节点中使用express。我更熟悉python而不是node。使用以下命令,如何运行python脚本?我已经包含了pythonshell,但当我单击pug网页上的按钮时,不确定如何运行python脚本 server.js // require all dependencies var express = require('express'); var app = express(); var PythonShell = require(

我正在尝试使用我在pug中创建的网页运行python脚本,在节点中使用express。我更熟悉python而不是node。使用以下命令,如何运行python脚本?我已经包含了pythonshell,但当我单击pug网页上的按钮时,不确定如何运行python脚本

server.js

// require all dependencies
var express = require('express');
var app = express();
var PythonShell = require('python-shell');


// set up the template engine
app.set('views', './views');
app.set('view engine', 'pug');

var options = {
  mode: 'text',
  pythonOptions: ['-u'],
  scriptPath: '../hello.py',
  args: ['value1', 'value2', 'value3']
};



// GET response for '/'
app.get('/', function (req, res) {

    // render the 'index' template, and pass in a few variables
    res.render('index', { title: 'Hey', message: 'Hello' });

PythonShell.run('hello.py', options, function (err, results) {
    if (err) throw err;
    // results is an array consisting of messages collected during execution
    console.log('results: %j', results);
});

});

// start up the server
app.listen(3000, function () {
    console.log('Listening on http://localhost:3000');
});
哈巴狗

html
    head
        title= title
    body
        h1= message
        a(href="http://www.google.com"): button(type="button") Run python script

让我们看看。通常,为了与python/nodejs交互,我使用djangorestframework,它使用服务器方法GET、POST等。因此,首先,您必须有一个用python运行的服务器和脚本。然后在node中,您可以使用jquery或js框架来侦听节点应用程序中的事件。单击按钮时,会向python发送get/post请求。在python中,还可以使用javascript对脚本进行条件呈现,如:if request==POST://run script。由于脚本是从节点运行的,所以当单击按钮时,必须通过节点中的http触发事件。只是一个想法。

创建另一条路线,单击该按钮时将调用该路线。让我们调用is
/foo
。现在设置此路由的处理程序:

const { spawn } = require('child_process')
app.get('/foo', function(req, res) {
    // Call your python script here.
    // I prefer using spawn from the child process module instead of the Python shell
    const scriptPath = 'hello.py'
    const process = spawn('python', [scriptPath, arg1, arg2])
    process.stdout.on('data', (myData) => {
        // Do whatever you want with the returned data.
        // ...
        res.send("Done!")
    })
    process.stderr.on('data', (myErr) => {
        // If anything gets written to stderr, it'll be in the myErr variable
    })
})
现在在前端使用帕格创建按钮。在客户端javascript中,单击此按钮时,对
/foo
进行AJAX调用。比如说,

button(type="button", onclick="makeCallToFoo()") Run python script
form(method="get" action="/foo")
    button(type="submit") Run python script
在客户端JS中:

function makeCallToFoo() {
    fetch('/foo').then(function(response) {
        // Use the response sent here
    })
}
编辑:您还可以以类似的方式使用已经在使用的shell模块。如果不需要客户端JS,可以用属性将按钮括起来:
method=“get”action=“/foo”
。比如说,

button(type="button", onclick="makeCallToFoo()") Run python script
form(method="get" action="/foo")
    button(type="submit") Run python script

OP已经有一个节点服务器。没有理由用DRF创建一个。现在就开始吧。长话短说,Express server可以而且应该处理整个工作流。我来举个例子。该死,你抢了我。回答得好。我喜欢您提供的JS示例和基于表单的示例。