Electron实战 -- 构建Windows桌面版Scrapydweb (二)

书接上回,我们说Pyinstaller可能并不适合用来打包Scrapydweb,原因见构建Windows桌面版Scrapydweb (一)的结尾处。本文尝试借助python embeddable将python解释器、Scrapydweb及其依赖一并打包,再利用Electron构建成一个桌面APP。

下载python embeddable package

www.python.org/downloads/w...

这里我下载的是python 3.9.9 64-bit版。下载后直接解压即完成安装。为方便叙述,这里将解压后的根目录记为python_root

安装Scrapydweb

安装pip

下载pip安装脚本,并用%python_root%/python.exe去执行该脚本。这样,pip等命令会被安装到%python_root%/Scripts目录下。

Warning! 按照上述方法安装的pip命令(即pip.exe)中含有python解释器路径的硬编码,这里即%python_root%/python.exe,当我们移动python embeddable的安装目录时,执行pip命令将因为找不到%python_root%/python.exe而报错。

安装Scrapyd、Scrapydweb

%python_root%/pip.exe install scrapyd scrapydweb

直接使用pip安装即可。依赖包会被安装在%python_root%/Lib/site-packages目录下。由于默认的包搜索路径不包含该目录,因此将该目录添加到%python_root%/python39_pth中,

Warning! 用pip安装的命令同样有上面所述的python解释器路径硬编码的问题。

创建Electron APP

npm init electron-app@latest my-app

编辑src/index.js,在其中启动scrapd和scrapydweb。

Note:

  1. 由于存在硬编码问题,所以这里改为用显式指定python去执行。
  2. 增加3秒后重新加载URL,尽量减少scrapdweb服务未启动完成而导致加载失败的可能。

最终src/index.js如下。

js 复制代码
const { app, BrowserWindow } = require('electron');
const path = require('node:path');

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) {
  app.quit();
}

// python_root = scrapydweb-embed-python
const backendScrapyd = require('child_process').spawn('.\\python.exe' ,['.\\Lib\\site-packages\\scrapyd\\scripts\\scrapyd_run.py'],
{cwd: path.join(app.getPath('exe'), '../scrapydweb-embed-python')});

const backendScrapydweb = require('child_process').spawn('.\\python.exe' ,['.\\Lib\\site-packages\\scrapydweb\\run.py'],
{cwd: path.join(app.getPath('exe'), '../scrapydweb-embed-python')});

const createWindow = () => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
    },
  });

  // and load the index.html of the app.
  // mainWindow.loadFile(path.join(__dirname, 'index.html'));
  mainWindow.loadURL('http://localhost:5000');

  mainWindow.webContents.on('did-fail-load', () => {
	setTimeout(() => {mainWindow.reload()}, 3000);
  });
  
  // Open the DevTools.
  // mainWindow.webContents.openDevTools();
};

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
  createWindow();

  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow();
    }
  });
});

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
  
  if (process.platform !== 'darwin') {
    backendScrapydweb.kill();
    backendScrapyd.kill();
    app.quit();
  }
});

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.

打包Electron APP

npm run make

打包完成后,将out目录中的文件拷贝出来,作为整包的根目录,再将python_root拷贝到该根目录下,压缩即可。

最终整包的目录结构如下。其中,my-app.exe是执行入口。

相关推荐
蹦蹦跳跳真可爱5893 小时前
Python----OpenCV(图像増强——高通滤波(索贝尔算子、沙尔算子、拉普拉斯算子),图像浮雕与特效处理)
人工智能·python·opencv·计算机视觉
nananaij3 小时前
【Python进阶篇 面向对象程序设计(3) 继承】
开发语言·python·神经网络·pycharm
雷羿 LexChien3 小时前
从 Prompt 管理到人格稳定:探索 Cursor AI 编辑器如何赋能 Prompt 工程与人格风格设计(上)
人工智能·python·llm·编辑器·prompt
敲键盘的小夜猫3 小时前
LLM复杂记忆存储-多会话隔离案例实战
人工智能·python·langchain
高压锅_12204 小时前
Django Channels WebSocket实时通信实战:从聊天功能到消息推送
python·websocket·django
胖达不服输5 小时前
「日拱一码」020 机器学习——数据处理
人工智能·python·机器学习·数据处理
吴佳浩5 小时前
Python入门指南-番外-LLM-Fingerprint(大语言模型指纹):从技术视角看AI开源生态的边界与挑战
python·llm·mcp
吴佳浩6 小时前
Python入门指南-AI模型相似性检测方法:技术原理与实现
人工智能·python·llm
叶 落6 小时前
计算阶梯电费
python·python 基础·python 入门
Python大数据分析@7 小时前
Origin、MATLAB、Python 用于科研作图,哪个最好?
开发语言·python·matlab