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   荣锋亮  阅读(7)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· spring官宣接入deepseek,真的太香了~
历史上的今天:
2024-02-23 dremio cloner 简单试用
2023-02-23 基于tmpfs 的nginx cache 优化
2022-02-23 基于golang cgi 实现一个简单的git http server
2021-02-23 dremio 自定义connector 图标
2021-02-23 dremio sql server 自定义connector开发解决中文乱码问题
2020-02-23 postgres pg_cron 扩展试用
2020-02-23 pgspider pg_cron 以及cstore fdw docker 镜像

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示