【问题标题】:Run python script on node-webkit startup (nw.js)在 node-webkit 启动时运行 python 脚本(nw.js)
【发布时间】:2015-05-11 22:24:22
【问题描述】:

我有一个基于 Python 的应用程序来控制 LED。它使用Flask 创建一个网络服务器,这样我就可以使用 HTML、CSS 和 JS 处理一个漂亮的 UI。

我目前的流程:

  1. python home.py
  2. 在浏览器中导航到localhost:5000
  3. 利润!

我想更进一步,将其打包为nw.js(以前的node-webkit)应用程序。

基本上,我相信我只需要在窗口加载之前执行我的 python 脚本,这样我久经考验的烧瓶网络服务器就会启动并为我的nw.js 应用程序界面创建一个localhost:5000 页面以打开.

如何打包我的 nw.js 应用程序,使其在启动时运行 python 脚本?

【问题讨论】:

  • 只需通过subprocess.Popen从python脚本启动node-webkit前端

标签: python desktop-application node-webkit nw.js


【解决方案1】:

您可以在网络服务器启动后创建加载页面并渲染实际应用。

package.json:

{
  "name": "pythonApp",
  "version": "0.0.0",
  "main": "loading.html",
  "window": {
    "title": "App Name",
    "width": 800,
    "height": 600,
    "position": "center",
  }
}

加载页面 (loading.html) 将加载一个 js 文件,该文件将启动您的实际应用程序页面作为隐藏窗口,然后您可以在服务器运行时显示它。

加载.html:

var gui = require('nw.gui');

var currentWindow = gui.Window.get(); // Get reference to loading.html

var exec = require('child_process').execFile;
exec('home.py', {cwd:'.'}, function (error, stdout, stderr){ // Runs your python code
  var appWindow = gui.Window.open('app.html', // Starts your application
    { width: 800,
      height: 600,
      position: 'center',
      focus: false,
      transparent: true // This hides the application window
    }
);
appWindow.on('loaded', function() { // Waits for application to be loaded
  currentWindow.close({force: 'true'}); // Closes loading.html
    appWindow.show();  // Shows app.html
    appWindow.focus(); // Set the focus on app.html
  });
});

这就是它的要点,但您可能需要针对您的特定设置进行更改。希望对您有所帮助。

【讨论】:

  • 这会加载应用程序窗口,但似乎没有运行 python 脚本。我错过了什么?
【解决方案2】:

使用节点的child_process 模块,并从 index.html 中调用它。

类似:

<!DOCTYPE html>
<html>
  <head>

  </head>
  <body>
    <script>
      var child_process= require('child_process');
      child_process.spawn('python home.py');
    </script>
  </body>
</html>

(代码未经测试,仅作为示例)

【讨论】:

    猜你喜欢
    • 2015-07-14
    • 2015-07-24
    • 2018-08-29
    • 2013-02-16
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    相关资源
    最近更新 更多