electron-egg electron-egg
首页
  • v3.x
  • v2.x
插件
API
demo
支持
知识点
案例
交流
  • GitHub (opens new window)
  • Gitee (opens new window)
首页
  • v3.x
  • v2.x
插件
API
demo
支持
知识点
案例
交流
  • GitHub (opens new window)
  • Gitee (opens new window)
❤️成为赞助商
  • 快速入门

  • 基础功能

  • 生成软件

  • 升级

  • 跨语言支持

    • 介绍
    • go

    • java

    • python

      • 开始
      • 配置
      • 开发
        • 业务
    • 更新记录
    • 常见问题
    目录

    开发

    python 3.8.10

    如果使用pyhon,那么框架可分为3个部分。

    • 前端UI
    • electron主进程
    • python业务进程

    # 概念

    # 前端UI

    负责软件界面展示和效果,你可以使用任意前端技术,请参见文档:前端模块

    # electron

    涉及操作系统功能的业务,例如 调用操作系统api、打开文件夹、系统弹窗、等等;请参见文档:基础功能 部分

    # python

    利用python来编写业务。

    # 目录结构

    project
    ├── ...
    ├── python 业务目录
        ├── dist 使用 cx_Freeze 构建的可执行程序
        ├── main.py 入口 demo为 flask框架
        ├── requirements.txt python依赖
        ├── setup.py 模块 cx_Freeze 的构建脚本
        ├── fastapi-demo.py    
    

    # 开发模式

    编辑 package.json

      "scripts": {
        "dev": "ee-bin dev",
        "dev-frontend": "ee-bin dev --serve=frontend",
        "dev-electron": "ee-bin dev --serve=electron",
      }
    

    启动渲染进程、主进程

    # 打开两个终端
    npm run dev-frontend
    
    npm run dev-electron
    

    注1:每次 electron 代码更新后,需重新运行npm run dev-electron。

    注2:目前的问题,(dev)如果应用运行时启动python服务,ctrl+c退出时,python进程无法完全退出(原因未知),需要关闭终端。

    因此建议:先单独开发、调试 python 接口。

    # 单独开发、调试 python项目

    1. 编辑./electron/config/bin.js
    // bin开发配置
      /**
       * 执行自定义命令
       * ee-bin exec
       */
      exec: {
        python: {
          directory: './python',
          cmd: 'python',
          args: ['./main.py', '--port=7074'],
          stdio: "inherit", // ignore
        },
    
    1. 编辑./package.json
      "scripts": {
        "dev-python": "ee-bin exec --cmds=python",
      }
    
    1. 调试
    npm run dev-python
    

    # 在electron中如何运行python项目

    提供两种启动方式。1:通过配置启动;2:通过cross模块的API来启动。

    # 方式一:配置模式

    请查看文档:配置

    执行命令npm run dev-electron时,会自动拉起python项目/程序。

    # 方式二:API模式 (推荐)

    通过API创建python服务

      // 文件 electron/service/cross.js
    
      /**
       * create python service
       * In the default configuration, services can be started with applications. 
       * Developers can turn off the configuration and create it manually.
       */   
      async createPythonServer() {
        // method 1: Use the default Settings
        //const entity = await Cross.run(serviceName);
    
        // method 2: Use custom configuration
        const serviceName = "python";
        const opt = {
          name: 'pyapp',
          cmd: path.join(Ps.getExtraResourcesDir(), 'py', 'pyapp'),
          directory: path.join(Ps.getExtraResourcesDir(), 'py'),
          args: ['--port=7074'],
          windowsExtname: true,
          appExit: true,
        }
        const entity = await Cross.run(serviceName, opt);
        Log.info('server name:', entity.name);
        Log.info('server config:', entity.config);
        Log.info('server url:', entity.getUrl());
    
        return;
      } 
    

    # 跟随软件启动

    如果你希望桌面软件运行时就启动python可执行程序,有以下两种方式。

    一:通过config.default.js中的配置:查看

    二:在预加载模块,直接引入Services并调用。

    // 文件 electron/preload/index.js
    
    /*************************************************
     ** preload为预加载模块,该文件将会在程序启动时加载 **
     *************************************************/
    const Addon = require('ee-core/addon');
    const Services = require('ee-core/services');
    
    /**
     * 预加载模块入口
     */
    module.exports = async () => {
    
      // 直接调用
      Services.get('cross').createPythonServer();
    }
    
    上次更新: 2024/12/26, 10:28:21
    配置
    业务

    ← 配置 业务→

    Theme by Vdoing | Copyright © 2023-2024 哆啦好梦 | 京ICP备15041380号-2
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式
    ×