2016-10-09 59 views
0

我有一个python脚本对文件数据库进行一些处理,脚本会产生一些图表和其他相关数字作为分析结果。我正在构建一个Meteor App,我想要的是在Meteor模板中显示我的python脚本的结果。Python处理如何与流星链接

我wnat是整个应用程序的工作原理是这样的:

1)上传文件到数据库(完成) 2)按钮由python脚本 3)节目开始文件的处理流星应用程序内的结果

到目前为止我已经按照this,我能够从流星按钮运行脚本,但我怎么能使用脚本生成的数据(包括一些情节和相关数字)来填充流星模板?

谢谢您的回答StackOverflow的:)

回答

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); 
       if (stdout) console.log('stdout'+stdout); 
       if (stderr) console.log('stderr'+stderr); 
      }); 
     }).run(); 
    } 

这里stdout包含Python代码的输出。你不能直接使用从python生成的图表,因为它很难整合,但是,你可以将数据发送到meteor并使用meteor在客户端使用d3js或plot.ly等库生成图。

输出数据可以是矩阵或JSON,或者甚至可以是流星随后读取并执行操作的文件。

编辑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}} 
+0

谢谢,我会给它一个尝试,走着瞧吧。 – RolandDeschain

+0

嗨@Ankit,我让它工作..但我仍然不知道如何使用流星模板中的标准输出数据。任何想法?谢谢 – RolandDeschain

+0

嗨@RolandDeschain,我用一个例子编辑了答案。 – Ankit