我正在尝试使用我在 pug 中创建的网页运行 python 脚本,在节点中表达。我对 python 比对 node 更熟悉。使用以下如何运行 python 脚本?我包含了 python shell,但当我单击 pug 网页上的按钮时,我不确定如何运行 python 脚本。
服务器.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');
});
指数.pug
html
head
title= title
body
h1= message
a(href="http://www.google.com"): button(type="button") Run python script