Eel 基于python+浏览器的开发类似electron 应用

Eel 是基于基于python 可以方便开发基于浏览器的桌面应用,内部eel 可以通过@eel.expose 实现js 与python 代码的互通信

包含的特点

  • 使用简单
  • 支持js 与python 代码的互相调用
  • 支持回掉以及同步处理,当然还支持python 的异步处理
  • 提供了基于pyinstaller 的快速打包(支持文件夹以及单文件模式)
  • 内部基于bottle web 框架

简单示例

  • 项目结构

eel 推荐web 以及python 代码会不同的位置,同时对于web 有一个独立的文件夹

├── app.py
└── webapp
    └── main.html
  • app.py
import eel

@eel.expose
def say_hello_py(str):
    print(f"Hello from python {str}")

eel.init('webapp')
eel.start('main.html',mode="chrome", size=(300, 200))
  • webapp/main.html
<!DOCTYPE html>
<html>
  <head>
    <title>Hello, World!</title>

    <!-- Include eel.js - note this file doesn't exist in the 'web' directory -->
    <script type="text/javascript" src="/eel.js"></script>
    <script type="text/javascript">
      eel.expose(say_hello_js); // Expose this function to Python
      function say_hello_js(x) {
        console.log("Hello from " + x);
      }
      say_hello_js("Javascript World!");
      eel.say_hello_py("python World!"); // Call a Python function

      function newwin(){
        window.open("https://www.baidu.com", "_blank");
      }
    </script>
  </head>

  <body>
    Hello, World!
    <input type="button" onclick="eel.say_hello_py('button!')" value="Click me!" />

    <input type="button" onclick="newwin()" value="new win!" />

  </body>
</html>
  • 运行效果

  • 打包

注意需要安装pyinstaller

python -m eel app.py webapp 

效果

说明

基于eel进行桌面开发也是一个不错的选择,值得尝试下

参考资料

https://github.com/python-eel/Eel

https://bottlepy.org/docs/dev/

https://github.com/bottlepy/bottle

posted on 2025-02-23 08:00  荣锋亮  阅读(197)  评论(0)    收藏  举报

导航