我有一个 python 脚本,它对文件数据库进行一些处理,该脚本根据分析结果生成一些图和其他相关数字。我正在构建一个 Meteor 应用程序,我想要的是在 Meteor 模板内显示我的 python 脚本的结果。
我想要的是整个应用程序的工作原理如下:
1)上传文件到数据库(完成) 2)通过python脚本开始处理文件的按钮 3)在meteor应用程序中显示结果
到目前为止我已经关注this我可以从 meteor 按钮运行脚本,但是如何使用脚本生成的数据(包括一些绘图和相关数字)来填充 meteor 模板?
感谢您的回答 StackOverflow :)
最佳答案
在 Meteor Method 中使用以下代码:
'methodName':function(){
new Fiber(function(){
console.log('test python file');
var file_path = process.env.PWD + "/path_to_file/hello.py";
exec("python " + file_path, function (error, stdout, stderr) {
if (error) console.log('error'+error);
if (stdout) console.log('stdout'+stdout);
if (stderr) console.log('stderr'+stderr);
});
}).run();
}
这里stdout
包含Python代码的输出。您不能直接使用Python生成的绘图,因为它很难集成,但是,您可以将数据发送到meteor并使用meteor使用d3js或plot.ly等库在客户端生成绘图。
输出数据可以是矩阵或 JSON,甚至可以是文件, meteor 随后读取并执行操作。
编辑1:在模板中使用它的示例
'methodName':function(){
new Fiber(function(){
console.log('test python file');
var file_path = process.env.PWD + "/path_to_file/hello.py";
exec("python " + file_path, function (error, stdout, stderr) {
if (error) console.log('error'+error);
else if (stdout) return stdout;
else if (stderr) console.log('stderr'+stderr);
});
}).run();
}
//在助手中
'helper1': function(){
return Meteor.call('methodName');
}
//在HTML中
{{heplper1}}
关于javascript - Python处理如何与Meteor链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39947179/