首页 > 解决方案 > 如何使用 Flask 将参数传递给 Electron 中的 UI?

问题描述

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

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

然后在 HTML 文件中使用它,如下所示:

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

谁能告诉我这方面的电子等效物?

标签: pythonflaskelectron

解决方案


我建议您应该使用电子应用程序作为烧瓶应用程序的 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


推荐阅读