diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index a1d535a0..a1621116 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -2,10 +2,10 @@ name: "CodeQL" on: push: - branches: [master] + branches: [main] pull_request: # The branches below must be a subset of the branches above - branches: [master] + branches: [main] schedule: - cron: '0 11 * * 0' diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 25a8949c..8f67eff8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,6 +17,8 @@ jobs: with: python-version: 3.x architecture: x64 + - name: Install setuptools + run: pip install setuptools==76.1.0 - name: Build a source distribution run: python setup.py sdist - name: Publish to prod PyPI diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f6c44255..3ae373a8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,17 +2,45 @@ name: Test Eel on: push: - branches: [ master ] + branches: [main] pull_request: + # The branches below must be a subset of the branches above + branches: [main] + workflow_dispatch: jobs: test: + strategy: + fail-fast: false + matrix: + os: [ubuntu-24.04, windows-latest, macos-latest] + python-version: [3.7, 3.8, 3.9, "3.10", "3.11", "3.12"] + exclude: + - os: macos-latest + python-version: 3.7 + - os: ubuntu-24.04 + python-version: 3.7 + runs-on: ${{ matrix.os }} + steps: + - name: Checkout repository + uses: actions/checkout@v2 + - name: Setup python + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Setup test execution environment. + run: pip3 install -r requirements-meta.txt + - name: Run tox tests + run: tox -- --durations=0 --timeout=240 + + typecheck: strategy: matrix: - os: [ubuntu-20.04, windows-latest, macos-latest] - python-version: [3.6, 3.7, 3.8, 3.9, "3.10"] + os: [windows-latest] + + runs-on: ${{ matrix.os }} steps: - name: Checkout repository @@ -20,8 +48,8 @@ jobs: - name: Setup python uses: actions/setup-python@v2 with: - python-version: ${{ matrix.python-version }} + python-version: "3.x" - name: Setup test execution environment. run: pip3 install -r requirements-meta.txt - name: Run tox tests - run: tox -- --durations=0 --timeout=30 + run: tox -e typecheck diff --git a/.gitignore b/.gitignore index e152e6ea..781d9ca4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ __pycache__ dist build +Drivers Eel.egg-info .tmp .DS_Store diff --git a/.python-version b/.python-version index 551bdc68..c8cfe395 100644 --- a/.python-version +++ b/.python-version @@ -1,5 +1 @@ -3.6.15 -3.7.14 -3.8.14 -3.9.13 -3.10.8 \ No newline at end of file +3.10 diff --git a/CHANGELOG.md b/CHANGELOG.md index d973c4a2..b91f88d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,30 @@ # Change log +### 0.18.2 + +* Switch from using `pkg_resources` to `importlib.resources`: https://github.com/python-eel/Eel/pull/766 + +### 0.18.1 + +* Fix: Include `typing_extensions` in install requirements. + +### 0.18.0 +* Added support for MS Internet Explorer in #744. +* Added supported for app_mode in the Edge browser in #744. +* Improved type annotations in #683. + +### 0.17.0 +* Adds support for Python 3.11 and Python 3.12 + +### v0.16.0 +* Drop support for Python versions below 3.7 + +### v0.15.3 +* Comprehensive type hints implement by @thatfloflo in https://github.com/python-eel/Eel/pull/577. + +### v0.15.2 +* Adds `register_eel_routes` to handle applying Eel routes to non-Bottle custom app instances. + ### v0.15.1 * Bump bottle dependency from 0.12.13 to 0.12.20 to address the critical CVE-2022-31799 and moderate CVE-2020-28473. diff --git a/README-developers.md b/README-developers.md index d628b753..a0d00d6c 100644 --- a/README-developers.md +++ b/README-developers.md @@ -29,7 +29,7 @@ pip3 install -r requirements-meta.txt # tox ``` ### (Recommended) Run Automated Tests -Tox is configured to run tests against each major version we support (3.6+). In order to run Tox as configured, you will need to install multiple versions of Python. See the pinned minor versions in `.python-version` for recommendations. +Tox is configured to run tests against each major version we support (3.7+). In order to run Tox as configured, you will need to install multiple versions of Python. See the pinned minor versions in `.python-version` for recommendations. #### Tox Setup Our Tox configuration requires [Chrome](https://www.google.com/chrome) and [ChromeDriver](https://chromedriver.chromium.org/home). See each of those respective project pages for more information on setting each up. @@ -38,7 +38,7 @@ Our Tox configuration requires [Chrome](https://www.google.com/chrome) and [Chro #### Running Tests -To test Eel against a specific version of Python you have installed, e.g. Python 3.6 in this case, run: +To test Eel against a specific version of Python you have installed, e.g. Python 3.7 in this case, run: ```bash tox -e py36 diff --git a/README.md b/README.md index 9d6d1480..a2dfc4dc 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,13 @@ # Eel +> [!CAUTION] +> This project is effectively unmaintained. It has not received regular update in a number of years, and there are no plans by active maintainers for this to change. Please treat this project in that light and use it with careful consideration towards how you will secure and maintain anything you build using it. + [![PyPI version](https://img.shields.io/pypi/v/Eel?style=for-the-badge)](https://pypi.org/project/Eel/) [![PyPi Downloads](https://img.shields.io/pypi/dm/Eel?style=for-the-badge)](https://pypistats.org/packages/eel) ![Python](https://img.shields.io/pypi/pyversions/Eel?style=for-the-badge) [![License](https://img.shields.io/pypi/l/Eel.svg?style=for-the-badge)](https://pypi.org/project/Eel/) - -[![Total alerts](https://img.shields.io/lgtm/alerts/g/samuelhwilliams/Eel.svg?logo=lgtm&style=for-the-badge)](https://lgtm.com/projects/g/samuelhwilliams/Eel/alerts/) -[![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/samuelhwilliams/Eel.svg?logo=lgtm&style=for-the-badge)](https://lgtm.com/projects/g/samuelhwilliams/Eel/context:javascript) -[![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/samuelhwilliams/Eel.svg?logo=lgtm&style=for-the-badge)](https://lgtm.com/projects/g/samuelhwilliams/Eel/context:python) - - Eel is a little Python library for making simple Electron-like offline HTML/JS GUI apps, with full access to Python capabilities and libraries. > **Eel hosts a local webserver, then lets you annotate functions in Python so that they can be called from Javascript, and vice versa.** @@ -106,7 +103,7 @@ Additional options can be passed to `eel.start()` as keyword arguments. Some of the options include the mode the app is in (e.g. 'chrome'), the port the app runs on, the host name of the app, and adding additional command line flags. As of Eel v0.12.0, the following options are available to `start()`: - - **mode**, a string specifying what browser to use (e.g. `'chrome'`, `'electron'`, `'edge'`, `'custom'`). Can also be `None` or `False` to not open a window. *Default: `'chrome'`* + - **mode**, a string specifying what browser to use (e.g. `'chrome'`, `'electron'`, `'edge'`,`'msie'`, `'custom'`). Can also be `None` or `False` to not open a window. *Default: `'chrome'`* - **host**, a string specifying what hostname to use for the Bottle server. *Default: `'localhost'`)* - **port**, an int specifying what port to use for the Bottle server. Use `0` for port to be picked automatically. *Default: `8000`*. - **block**, a bool saying whether or not the call to `start()` should block the calling thread. *Default: `True`* @@ -116,8 +113,7 @@ As of Eel v0.12.0, the following options are available to `start()`: - **position**, a tuple of ints specifying the (left, top) of the main window in pixels *Default: `None`* - **geometry**, a dictionary specifying the size and position for all windows. The keys should be the relative path of the page, and the values should be a dictionary of the form `{'size': (200, 100), 'position': (300, 50)}`. *Default: {}* - **close_callback**, a lambda or function that is called when a websocket to a window closes (i.e. when the user closes the window). It should take two arguments; a string which is the relative path of the page that just closed, and a list of other websockets that are still open. *Default: `None`* - - **app**, an instance of Bottle which will be used rather than creating a fresh one. This can be used to install middleware on the - instance before starting eel, e.g. for session management, authentication, etc. + - **app**, an instance of Bottle which will be used rather than creating a fresh one. This can be used to install middleware on the instance before starting eel, e.g. for session management, authentication, etc. If your `app` is not a Bottle instance, you will need to call `eel.register_eel_routes(app)` on your custom app instance. - **shutdown_delay**, timer configurable for Eel's shutdown detection mechanism, whereby when any websocket closes, it waits `shutdown_delay` seconds, and then checks if there are now any websocket connections. If not, then Eel closes. In case the user has closed the browser and wants to exit the program. By default, the value of **shutdown_delay** is `1.0` second diff --git a/eel/__init__.py b/eel/__init__.py index 1f9d8bf4..34fc7ce1 100644 --- a/eel/__init__.py +++ b/eel/__init__.py @@ -1,62 +1,57 @@ +from __future__ import annotations from builtins import range import traceback from io import open - -from gevent.threading import Timer +from typing import Union, Any, Dict, List, Set, Tuple, Optional, Callable +from typing_extensions import Literal +from eel.types import OptionsDictT, WebSocketT import gevent as gvt import json as jsn import bottle as btl -import bottle.ext.websocket as wbs +try: + import bottle_websocket as wbs +except ImportError: + import bottle.ext.websocket as wbs import re as rgx import os import eel.browsers as brw import pyparsing as pp import random as rnd import sys -import pkg_resources as pkg +import importlib_resources import socket import mimetypes + mimetypes.add_type('application/javascript', '.js') -_eel_js_file = pkg.resource_filename('eel', 'eel.js') -_eel_js = open(_eel_js_file, encoding='utf-8').read() -_websockets = [] -_call_return_values = {} -_call_return_callbacks = {} -_call_number = 0 -_exposed_functions = {} -_js_functions = [] -_mock_queue = [] -_mock_queue_done = set() -_shutdown = None + +# https://setuptools.pypa.io/en/latest/pkg_resources.html +# Use of pkg_resources is deprecated in favor of importlib.resources +# Migration guide: https://importlib-resources.readthedocs.io/en/latest/migration.html +_eel_js_reference = importlib_resources.files('eel') / 'eel.js' +with importlib_resources.as_file(_eel_js_reference) as _eel_js_path: + _eel_js: str = _eel_js_path.read_text(encoding='utf-8') + +_websockets: List[Tuple[Any, WebSocketT]] = [] +_call_return_values: Dict[Any, Any] = {} +_call_return_callbacks: Dict[float, Tuple[Callable[..., Any], Optional[Callable[..., Any]]]] = {} +_call_number: int = 0 +_exposed_functions: Dict[Any, Any] = {} +_js_functions: List[Any] = [] +_mock_queue: List[Any] = [] +_mock_queue_done: Set[Any] = set() +_shutdown: Optional[gvt.Greenlet] = None # Later assigned as global by _websocket_close() +root_path: str # Later assigned as global by init() # The maximum time (in milliseconds) that Python will try to retrieve a return value for functions executing in JS # Can be overridden through `eel.init` with the kwarg `js_result_timeout` (default: 10000) -_js_result_timeout = 10000 - -# All start() options must provide a default value and explanation here -_start_args = { - 'mode': 'chrome', # What browser is used - 'host': 'localhost', # Hostname use for Bottle server - 'port': 8000, # Port used for Bottle server (use 0 for auto) - 'block': True, # Whether start() blocks calling thread - 'jinja_templates': None, # Folder for jinja2 templates - 'cmdline_args': ['--disable-http-cache'], # Extra cmdline flags to pass to browser start - 'size': None, # (width, height) of main window - 'position': None, # (left, top) of main window - 'geometry': {}, # Dictionary of size/position for all windows - 'close_callback': None, # Callback for when all windows have closed - 'app_mode': True, # (Chrome specific option) - 'all_interfaces': False, # Allow bottle server to listen for connections on all interfaces - 'disable_cache': True, # Sets the no-store response header when serving assets - 'default_path': 'index.html', # The default file to retrieve for the root URL - 'app': btl.default_app(), # Allows passing in a custom Bottle instance, e.g. with middleware - 'shutdown_delay': 1.0 # how long to wait after a websocket closes before detecting complete shutdown -} +_js_result_timeout: int = 10000 + +# Attribute holding the start args from calls to eel.start() +_start_args: OptionsDictT = {} # == Temporary (suppressible) error message to inform users of breaking API change for v1.0.0 === -_start_args['suppress_error'] = False -api_error_message = ''' +api_error_message: str = ''' ---------------------------------------------------------------------------------- 'options' argument deprecated in v1.0.0, see https://github.com/ChrisKnott/Eel To suppress this error, add 'suppress_error=True' to start() call. @@ -65,17 +60,53 @@ ''' # =============================================================================================== + # Public functions -def expose(name_or_function=None): + +def expose(name_or_function: Optional[Callable[..., Any]] = None) -> Callable[..., Any]: + '''Decorator to expose Python callables via Eel's JavaScript API. + + When an exposed function is called, a callback function can be passed + immediately afterwards. This callback will be called asynchronously with + the return value (possibly `None`) when the Python function has finished + executing. + + Blocking calls to the exposed function from the JavaScript side are only + possible using the :code:`await` keyword inside an :code:`async function`. + These still have to make a call to the response, i.e. + :code:`await eel.py_random()();` inside an :code:`async function` will work, + but just :code:`await eel.py_random();` will not. + + :Example: + + In Python do: + + .. code-block:: python + + @expose + def say_hello_py(name: str = 'You') -> None: + print(f'{name} said hello from the JavaScript world!') + + In JavaScript do: + + .. code-block:: javascript + + eel.say_hello_py('Alice')(); + + Expected output on the Python console:: + + Alice said hello from the JavaScript world! + + ''' # Deal with '@eel.expose()' - treat as '@eel.expose' if name_or_function is None: return expose - if type(name_or_function) == str: # Called as '@eel.expose("my_name")' + if isinstance(name_or_function, str): # Called as '@eel.expose("my_name")' name = name_or_function - def decorator(function): + def decorator(function: Callable[..., Any]) -> Any: _expose(name, function) return function return decorator @@ -87,7 +118,7 @@ def decorator(function): # PyParsing grammar for parsing exposed functions in JavaScript code # Examples: `eel.expose(w, "func_name")`, `eel.expose(func_name)`, `eel.expose((function (e){}), "func_name")` -EXPOSED_JS_FUNCTIONS = pp.ZeroOrMore( +EXPOSED_JS_FUNCTIONS: pp.ZeroOrMore = pp.ZeroOrMore( pp.Suppress( pp.SkipTo(pp.Literal('eel.expose(')) + pp.Literal('eel.expose(') @@ -101,8 +132,28 @@ def decorator(function): ) -def init(path, allowed_extensions=['.js', '.html', '.txt', '.htm', - '.xhtml', '.vue'], js_result_timeout=10000): +def init( + path: str, + allowed_extensions: List[str] = ['.js', '.html', '.txt', '.htm', '.xhtml', '.vue'], + js_result_timeout: int = 10000) -> None: + '''Initialise Eel. + + This function should be called before :func:`start()` to initialise the + parameters for the web interface, such as the path to the files to be + served. + + :param path: Sets the path on the filesystem where files to be served to + the browser are located, e.g. :file:`web`. + :param allowed_extensions: A list of filename extensions which will be + parsed for exposed eel functions which should be callable from python. + Files with extensions not in *allowed_extensions* will still be served, + but any JavaScript functions, even if marked as exposed, will not be + accessible from python. + *Default:* :code:`['.js', '.html', '.txt', '.htm', '.xhtml', '.vue']`. + :param js_result_timeout: How long Eel should be waiting to register the + results from a call to Eel's JavaScript API before before timing out. + *Default:* :code:`10000` milliseconds. + ''' global root_path, _js_functions, _js_result_timeout root_path = _get_real_path(path) @@ -133,14 +184,117 @@ def init(path, allowed_extensions=['.js', '.html', '.txt', '.htm', _js_result_timeout = js_result_timeout -def start(*start_urls, **kwargs): - _start_args.update(kwargs) - - if 'options' in kwargs: - if _start_args['suppress_error']: - _start_args.update(kwargs['options']) - else: - raise RuntimeError(api_error_message) +def start( + *start_urls: str, + mode: Optional[Union[str, Literal[False]]] = 'chrome', + host: str = 'localhost', + port: int = 8000, + block: bool = True, + jinja_templates: Optional[str] = None, + cmdline_args: List[str] = ['--disable-http-cache'], + size: Optional[Tuple[int, int]] = None, + position: Optional[Tuple[int, int]] = None, + geometry: Dict[str, Tuple[int, int]] = {}, + close_callback: Optional[Callable[..., Any]] = None, + app_mode: bool = True, + all_interfaces: bool = False, + disable_cache: bool = True, + default_path: str = 'index.html', + app: btl.Bottle = btl.default_app(), + shutdown_delay: float = 1.0, + suppress_error: bool = False) -> None: + '''Start the Eel app. + + Suppose you put all the frontend files in a directory called + :file:`web`, including your start page :file:`main.html`, then the app + is started like this: + + .. code-block:: python + + import eel + eel.init('web') + eel.start('main.html') + + This will start a webserver on the default settings + (http://localhost:8000) and open a browser to + http://localhost:8000/main.html. + + If Chrome or Chromium is installed then by default it will open that in + *App Mode* (with the `--app` cmdline flag), regardless of what the OS's + default browser is set to (it is possible to override this behaviour). + + :param mode: What browser is used, e.g. :code:`'chrome'`, + :code:`'electron'`, :code:`'edge'`, :code:`'custom'`. Can also be + `None` or `False` to not open a window. *Default:* :code:`'chrome'`. + :param host: Hostname used for Bottle server. *Default:* + :code:`'localhost'`. + :param port: Port used for Bottle server. Use :code:`0` for port to be + picked automatically. *Default:* :code:`8000`. + :param block: Whether the call to :func:`start()` blocks the calling + thread. *Default:* `True`. + :param jinja_templates: Folder for :mod:`jinja2` templates, e.g. + :file:`my_templates`. *Default:* `None`. + :param cmdline_args: A list of strings to pass to the command starting the + browser. For example, we might add extra flags to Chrome with + :code:`eel.start('main.html', mode='chrome-app', port=8080, + cmdline_args=['--start-fullscreen', '--browser-startup-dialog'])`. + *Default:* :code:`[]`. + :param size: Tuple specifying the (width, height) of the main window in + pixels. *Default:* `None`. + :param position: Tuple specifying the (left, top) position of the main + window in pixels. *Default*: `None`. + :param geometry: A dictionary of specifying the size/position for all + windows. The keys should be the relative path of the page, and the + values should be a dictionary of the form + :code:`{'size': (200, 100), 'position': (300, 50)}`. *Default:* + :code:`{}`. + :param close_callback: A lambda or function that is called when a websocket + or window closes (i.e. when the user closes the window). It should take + two arguments: a string which is the relative path of the page that + just closed, and a list of the other websockets that are still open. + *Default:* `None`. + :param app_mode: Whether to run Chrome/Edge in App Mode. You can also + specify *mode* as :code:`mode='chrome-app'` as a shorthand to start + Chrome in App Mode. + :param all_interfaces: Whether to allow the :mod:`bottle` server to listen + for connections on all interfaces. + :param disable_cache: Sets the no-store response header when serving + assets. + :param default_path: The default file to retrieve for the root URL. + :param app: An instance of :class:`bottle.Bottle` which will be used rather + than creating a fresh one. This can be used to install middleware on + the instance before starting Eel, e.g. for session management, + authentication, etc. If *app* is not a :class:`bottle.Bottle` instance, + you will need to call :code:`eel.register_eel_routes(app)` on your + custom app instance. + :param shutdown_delay: Timer configurable for Eel's shutdown detection + mechanism, whereby when any websocket closes, it waits *shutdown_delay* + seconds, and then checks if there are now any websocket connections. + If not, then Eel closes. In case the user has closed the browser and + wants to exit the program. *Default:* :code:`1.0` seconds. + :param suppress_error: Temporary (suppressible) error message to inform + users of breaking API change for v1.0.0. Set to `True` to suppress + the error message. + ''' + _start_args.update({ + 'mode': mode, + 'host': host, + 'port': port, + 'block': block, + 'jinja_templates': jinja_templates, + 'cmdline_args': cmdline_args, + 'size': size, + 'position': position, + 'geometry': geometry, + 'close_callback': close_callback, + 'app_mode': app_mode, + 'all_interfaces': all_interfaces, + 'disable_cache': disable_cache, + 'default_path': default_path, + 'app': app, + 'shutdown_delay': shutdown_delay, + 'suppress_error': suppress_error, + }) if _start_args['port'] == 0: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -148,37 +302,47 @@ def start(*start_urls, **kwargs): _start_args['port'] = sock.getsockname()[1] sock.close() - if _start_args['jinja_templates'] != None: + if _start_args['jinja_templates'] is not None: from jinja2 import Environment, FileSystemLoader, select_autoescape + if not isinstance(_start_args['jinja_templates'], str): + raise TypeError("'jinja_templates' start_arg/option must be of type str") templates_path = os.path.join(root_path, _start_args['jinja_templates']) - _start_args['jinja_env'] = Environment(loader=FileSystemLoader(templates_path), - autoescape=select_autoescape(['html', 'xml'])) + _start_args['jinja_env'] = Environment( + loader=FileSystemLoader(templates_path), + autoescape=select_autoescape(['html', 'xml']) + ) # verify shutdown_delay is correct value if not isinstance(_start_args['shutdown_delay'], (int, float)): - raise ValueError("`shutdown_delay` must be a number, "\ - "got a {}".format(type(_start_args['shutdown_delay']))) + raise ValueError( + '`shutdown_delay` must be a number, ' + 'got a {}'.format(type(_start_args['shutdown_delay'])) + ) # Launch the browser to the starting URLs show(*start_urls) - def run_lambda(): - if _start_args['all_interfaces'] == True: + def run_lambda() -> None: + if _start_args['all_interfaces'] is True: HOST = '0.0.0.0' else: + if not isinstance(_start_args['host'], str): + raise TypeError("'host' start_arg/option must be of type str") HOST = _start_args['host'] - app = _start_args['app'] # type: btl.Bottle - for route_path, route_params in BOTTLE_ROUTES.items(): - route_func, route_kwargs = route_params - btl.route(path=route_path, callback=route_func, **route_kwargs) + app = _start_args['app'] + + if isinstance(app, btl.Bottle): + register_eel_routes(app) + else: + register_eel_routes(btl.default_app()) - return btl.run( + btl.run( host=HOST, port=_start_args['port'], server=wbs.GeventWebSocketServer, quiet=True, - app=app) + app=app) # Always returns None # Start the webserver if _start_args['block']: @@ -187,20 +351,80 @@ def run_lambda(): spawn(run_lambda) -def show(*start_urls): - brw.open(start_urls, _start_args) +def show(*start_urls: str) -> None: + '''Show the specified URL(s) in the browser. + + Suppose you have two files in your :file:`web` folder. The file + :file:`hello.html` regularly includes :file:`eel.js` and provides + interactivity, and the file :file:`goodbye.html` does not include + :file:`eel.js` and simply provides plain HTML content not reliant on Eel. + + First, we defien a callback function to be called when the browser + window is closed: + .. code-block:: python -def sleep(seconds): + def last_calls(): + eel.show('goodbye.html') + + Now we initialise and start Eel, with a :code:`close_callback` to our + function: + + ..code-block:: python + + eel.init('web') + eel.start('hello.html', mode='chrome-app', close_callback=last_calls) + + When the websocket from :file:`hello.html` is closed (e.g. because the + user closed the browser window), Eel will wait *shutdown_delay* seconds + (by default 1 second), then call our :code:`last_calls()` function, which + opens another window with the :file:`goodbye.html` shown before our Eel app + terminates. + + :param start_urls: One or more URLs to be opened. + ''' + brw.open(list(start_urls), _start_args) + + +def sleep(seconds: Union[int, float]) -> None: + '''A non-blocking sleep call compatible with the Gevent event loop. + + .. note:: + While this function simply wraps :func:`gevent.sleep()`, it is better + to call :func:`eel.sleep()` in your eel app, as this will ensure future + compatibility in case the implementation of Eel should change in some + respect. + + :param seconds: The number of seconds to sleep. + ''' gvt.sleep(seconds) -def spawn(function, *args, **kwargs): +def spawn(function: Callable[..., Any], *args: Any, **kwargs: Any) -> gvt.Greenlet: + '''Spawn a new Greenlet. + + Calling this function will spawn a new :class:`gevent.Greenlet` running + *function* asynchronously. + + .. caution:: + If you spawn your own Greenlets to run in addition to those spawned by + Eel's internal core functionality, you will have to ensure that those + Greenlets will terminate as appropriate (either by returning or by + being killed via Gevent's kill mechanism), otherwise your app may not + terminate correctly when Eel itself terminates. + + :param function: The function to be called and run as the Greenlet. + :param *args: Any positional arguments that should be passed to *function*. + :param **kwargs: Any key-word arguments that should be passed to + *function*. + ''' return gvt.spawn(function, *args, **kwargs) + # Bottle Routes -def _eel(): + +def _eel() -> str: start_geometry = {'default': {'size': _start_args['size'], 'position': _start_args['position']}, 'pages': _start_args['geometry']} @@ -213,12 +437,18 @@ def _eel(): _set_response_headers(btl.response) return page -def _root(): + +def _root() -> btl.Response: + if not isinstance(_start_args['default_path'], str): + raise TypeError("'default_path' start_arg/option must be of type str") return _static(_start_args['default_path']) -def _static(path): + +def _static(path: str) -> btl.Response: response = None if 'jinja_env' in _start_args and 'jinja_templates' in _start_args: + if not isinstance(_start_args['jinja_templates'], str): + raise TypeError("'jinja_templates' start_arg/option must be of type str") template_prefix = _start_args['jinja_templates'] + '/' if path.startswith(template_prefix): n = len(template_prefix) @@ -231,7 +461,8 @@ def _static(path): _set_response_headers(response) return response -def _websocket(ws): + +def _websocket(ws: WebSocketT) -> None: global _websockets for js_function in _js_functions: @@ -257,20 +488,46 @@ def _websocket(ws): _websocket_close(page) -BOTTLE_ROUTES = { +BOTTLE_ROUTES: Dict[str, Tuple[Callable[..., Any], Dict[Any, Any]]] = { "/eel.js": (_eel, dict()), "/": (_root, dict()), "/": (_static, dict()), "/eel": (_websocket, dict(apply=[wbs.websocket])) } + +def register_eel_routes(app: btl.Bottle) -> None: + '''Register the required eel routes with `app`. + + .. note:: + + :func:`eel.register_eel_routes()` is normally invoked implicitly by + :func:`eel.start()` and does not need to be called explicitly in most + cases. Registering the eel routes explicitly is only needed if you are + passing something other than an instance of :class:`bottle.Bottle` to + :func:`eel.start()`. + + :Example: + + >>> app = bottle.Bottle() + >>> eel.register_eel_routes(app) + >>> middleware = beaker.middleware.SessionMiddleware(app) + >>> eel.start(app=middleware) + + ''' + for route_path, route_params in BOTTLE_ROUTES.items(): + route_func, route_kwargs = route_params + app.route(path=route_path, callback=route_func, **route_kwargs) + + # Private functions -def _safe_json(obj): + +def _safe_json(obj: Any) -> str: return jsn.dumps(obj, default=lambda o: None) -def _repeated_send(ws, msg): +def _repeated_send(ws: WebSocketT, msg: str) -> None: for attempt in range(100): try: ws.send(msg) @@ -279,7 +536,7 @@ def _repeated_send(ws, msg): sleep(0.001) -def _process_message(message, ws): +def _process_message(message: Dict[str, Any], ws: WebSocketT) -> None: if 'call' in message: error_info = {} try: @@ -311,47 +568,48 @@ def _process_message(message, ws): print('Invalid message received: ', message) -def _get_real_path(path): +def _get_real_path(path: str) -> str: if getattr(sys, 'frozen', False): - return os.path.join(sys._MEIPASS, path) + return os.path.join(sys._MEIPASS, path) # type: ignore # sys._MEIPASS is dynamically added by PyInstaller else: return os.path.abspath(path) -def _mock_js_function(f): +def _mock_js_function(f: str) -> None: exec('%s = lambda *args: _mock_call("%s", args)' % (f, f), globals()) -def _import_js_function(f): +def _import_js_function(f: str) -> None: exec('%s = lambda *args: _js_call("%s", args)' % (f, f), globals()) -def _call_object(name, args): +def _call_object(name: str, args: Any) -> Dict[str, Any]: global _call_number _call_number += 1 call_id = _call_number + rnd.random() return {'call': call_id, 'name': name, 'args': args} -def _mock_call(name, args): +def _mock_call(name: str, args: Any) -> Callable[[Optional[Callable[..., Any]], Optional[Callable[..., Any]]], Any]: call_object = _call_object(name, args) global _mock_queue _mock_queue += [call_object] return _call_return(call_object) -def _js_call(name, args): +def _js_call(name: str, args: Any) -> Callable[[Optional[Callable[..., Any]], Optional[Callable[..., Any]]], Any]: call_object = _call_object(name, args) for _, ws in _websockets: _repeated_send(ws, _safe_json(call_object)) return _call_return(call_object) -def _call_return(call): +def _call_return(call: Dict[str, Any]) -> Callable[[Optional[Callable[..., Any]], Optional[Callable[..., Any]]], Any]: global _js_result_timeout call_id = call['call'] - def return_func(callback=None, error_callback=None): + def return_func(callback: Optional[Callable[..., Any]] = None, + error_callback: Optional[Callable[..., Any]] = None) -> Any: if callback is not None: _call_return_callbacks[call_id] = (callback, error_callback) else: @@ -362,33 +620,35 @@ def return_func(callback=None, error_callback=None): return return_func -def _expose(name, function): +def _expose(name: str, function: Callable[..., Any]) -> None: msg = 'Already exposed function with name "%s"' % name assert name not in _exposed_functions, msg _exposed_functions[name] = function -def _detect_shutdown(): +def _detect_shutdown() -> None: if len(_websockets) == 0: sys.exit() -def _websocket_close(page): +def _websocket_close(page: str) -> None: global _shutdown close_callback = _start_args.get('close_callback') if close_callback is not None: + if not callable(close_callback): + raise TypeError("'close_callback' start_arg/option must be callable or None") sockets = [p for _, p in _websockets] close_callback(page, sockets) else: - if _shutdown: + if isinstance(_shutdown, gvt.Greenlet): _shutdown.kill() _shutdown = gvt.spawn_later(_start_args['shutdown_delay'], _detect_shutdown) -def _set_response_headers(response): +def _set_response_headers(response: btl.Response) -> None: if _start_args['disable_cache']: # https://stackoverflow.com/a/24748094/280852 response.set_header('Cache-Control', 'no-store') diff --git a/eel/__main__.py b/eel/__main__.py index f5a81601..74910995 100644 --- a/eel/__main__.py +++ b/eel/__main__.py @@ -1,9 +1,11 @@ +from __future__ import annotations import pkg_resources as pkg import PyInstaller.__main__ as pyi import os -from argparse import ArgumentParser +from argparse import ArgumentParser, Namespace +from typing import List -parser = ArgumentParser(description=""" +parser: ArgumentParser = ArgumentParser(description=""" Eel is a little Python library for making simple Electron-like offline HTML/JS GUI apps, with full access to Python capabilities and libraries. """) @@ -17,20 +19,22 @@ type=str, help="Folder including all web files including file as html, css, ico, etc." ) +args: Namespace +unknown_args: List[str] args, unknown_args = parser.parse_known_args() -main_script = args.main_script -web_folder = args.web_folder +main_script: str = args.main_script +web_folder: str = args.web_folder print("Building executable with main script '%s' and web folder '%s'...\n" % (main_script, web_folder)) -eel_js_file = pkg.resource_filename('eel', 'eel.js') -js_file_arg = '%s%seel' % (eel_js_file, os.pathsep) -web_folder_arg = '%s%s%s' % (web_folder, os.pathsep, web_folder) +eel_js_file: str = pkg.resource_filename('eel', 'eel.js') +js_file_arg: str = '%s%seel' % (eel_js_file, os.pathsep) +web_folder_arg: str = '%s%s%s' % (web_folder, os.pathsep, web_folder) -needed_args = ['--hidden-import', 'bottle_websocket', - '--add-data', js_file_arg, '--add-data', web_folder_arg] -full_args = [main_script] + needed_args + unknown_args +needed_args: List[str] = ['--hidden-import', 'bottle_websocket', + '--add-data', js_file_arg, '--add-data', web_folder_arg] +full_args: List[str] = [main_script] + needed_args + unknown_args print('Running:\npyinstaller', ' '.join(full_args), '\n') pyi.run(full_args) diff --git a/eel/browsers.py b/eel/browsers.py index 79639141..183dd905 100644 --- a/eel/browsers.py +++ b/eel/browsers.py @@ -1,53 +1,68 @@ +from __future__ import annotations import subprocess as sps import webbrowser as wbr +from typing import Union, List, Dict, Iterable, Optional +from types import ModuleType +from eel.types import OptionsDictT import eel.chrome as chm import eel.electron as ele import eel.edge as edge +import eel.msIE as ie #import eel.firefox as ffx TODO #import eel.safari as saf TODO -_browser_paths = {} -_browser_modules = {'chrome': chm, - 'electron': ele, - 'edge': edge} +_browser_paths: Dict[str, str] = {} +_browser_modules: Dict[str, ModuleType] = {'chrome': chm, + 'electron': ele, + 'edge': edge, + 'msie':ie} -def _build_url_from_dict(page, options): +def _build_url_from_dict(page: Dict[str, str], options: OptionsDictT) -> str: scheme = page.get('scheme', 'http') host = page.get('host', 'localhost') port = page.get('port', options["port"]) path = page.get('path', '') - return '%s://%s:%d/%s' % (scheme, host, port, path) + if not isinstance(port, (int, str)): + raise TypeError("'port' option must be an integer") + return '%s://%s:%d/%s' % (scheme, host, int(port), path) -def _build_url_from_string(page, options): - base_url = 'http://%s:%d/' % (options['host'], options['port']) +def _build_url_from_string(page: str, options: OptionsDictT) -> str: + if not isinstance(options['port'], (int, str)): + raise TypeError("'port' option must be an integer") + base_url = 'http://%s:%d/' % (options['host'], int(options['port'])) return base_url + page -def _build_urls(start_pages, options): - urls = [] +def _build_urls(start_pages: Iterable[Union[str, Dict[str, str]]], options: OptionsDictT) -> List[str]: + urls: List[str] = [] for page in start_pages: - method = _build_url_from_dict if isinstance( - page, dict) else _build_url_from_string - url = method(page, options) + if isinstance(page, dict): + url = _build_url_from_dict(page, options) + else: + url = _build_url_from_string(page, options) urls.append(url) return urls -def open(start_pages, options): +def open(start_pages: Iterable[Union[str, Dict[str, str]]], options: OptionsDictT) -> None: # Build full URLs for starting pages (including host and port) start_urls = _build_urls(start_pages, options) mode = options.get('mode') - if mode in [None, False]: + if not isinstance(mode, (str, type(None))) and mode is not False: + raise TypeError("'mode' option must by either a string, False, or None") + if mode is None or mode is False: # Don't open a browser pass elif mode == 'custom': # Just run whatever command the user provided + if not isinstance(options['cmdline_args'], list): + raise TypeError("'cmdline_args' option must be of type List[str]") sps.Popen(options['cmdline_args'], stdout=sps.PIPE, stderr=sps.PIPE, stdin=sps.PIPE) elif mode in _browser_modules: @@ -69,10 +84,10 @@ def open(start_pages, options): wbr.open(url) -def set_path(browser_name, path): +def set_path(browser_name: str, path: str) -> None: _browser_paths[browser_name] = path -def get_path(browser_name): +def get_path(browser_name: str) -> Optional[str]: return _browser_paths.get(browser_name) diff --git a/eel/chrome.py b/eel/chrome.py index f827b20b..a8112f5d 100644 --- a/eel/chrome.py +++ b/eel/chrome.py @@ -1,22 +1,30 @@ -import sys, subprocess as sps, os +from __future__ import annotations +import sys +import os +import subprocess as sps +from shutil import which +from typing import List, Optional +from eel.types import OptionsDictT # Every browser specific module must define run(), find_path() and name like this -name = 'Google Chrome/Chromium' +name: str = 'Google Chrome/Chromium' -def run(path, options, start_urls): +def run(path: str, options: OptionsDictT, start_urls: List[str]) -> None: + if not isinstance(options['cmdline_args'], list): + raise TypeError("'cmdline_args' option must be of type List[str]") if options['app_mode']: for url in start_urls: sps.Popen([path, '--app=%s' % url] + options['cmdline_args'], stdout=sps.PIPE, stderr=sps.PIPE, stdin=sps.PIPE) else: - args = options['cmdline_args'] + start_urls + args: List[str] = options['cmdline_args'] + start_urls sps.Popen([path, '--new-window'] + args, stdout=sps.PIPE, stderr=sys.stderr, stdin=sps.PIPE) -def find_path(): +def find_path() -> Optional[str]: if sys.platform in ['win32', 'win64']: return _find_chrome_win() elif sys.platform == 'darwin': @@ -27,7 +35,7 @@ def find_path(): return None -def _find_chrome_mac(): +def _find_chrome_mac() -> Optional[str]: default_dir = r'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' if os.path.exists(default_dir): return default_dir @@ -39,7 +47,7 @@ def _find_chrome_mac(): return None -def _find_chromium_mac(): +def _find_chromium_mac() -> Optional[str]: default_dir = r'/Applications/Chromium.app/Contents/MacOS/Chromium' if os.path.exists(default_dir): return default_dir @@ -51,23 +59,23 @@ def _find_chromium_mac(): return None -def _find_chrome_linux(): - import whichcraft as wch +def _find_chrome_linux() -> Optional[str]: chrome_names = ['chromium-browser', 'chromium', 'google-chrome', 'google-chrome-stable'] for name in chrome_names: - chrome = wch.which(name) + chrome = which(name) if chrome is not None: return chrome return None -def _find_chrome_win(): +def _find_chrome_win() -> Optional[str]: import winreg as reg reg_path = r'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe' + chrome_path: Optional[str] = None for install_type in reg.HKEY_CURRENT_USER, reg.HKEY_LOCAL_MACHINE: try: diff --git a/eel/edge.py b/eel/edge.py index cef818aa..7d785233 100644 --- a/eel/edge.py +++ b/eel/edge.py @@ -1,16 +1,27 @@ +from __future__ import annotations import platform import subprocess as sps import sys +from typing import List -name = 'Edge' +from eel.types import OptionsDictT +name: str = 'Edge' -def run(_path, options, start_urls): - 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 not isinstance(options['cmdline_args'], list): + raise TypeError("'cmdline_args' option must be of type List[str]") + args: List[str] = options['cmdline_args'] + if options['app_mode']: + cmd = 'start msedge --app={} '.format(start_urls[0]) + cmd = cmd + (" ".join(args)) + sps.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=sps.PIPE, shell=True) + else: + cmd = "start msedge --new-window "+(" ".join(args)) +" "+(start_urls[0]) + sps.Popen(cmd,stdout=sys.stdout, stderr=sys.stderr, stdin=sps.PIPE, shell=True) -def find_path(): +def find_path() -> bool: if platform.system() == 'Windows': return True diff --git a/eel/electron.py b/eel/electron.py index 7a443025..4ff8fcc1 100644 --- a/eel/electron.py +++ b/eel/electron.py @@ -1,24 +1,29 @@ +from __future__ import annotations import sys import os import subprocess as sps -import whichcraft as wch +from shutil import which +from typing import List, Optional -name = 'Electron' +from eel.types import OptionsDictT -def run(path, options, start_urls): +name: str = 'Electron' + +def run(path: str, options: OptionsDictT, start_urls: List[str]) -> None: + if not isinstance(options['cmdline_args'], list): + raise TypeError("'cmdline_args' option must be of type List[str]") cmd = [path] + options['cmdline_args'] cmd += ['.', ';'.join(start_urls)] sps.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr, stdin=sps.PIPE) -def find_path(): +def find_path() -> Optional[str]: if sys.platform in ['win32', 'win64']: # It doesn't work well passing the .bat file to Popen, so we get the actual .exe - bat_path = wch.which('electron') - return os.path.join(bat_path, r'..\node_modules\electron\dist\electron.exe') + bat_path = which('electron') + if bat_path: + return os.path.join(bat_path, r'..\node_modules\electron\dist\electron.exe') elif sys.platform in ['darwin', 'linux']: - # This should work find... - return wch.which('electron') - else: - return None - + # This should work fine... + return which('electron') + return None diff --git a/eel/msIE.py b/eel/msIE.py new file mode 100644 index 00000000..7f7f2e26 --- /dev/null +++ b/eel/msIE.py @@ -0,0 +1,20 @@ +import platform +import subprocess as sps +import sys +from typing import List + +from eel.types import OptionsDictT + +name: str = 'MSIE' + + +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 find_path() -> bool: + if platform.system() == 'Windows': + return True + + return False diff --git a/eel/py.typed b/eel/py.typed new file mode 100644 index 00000000..e69de29b diff --git a/eel/types.py b/eel/types.py new file mode 100644 index 00000000..0cd2d6ee --- /dev/null +++ b/eel/types.py @@ -0,0 +1,42 @@ +from __future__ import annotations +from typing import Union, Dict, List, Tuple, Callable, Optional, Any, TYPE_CHECKING +from typing_extensions import Literal, TypedDict, TypeAlias +from bottle import Bottle + +# This business is slightly awkward, but needed for backward compatibility, +# because Python <3.10 doesn't support TypeAlias, jinja2 may not be available +# at runtime, and geventwebsocket.websocket doesn't have type annotations so +# that direct imports will raise an error. +if TYPE_CHECKING: + from jinja2 import Environment + JinjaEnvironmentT: TypeAlias = Environment + from geventwebsocket.websocket import WebSocket + WebSocketT: TypeAlias = WebSocket +else: + JinjaEnvironmentT: TypeAlias = Any + WebSocketT: TypeAlias = Any + +OptionsDictT = TypedDict( + 'OptionsDictT', + { + 'mode': Optional[Union[str, Literal[False]]], + 'host': str, + 'port': int, + 'block': bool, + 'jinja_templates': Optional[str], + 'cmdline_args': List[str], + 'size': Optional[Tuple[int, int]], + 'position': Optional[Tuple[int, int]], + 'geometry': Dict[str, Tuple[int, int]], + 'close_callback': Optional[Callable[..., Any]], + 'app_mode': bool, + 'all_interfaces': bool, + 'disable_cache': bool, + 'default_path': str, + 'app': Bottle, + 'shutdown_delay': float, + 'suppress_error': bool, + 'jinja_env': JinjaEnvironmentT, + }, + total=False +) diff --git a/examples/04 - file_access/README.md b/examples/04 - file_access/README.md new file mode 100644 index 00000000..e24ed93e --- /dev/null +++ b/examples/04 - file_access/README.md @@ -0,0 +1,3 @@ +# Example 4 - file access + +![Screenshot](Screenshot.png) diff --git a/examples/07 - CreateReactApp/README.md b/examples/07 - CreateReactApp/README.md index f2b0f249..3f6a85b6 100644 --- a/examples/07 - CreateReactApp/README.md +++ b/examples/07 - CreateReactApp/README.md @@ -19,7 +19,7 @@ If you run into any issues with this example, open a [new issue](https://github. ## Quick Start -1. **Configure:** In the app's directory, run `npm install` and `pip install bottle bottle-websocket future whichcraft pyinstaller` +1. **Configure:** In the app's directory, run `npm install` and `pip install bottle bottle-websocket future pyinstaller` 2. **Demo:** Build static files with `npm run build` then run the application with `python eel_CRA.py`. A Chrome-app window should open running the built code from `build/` 3. **Distribute:** (Run `npm run build` first) Build a binary distribution with PyInstaller using `python -m eel eel_CRA.py build --onefile` (See more detailed PyInstaller instructions at bottom of [the main README](https://github.com/ChrisKnott/Eel)) 4. **Develop:** Open two prompts. In one, run `python eel_CRA.py true` and the other, `npm start`. A browser window should open in your default web browser at: [http://localhost:3000/](http://localhost:3000/). As you make changes to the JavaScript in `src/` the browser will reload. Any changes to `eel_CRA.py` will require a restart to take effect. You may need to refresh the browser window if it gets out of sync with eel. diff --git a/examples/09 - Eelectron-quick-start/hello.py b/examples/09 - Eelectron-quick-start/hello.py index c10d9fb7..a59975e1 100644 --- a/examples/09 - Eelectron-quick-start/hello.py +++ b/examples/09 - Eelectron-quick-start/hello.py @@ -1,5 +1,4 @@ import eel - # Set web files folder eel.init('web') @@ -10,11 +9,5 @@ def say_hello_py(x): say_hello_py('Python World!') eel.say_hello_js('Python World!') # Call a Javascript function - -options = { - 'mode': 'custom', - 'args': ['node_modules/electron/dist/electron.exe', '.'] -} - -eel.start('hello.html', options=options) +eel.start('hello.html',mode='electron') #eel.start('hello.html', mode='custom', cmdline_args=['node_modules/electron/dist/electron.exe', '.']) diff --git a/examples/10 - custom_app_routes/custom_app.py b/examples/10 - custom_app_routes/custom_app.py new file mode 100644 index 00000000..a95ab082 --- /dev/null +++ b/examples/10 - custom_app_routes/custom_app.py @@ -0,0 +1,17 @@ +import eel +import bottle +# from beaker.middleware import SessionMiddleware + +app = bottle.Bottle() +@app.route('/custom') +def custom_route(): + return 'Hello, World!' + +eel.init('web') + +# need to manually add eel routes if we are wrapping our Bottle instance with middleware +# eel.add_eel_routes(app) +# middleware = SessionMiddleware(app) +# eel.start('index.html', app=middleware) + +eel.start('index.html', app=app) diff --git a/examples/10 - custom_app_routes/web/index.html b/examples/10 - custom_app_routes/web/index.html new file mode 100644 index 00000000..e802b567 --- /dev/null +++ b/examples/10 - custom_app_routes/web/index.html @@ -0,0 +1,8 @@ + + + + + Hello, World! + + + diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 00000000..4be68351 --- /dev/null +++ b/mypy.ini @@ -0,0 +1,30 @@ +[mypy] +python_version = 3.10 +warn_unused_configs = True + +[mypy-bottle_websocket] +ignore_missing_imports = True + +[mypy-gevent] +ignore_missing_imports = True + +[mypy-gevent.threading] +ignore_missing_imports = True + +[mypy-geventwebsocket.websocket] +ignore_missing_imports = True + +[mypy-bottle] +ignore_missing_imports = True + +[mypy-bottle.ext] +ignore_missing_imports = True + +[mypy-bottle.ext.websocket] +ignore_missing_imports = True + +[mypy-PyInstaller] +ignore_missing_imports = True + +[mypy-PyInstaller.__main__] +ignore_missing_imports = True diff --git a/requirements-meta.txt b/requirements-meta.txt index 0ebe9256..db1fb74b 100644 --- a/requirements-meta.txt +++ b/requirements-meta.txt @@ -1,4 +1,5 @@ tox>=3.15.2,<4.0.0 tox-pyenv==1.1.0 -tox-gh-actions==1.3.0 +tox-gh-actions==2.0.0 virtualenv>=16.7.10 +setuptools diff --git a/requirements-test.txt b/requirements-test.txt index a773aeb6..ec2da232 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -1,6 +1,11 @@ .[jinja2] -psutil==5.9.2 -pytest==7.0.1 -pytest-timeout==2.1.0 -selenium==3.141.0 +psutil>=5.0.0,<6.0.0 +pytest>=7.0.0,<8.0.0 +pytest-timeout>=2.0.0,<3.0.0 +selenium>=4.0.0,<5.0.0 +webdriver_manager>=4.0.0,<5.0.0 +mypy>=1.0.0,<2.0.0 +pyinstaller +types-setuptools +importlib_resources>=1.3 diff --git a/requirements.txt b/requirements.txt index 3ed6fe71..77d8646e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,8 @@ -bottle==0.12.20 -bottle-websocket==0.2.9 -gevent==1.3.6 -gevent-websocket==0.10.1 -greenlet==0.4.15 -pyparsing==2.4.7 -whichcraft==0.4.1 +bottle<1.0.0 +bottle-websocket<1.0.0 +gevent +gevent-websocket<1.0.0 +greenlet>=1.0.0,<2.0.0 +pyparsing>=3.0.0,<4.0.0 +typing-extensions>=4.3.0 +importlib_resources>=1.3 diff --git a/setup.py b/setup.py index 4e2c0250..000dfbeb 100644 --- a/setup.py +++ b/setup.py @@ -6,19 +6,19 @@ setup( name='Eel', - version='0.15.1', + version='0.18.2', author='Python Eel Organisation', author_email='python-eel@protonmail.com', url='https://github.com/python-eel/Eel', packages=['eel'], package_data={ - 'eel': ['eel.js'], + 'eel': ['eel.js', 'py.typed'], }, - install_requires=['bottle', 'bottle-websocket', 'future', 'pyparsing', 'whichcraft'], + install_requires=['bottle', 'bottle-websocket', 'future', 'pyparsing', 'typing_extensions', 'importlib_resources'], extras_require={ "jinja2": ['jinja2>=2.10'] }, - python_requires='>=3.6', + python_requires='>=3.7', description='For little HTML GUI applications, with easy Python/JS interop', long_description=long_description, long_description_content_type='text/markdown', @@ -30,7 +30,6 @@ 'Operating System :: POSIX', 'Operating System :: Microsoft :: Windows :: Windows 10', 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: Implementation :: CPython', diff --git a/tests/conftest.py b/tests/conftest.py index b3c3a66f..30ca7198 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,9 +1,11 @@ import os +import platform from unittest import mock import pytest from selenium import webdriver -from selenium.webdriver import DesiredCapabilities +from selenium.webdriver.chrome.service import Service as ChromeService +from webdriver_manager.chrome import ChromeDriverManager @pytest.fixture @@ -12,11 +14,16 @@ def driver(): if TEST_BROWSER == "chrome": options = webdriver.ChromeOptions() - options.headless = True - capabilities = DesiredCapabilities.CHROME - capabilities['goog:loggingPrefs'] = {"browser": "ALL"} + options.add_argument('--headless=new') + options.set_capability("goog:loggingPrefs", {"browser": "ALL"}) - driver = webdriver.Chrome(options=options, desired_capabilities=capabilities, service_log_path=os.path.devnull) + if platform.system() == "Windows": + options.binary_location = "C:/Program Files/Google/Chrome/Application/chrome.exe" + + driver = webdriver.Chrome( + service=ChromeService(ChromeDriverManager().install()), + options=options, + ) # Firefox doesn't currently supported pulling JavaScript console logs, which we currently scan to affirm that # JS/Python can communicate in some places. So for now, we can't really use firefox/geckodriver during testing. diff --git a/tests/integration/test_examples.py b/tests/integration/test_examples.py index 63119ef7..53dff2a0 100644 --- a/tests/integration/test_examples.py +++ b/tests/integration/test_examples.py @@ -1,4 +1,5 @@ import os +import time from tempfile import TemporaryDirectory, NamedTemporaryFile from selenium import webdriver @@ -45,11 +46,12 @@ def test_04_file_access(driver: webdriver.Remote): assert driver.title == "Eel Demo" with TemporaryDirectory() as temp_dir, NamedTemporaryFile(dir=temp_dir) as temp_file: - driver.find_element_by_id('input-box').clear() - driver.find_element_by_id('input-box').send_keys(temp_dir) - driver.find_element_by_css_selector('button').click() + driver.find_element(value='input-box').clear() + driver.find_element(value='input-box').send_keys(temp_dir) + time.sleep(0.5) + driver.find_element(By.CSS_SELECTOR, 'button').click() - assert driver.find_element_by_id('file-name').text == os.path.basename(temp_file.name) + assert driver.find_element(value='file-name').text == os.path.basename(temp_file.name) def test_06_jinja_templates(driver: webdriver.Remote): @@ -57,5 +59,19 @@ def test_06_jinja_templates(driver: webdriver.Remote): driver.get(eel_url) assert driver.title == "Hello, World!" - driver.find_element_by_css_selector('a').click() + driver.find_element(By.CSS_SELECTOR, 'a').click() WebDriverWait(driver, 2.0).until(expected_conditions.presence_of_element_located((By.XPATH, '//h1[text()="This is page 2"]'))) + + +def test_10_custom_app(driver: webdriver.Remote): + # test default eel routes are working + with get_eel_server('examples/10 - custom_app_routes/custom_app.py', 'index.html') as eel_url: + driver.get(eel_url) + # we really need to test if the page 404s, but selenium has no support for status codes + # so we just test if we can get our page title + assert driver.title == 'Hello, World!' + + # test custom routes are working + with get_eel_server('examples/10 - custom_app_routes/custom_app.py', 'custom') as eel_url: + driver.get(eel_url) + assert 'Hello, World!' in driver.page_source diff --git a/tests/utils.py b/tests/utils.py index 1fdd8f8a..ff14d473 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,5 +1,7 @@ import contextlib import os +import sys +import platform import subprocess import tempfile import time @@ -8,15 +10,30 @@ import psutil # Path to the test data folder. -TEST_DATA_DIR = Path(__file__).parent / 'data' +TEST_DATA_DIR = Path(__file__).parent / "data" def get_process_listening_port(proc): - psutil_proc = psutil.Process(proc.pid) - while not any(conn.status == 'LISTEN' for conn in psutil_proc.connections()): - time.sleep(0.01) - - conn = next(filter(lambda conn: conn.status == 'LISTEN', psutil_proc.connections())) + conn = None + if platform.system() == "Windows": + current_process = psutil.Process(proc.pid) + children = [] + while children == []: + time.sleep(0.01) + children = current_process.children(recursive=True) + if (3, 6) <= sys.version_info < (3, 7): + children = [current_process] + for child in children: + while child.connections() == [] and not any(conn.status == "LISTEN" for conn in child.connections()): + time.sleep(0.01) + + conn = next(filter(lambda conn: conn.status == "LISTEN", child.connections())) + else: + psutil_proc = psutil.Process(proc.pid) + while not any(conn.status == "LISTEN" for conn in psutil_proc.connections()): + time.sleep(0.01) + + conn = next(filter(lambda conn: conn.status == "LISTEN", psutil_proc.connections())) return conn.laddr.port @@ -40,8 +57,10 @@ def get_eel_server(example_py, start_html): import {os.path.splitext(os.path.basename(example_py))[0]} """) - - proc = subprocess.Popen(['python', test.name], cwd=os.path.dirname(example_py)) + proc = subprocess.Popen( + [sys.executable, test.name], + cwd=os.path.dirname(example_py), + ) eel_port = get_process_listening_port(proc) yield f"http://localhost:{eel_port}/{start_html}" diff --git a/tox.ini b/tox.ini index fe783da7..0d3838e4 100644 --- a/tox.ini +++ b/tox.ini @@ -1,21 +1,31 @@ [tox] -envlist = py36,py37,py38,py39,py310 +envlist = typecheck,py{37,38,39,310,311,312,313} [pytest] -timeout = 5 +timeout = 30 [gh-actions] python = - 3.6: py36 3.7: py37 3.8: py38 3.9: py39 3.10: py310 + 3.11: py311 + 3.12: py312 + 3.13: py313 + [testenv] +description = run py.test tests deps = -r requirements-test.txt commands = # this ugly hack is here because: # https://github.com/tox-dev/tox/issues/149 - pip install -q -r {toxinidir}/requirements-test.txt - {envpython} -m pytest {posargs} + pip install -q -r '{toxinidir}'/requirements-test.txt + '{envpython}' -m pytest {posargs} + +[testenv:typecheck] +description = run type checks +deps = -r requirements-test.txt +commands = + mypy --strict {posargs:eel}