跳转到主要内容

无需Electron即可构建类似Electron的应用

项目描述

demo app screenshot

native-web-app

Python的webbrowser.open()的替代品,可以打开不带浏览器控制的本地浏览器窗口。无需发布Electron即可构建Electron风格的应用!

import native_web_app

url = "http://localhost:8000/"

try:
    native_web_app.open(url)
except Exception:
    print(f"No web browser found. Please open a browser and point it to {url}.")

演示

在仓库中有一个 示例应用

API文档

此模块公开了一个open函数

def open(url: str, try_app_mode: bool = True) -> None:
    """
    Open a URL in a modern browser.
    In contrast to webbrowser.open, this method gracefully degrades
    to a no-op on headless servers, where webbrowser.open would otherwise open lynx.

    Args:
        url:
            The URL to open, e.g. http://localhost:1234.
        try_app_mode:
            If True, try to open the URL in "app mode", i.e. without browser controls.
            This allows for Electron-like apps without having to deal with Electron.
            If no suitable browser is found, it gracefully falls back to a regular browser instance.

    Raises:
        RuntimeError, if no suitable browser is found.
        OSError, if the browser executable could not be executed.

        For robustness, implementors should catch any Exception and take that as a signal that opening the URL failed.
    """

兼容性

OS 浏览器 状态(✅应用模式,☑️常规浏览器)
Windows 10 (2004) Google Chrome 84
Windows 10 (2004) Microsoft Edge 84
Windows 10 (2004) Windows Subsystem for Linux ☑️
Windows 10 (2004) 默认浏览器 ☑️
Ubuntu 20.04 Google Chrome 84
Ubuntu 20.04 默认浏览器 ☑️
macOS Catalina Google Chrome 84
macOS Catalina 默认浏览器 ☑️

Firefox在2020年实现了应用模式(“特定站点浏览器功能”),但仅对https:// URL启用。这意味着它 无法与http://localhost:1234file:// URL一起使用。这使得它不适合包含在native_web_app中。

变更日志

此项目遵循语义版本控制。

native_web_app 1.0.2 (2020-08-12)

  • 添加对Python 3.5和3.6的支持。

native_web_app 1.0.1 (2020-08-12)

  • 强制浏览器在后台启动。这修复了与Windows 10上Microsoft Edge的兼容性。
  • 扩展兼容性文档。

native_web_app 1.0.0 (2020-08-11)

  • 初始发布

常见问题解答

如何检测浏览器窗口何时关闭?

在各个平台上监控生成的浏览器进程并不稳定。我们建议您使用JavaScript来通知后端。

window.addEventListener('unload', function() {
    navigator.sendBeacon("/shutdown");
}, false);

项目详情


下载文件

下载您平台对应的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码分发

此版本没有可用的源代码分发文件。请参阅有关生成分发存档的教程。

构建分发

native_web_app-1.0.2-py3-none-any.whl (5.2 kB 查看哈希值)

上传时间 Python 3

由以下支持