From 5c960743e98ba1c4f5024bfdcda98f759cbf0044 Mon Sep 17 00:00:00 2001 From: Florian Date: Sat, 1 Apr 2023 00:03:57 +0100 Subject: [PATCH] Make Eel work with Microsoft Edge on Linux --- README.md | 2 +- eel/edge.py | 33 ++++++++++++++++++------- examples/01 - hello_world-Edge/hello.py | 29 ++++++++++------------ 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 6b70d27d..8b3d5a13 100644 --- a/README.md +++ b/README.md @@ -344,7 +344,7 @@ Consult the [documentation for PyInstaller](http://PyInstaller.readthedocs.io/en ## Microsoft Edge -For Windows 10 users, Microsoft Edge (`eel.start(.., mode='edge')`) is installed by default and a useful fallback if a preferred browser is not installed. See the examples: +Generally, Microsoft Edge can be installed on both Windows and Linux. However, for users of Windows 10 and 11, Microsoft Edge (`eel.start(.., mode='edge')`) is installed by default, making it a useful fallback if an otherwise preferred browser is not installed. See these examples: - A Hello World example using Microsoft Edge: [examples/01 - hello_world-Edge/](https://github.com/ChrisKnott/Eel/tree/master/examples/01%20-%20hello_world-Edge) - Example implementing browser-fallbacks: [examples/07 - CreateReactApp/eel_CRA.py](https://github.com/ChrisKnott/Eel/tree/master/examples/07%20-%20CreateReactApp/eel_CRA.py) diff --git a/eel/edge.py b/eel/edge.py index 7f2dab1e..bacb4886 100644 --- a/eel/edge.py +++ b/eel/edge.py @@ -1,20 +1,35 @@ -import platform import subprocess as sps import sys -from typing import List +from typing import List, Optional from eel.types import OptionsDictT name: str = 'Edge' -def run(_path: str, options: OptionsDictT, start_urls: List[str]) -> None: - cmd = 'start microsoft-edge:{}'.format(start_urls[0]) - sps.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=sps.PIPE, shell=True) +def run(path: str, options: OptionsDictT, start_urls: List[str]) -> None: + if path.startswith('start microsoft-edge:'): + cmd = 'start microsoft-edge:{}'.format(start_urls[0]) + sps.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=sps.PIPE, shell=True) + else: + args: List[str] = options['cmdline_args'] + start_urls # type: ignore + sps.Popen([path, '--new-window'] + args, + stdout=sps.PIPE, stderr=sys.stderr, stdin=sps.PIPE) -def find_path() -> bool: - if platform.system() == 'Windows': - return True +def find_path() -> Optional[str]: + if sys.platform in ['win32', 'win64']: + return _find_edge_win() + elif sys.platform.startswith('linux'): + return _find_edge_linux() + else: + return None - return False + +def _find_edge_linux() -> Optional[str]: + import whichcraft as wch + return wch.which('microsoft-edge') # type: ignore # whichcraft doesn't currently have type hints + + +def _find_edge_win() -> str: + return 'start microsoft-edge:' diff --git a/examples/01 - hello_world-Edge/hello.py b/examples/01 - hello_world-Edge/hello.py index 965b9d5b..e324c639 100644 --- a/examples/01 - hello_world-Edge/hello.py +++ b/examples/01 - hello_world-Edge/hello.py @@ -1,5 +1,4 @@ import os -import platform import sys # Use latest version of Eel from parent directory @@ -21,18 +20,16 @@ def say_hello_py(x): say_hello_py('Python World!') eel.say_hello_js('Python World!') # Call a Javascript function -# Launch example in Microsoft Edge only on Windows 10 and above -if sys.platform in ['win32', 'win64'] and int(platform.release()) >= 10: - eel.start('hello.html', mode='edge') -else: - raise EnvironmentError('Error: System is not Windows 10 or above') - -# # Launching Edge can also be gracefully handled as a fall back -# try: -# eel.start('hello.html', mode='chrome-app', size=(300, 200)) -# except EnvironmentError: -# # If Chrome isn't found, fallback to Microsoft Edge on Win10 or greater -# if sys.platform in ['win32', 'win64'] and int(platform.release()) >= 10: -# eel.start('hello.html', mode='edge') -# else: -# raise +# Set parameters irrespective of browser choice +start_page = 'hello.html' +window_size = (300, 200) +# Launch example in Microsoft Edge if found +try: + eel.start(start_page, mode='edge', size=window_size) +except EnvironmentError as exc1: + # If Edge isn't found, attempt fallback to Chrome or raise error + try: + print("Try chrome...") + eel.start(start_page, mode='chrome', size=window_size) + except EnvironmentError as exc2: + raise EnvironmentError(f'{exc1} AND {exc2}')