python - 如何使用 Flask 将参数传递给 Electron 中的 UI?
问题描述
我正在使用 Flask 创建一个 web 应用程序,并想为它创建一个桌面应用程序。现在在 Flask 中,我可以在 render_template 函数中传递一个列表和 HTML 文件
return render_template('index.html', someList = someList)
然后在 HTML 文件中使用它,如下所示:
{% for i in someList %}
#doSomething
{% endfor %}
谁能告诉我这方面的电子等效物?
解决方案
我建议您应该使用电子应用程序作为烧瓶应用程序的 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
推荐阅读
- node.js - 角度与快递,会话不是持久的。每个请求都会创建一个新会话
- c++ - build2:编译器不支持模块
- ruby-on-rails - ruby on rails best_in_place nomethoderror
- r - 为什么悬停文本不适用于 ggplotly、geom_histogram 和 facet_grid?
- ios - 由 UIAlert 按钮选择启动的导航的 SWReveal 问题
- docker - Minikube 操作超时
- r - 改变数据框的结构
- llvm - 使用 Bazel 构建 LLVM
- bash - 在终端中将值转换为多线条形图
- apache - Apache服务器重启错误