【问题标题】:How to pass arguments to UI in Electron, using Flask?如何使用 Flask 将参数传递给 Electron 中的 UI?
【发布时间】:2019-03-07 10:24:18
【问题描述】:

我正在使用 Flask 创建一个 web 应用程序,并希望为此创建一个桌面应用程序。现在在 Flask 中,我可以在 render_template 函数中传递一个列表和 HTML 文件

return render_template('index.html', someList = someList)

然后在HTML文件中使用如下:

{% for i in someList %}
  #doSomething
{% endfor %}

谁能告诉我这方面的 Electron 等价物吗?

【问题讨论】:

  • 您的意思是使用模板语言?如果是这样,您可以使用 React+Redux 或其他一些前端框架来创建应用程序,就像使用 Python+Pug 一样

标签: python flask electron


【解决方案1】:

我建议您应该将电子应用程序用作烧瓶应用程序的 UI。这意味着您使用电子启动烧瓶服务器,然后导航到电子 UI 中的 url。 以下是如何执行此操作的示例:

app.on('ready', function() {
  //call python?
  var subpy = require('child_process').spawn('python', ['./hello.py']);

  var rq = require('request-promise');
  var mainAddr = 'http://localhost:5000';

  var openWindow = function(){
    // Create the browser window.
    mainWindow = new BrowserWindow({width: 800, height: 600});
    // and load the index.html of the app.
    // mainWindow.loadURL('file://' + __dirname + '/index.html');
    mainWindow.loadURL('http://localhost:5000');
    // Open the devtools.
    mainWindow.webContents.openDevTools();
    // Emitted when the window is closed.
    mainWindow.on('closed', function() {
      // Dereference the window object, usually you would store windows
      // in an array if your app supports multi windows, this is the time
      // when you should delete the corresponding element.
      mainWindow = null;
      // kill python
      subpy.kill('SIGINT');
    });
  };

完整的项目可以在 github 上找到: https://github.com/fyears/electron-python-example

【讨论】:

    猜你喜欢
    • 2012-02-29
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    相关资源
    最近更新 更多