From 55ec9817c6ac675a0821cf21e8b010fae9877714 Mon Sep 17 00:00:00 2001 From: ndonkoHenri Date: Sun, 27 Jul 2025 06:10:17 +0200 Subject: [PATCH 1/6] initial commit --- .github/workflows/docs.yml | 1 + .gitignore | 135 +++++++++++++++++++++++++- .pre-commit-config.yaml | 30 ++++-- docs/types/log_level_severity.md | 4 +- docs/types/request_method.md | 4 +- mkdocs.yml | 12 +-- pyproject.toml | 45 ++++++--- src/flet_webview/__init__.py | 9 ++ src/flet_webview/types.py | 24 +++-- src/flet_webview/webview.py | 108 ++++++++++++--------- src/flutter/flet_webview/pubspec.yaml | 2 +- 11 files changed, 286 insertions(+), 88 deletions(-) diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 99ab865..51af3a7 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -4,6 +4,7 @@ on: push: branches: - main + - dev paths: - 'LICENSE' - 'CHANGELOG.md' diff --git a/.gitignore b/.gitignore index 006e58f..891876a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,135 @@ -*.egg-info/ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python build/ develop-eggs/ dist/ -.DS_store -.venv/ \ No newline at end of file +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock +uv.lock + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# VS Code +.vscode/ + +# PDM +.pdm.toml +__pypackages__/ + +.DS_Store diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e69f06f..0997bef 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,9 +1,27 @@ repos: - - repo: https://github.com/pycqa/isort - rev: 5.13.2 + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.12.5 hooks: - - id: isort - - repo: https://github.com/ambv/black - rev: 22.12.0 + # Run the linter. + - id: ruff + args: [ --fix ] + # Run the formatter. + - id: ruff-format + + - repo: https://github.com/executablebooks/mdformat + rev: 0.7.22 hooks: - - id: black + - id: mdformat + additional_dependencies: + - mdformat-ruff + + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v5.0.0 + hooks: + - id: end-of-file-fixer + - id: trailing-whitespace + + - repo: https://github.com/aio-libs/sort-all + rev: v1.3.0 + hooks: + - id: sort-all diff --git a/docs/types/log_level_severity.md b/docs/types/log_level_severity.md index d4e0308..4277a51 100644 --- a/docs/types/log_level_severity.md +++ b/docs/types/log_level_severity.md @@ -1 +1,3 @@ -::: flet_webview.types.LogLevelSeverity \ No newline at end of file +::: flet_webview.types.LogLevelSeverity +options: +separate_signature: false diff --git a/docs/types/request_method.md b/docs/types/request_method.md index d2454a6..0d6003a 100644 --- a/docs/types/request_method.md +++ b/docs/types/request_method.md @@ -1 +1,3 @@ -::: flet_webview.types.RequestMethod \ No newline at end of file +::: flet_webview.types.RequestMethod +options: +separate_signature: false diff --git a/mkdocs.yml b/mkdocs.yml index 8d3e62d..b3b965e 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -104,6 +104,7 @@ plugins: # - footnotes - search: lang: en + - open-in-new-tab - mike: alias_type: symlink - glightbox @@ -128,21 +129,16 @@ plugins: show_labels: false show_if_no_docstring: true docstring_section_style: spacy + separate_signature: true inherited_members: true preload_modules: [ flet ] filters: - "!^_" # Exclude private members starting with only one underscore - - "!before_update" - - "!before_event" - - "!clean" - - "!did_mount" - - "!init" - - "!is_isolated" - - "!update" - - "!will_unmount" extensions: - griffe_modernized_annotations + - griffe_warnings_deprecated inventories: + - url: https://docs.flet.dev/objects.inv - url: https://docs.python.org/3/objects.inv domains: [ py, std ] - url: https://typing-extensions.readthedocs.io/en/latest/objects.inv diff --git a/pyproject.toml b/pyproject.toml index cba2e7c..059f323 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,22 +20,40 @@ Issues = "https://github.com/flet-dev/flet-webview/issues" "flutter.flet_webview" = ["**/*"] [dependency-groups] +test = [ + "pytest >=7.2.0", +] dev = [ - "pre-commit>=4.2.0", - "ruff>=0.11.7", + "pre-commit >=4.2.0", + "ruff >=0.11.7", + { include-group = 'test' }, +] +docs-coverage = [ + "docstr-coverage >=2.3.2", ] docs = [ - "mkdocs", - "mkdocs-material", - "mkdocstrings[python]", - "mkdocstrings-python-xref", - "mike", - "markdown>=3.6", - "pymdown-extensions", - "mkdocs-glightbox", - "mkdocs-section-index", - "griffe-modernized-annotations", - "pygments>=2.16", + "mkdocs >=1.6.1", + "mkdocs-material >=9.6.15", + "mkdocstrings-python >=1.16.12", + "mkdocstrings-python-xref >=1.16.3", + "mike >=2.1.3", + "markdown >=3.6", + "pymdown-extensions >=10.16", + "mkdocs-exclude >=1.0.2", + "mkdocs-glightbox >=0.4.0", + "mkdocs-open-in-new-tab >=1.0.8", + "mkdocs-section-index >=0.3.10", + "griffe-modernized-annotations >=1.0.8", + "griffe-warnings-deprecated >=1.1.0", + "pygments >=2.16", + "markdown-exec[ansi] >=1.11.0", + "pydocstyle >=6.3.0", + "linkcheckmd >=1.4.0", + { include-group = 'docs-coverage' }, +] +all = [ + { include-group = 'dev' }, + { include-group = 'docs' }, ] [build-system] @@ -64,6 +82,7 @@ select = [ "I" ] preview = true +pydocstyle = { convention = 'google' } [tool.ruff.format] quote-style = "double" diff --git a/src/flet_webview/__init__.py b/src/flet_webview/__init__.py index 2b5da77..fef36af 100644 --- a/src/flet_webview/__init__.py +++ b/src/flet_webview/__init__.py @@ -6,3 +6,12 @@ WebViewScrollEvent, ) from .webview import WebView + +__all__ = [ + "LogLevelSeverity", + "RequestMethod", + "WebView", + "WebViewConsoleMessageEvent", + "WebViewJavaScriptEvent", + "WebViewScrollEvent", +] diff --git a/src/flet_webview/types.py b/src/flet_webview/types.py index cf48f98..f041a4d 100644 --- a/src/flet_webview/types.py +++ b/src/flet_webview/types.py @@ -1,14 +1,18 @@ from dataclasses import dataclass from enum import Enum +from typing import TYPE_CHECKING import flet as ft +if TYPE_CHECKING: + from .webview import WebView # noqa + __all__ = [ - "RequestMethod", "LogLevelSeverity", - "WebViewScrollEvent", + "RequestMethod", "WebViewConsoleMessageEvent", "WebViewJavaScriptEvent", + "WebViewScrollEvent", ] @@ -45,16 +49,22 @@ class LogLevelSeverity(Enum): @dataclass -class WebViewScrollEvent(ft.Event[ft.EventControlType]): +class WebViewScrollEvent(ft.Event["WebView"]): x: float - """The value of the horizontal offset with the origin being at the leftmost of the `WebView`.""" + """ + The value of the horizontal offset with the origin being at the + leftmost of the `WebView`. + """ y: float - """The value of the vertical offset with the origin being at the topmost of the `WebView`.""" + """ + The value of the vertical offset with the origin being at the + topmost of the `WebView`. + """ @dataclass -class WebViewConsoleMessageEvent(ft.Event[ft.EventControlType]): +class WebViewConsoleMessageEvent(ft.Event["WebView"]): message: str """The message written to the console.""" @@ -63,7 +73,7 @@ class WebViewConsoleMessageEvent(ft.Event[ft.EventControlType]): @dataclass -class WebViewJavaScriptEvent(ft.Event[ft.EventControlType]): +class WebViewJavaScriptEvent(ft.Event["WebView"]): message: str """The message to be displayed in the window.""" diff --git a/src/flet_webview/webview.py b/src/flet_webview/webview.py index 60e80cb..b6703f6 100644 --- a/src/flet_webview/webview.py +++ b/src/flet_webview/webview.py @@ -1,5 +1,5 @@ import asyncio -from typing import List, Optional +from typing import Optional import flet as ft @@ -27,103 +27,101 @@ class WebView(ft.ConstrainedControl): enable_javascript: Optional[bool] = None """ - Enable or disable the JavaScript execution on the page. - - Note that disabling the JavaScript execution on the page may result to unexpected web page behaviour. + Enable or disable the JavaScript execution on the page. + + Note that disabling the JavaScript execution on the page may result to + unexpected web page behaviour. """ - prevent_links: Optional[List[str]] = None + prevent_links: Optional[list[str]] = None """List of url-prefixes that should not be followed/loaded/downloaded.""" bgcolor: Optional[ft.ColorValue] = None """Defines the background color of the WebView.""" - on_page_started: ft.OptionalControlEventHandler["WebView"] = None + on_page_started: Optional[ft.ControlEventHandler["WebView"]] = None """ Fires soon as the first loading process of the webview page is started. - - Event handler argument's `data` property is of type `str` and contains the URL. - + + Event handler argument's [`data`][flet.Event.data] property is of type + `str` and contains the URL. + Note: Works only on the following platforms: iOS, Android and macOS. """ - on_page_ended: ft.OptionalControlEventHandler["WebView"] = None + on_page_ended: Optional[ft.ControlEventHandler["WebView"]] = None """ Fires when all the webview page loading processes are ended. - - Event handler argument's `data` property is of type `str` and contains the URL. - + + Event handler argument's [`data`][flet.Event.data] property is of type `str` + and contains the URL. + Note: Works only on the following platforms: iOS, Android and macOS. """ - on_web_resource_error: ft.OptionalControlEventHandler["WebView"] = None + on_web_resource_error: Optional[ft.ControlEventHandler["WebView"]] = None """ Fires when there is error with loading a webview page resource. - - Event handler argument's `data` property is of type `str` and contains the error message. - + + Event handler argument's [`data`][flet.Event.data] property is of type + `str` and contains the error message. + Note: Works only on the following platforms: iOS, Android and macOS. """ - on_progress: ft.OptionalControlEventHandler["WebView"] = None + on_progress: Optional[ft.ControlEventHandler["WebView"]] = None """ Fires when the progress of the webview page loading is changed. - - Event handler argument's `data` property is of type `int` and contains the progress value. - + + Event handler argument's [`data`][flet.Event.data] property is of type + `int` and contains the progress value. + Note: Works only on the following platforms: iOS, Android and macOS. """ - on_url_change: ft.OptionalControlEventHandler["WebView"] = None + on_url_change: Optional[ft.ControlEventHandler["WebView"]] = None """ Fires when the URL of the webview page is changed. - - Event handler argument's `data` property is of type `str` and contains the new URL. - + + Event handler argument's [`data`][flet.Event.data] property is of type + `str` and contains the new URL. + Note: Works only on the following platforms: iOS, Android and macOS. """ - on_scroll: ft.OptionalEventHandler[WebViewScrollEvent["WebView"]] = None + on_scroll: Optional[ft.EventHandler[WebViewScrollEvent]] = None """ Fires when the web page's scroll position changes. - - Event handler argument is of type `WebviewScrollEvent`. - + Note: Works only on the following platforms: iOS, Android and macOS. """ - on_console_message: ft.OptionalEventHandler[ - WebViewConsoleMessageEvent["WebView"] - ] = None + on_console_message: Optional[ft.EventHandler[WebViewConsoleMessageEvent]] = None """ Fires when a log message is written to the JavaScript console. - - Event handler argument is of type `WebviewConsoleMessageEvent`. - + Note: Works only on the following platforms: iOS, Android and macOS. """ - on_javascript_alert_dialog: ft.OptionalEventHandler[ - WebViewJavaScriptEvent["WebView"] - ] = None + on_javascript_alert_dialog: Optional[ft.EventHandler[WebViewJavaScriptEvent]] = None """ Fires when the web page attempts to display a JavaScript alert() dialog. - - Event handler argument is of type `WebviewJavaScriptEvent`. - + Note: Works only on the following platforms: iOS, Android and macOS. """ def _check_mobile_or_mac_platform(self): - """Checks/Validates support for the current platform (iOS, Android, or macOS).""" + """ + Checks/Validates support for the current platform (iOS, Android, or macOS). + """ assert self.page is not None, "WebView must be added to page first." if self.page.web or self.page.platform not in [ ft.PagePlatform.ANDROID, @@ -312,10 +310,12 @@ async def clear_local_storage_async(self): async def get_current_url_async(self) -> Optional[str]: """ - Returns the current URL that the WebView is displaying or `None` if no URL was ever loaded. + Returns the current URL that the WebView is displaying or `None` + if no URL was ever loaded. Returns: - The current URL that the WebView is displaying or `None` if no URL was ever loaded. + The current URL that the WebView is displaying or `None` + if no URL was ever loaded. Note: Works only on the following platforms: iOS, Android and macOS. @@ -373,7 +373,10 @@ async def load_file_async(self, path: str): Works only on the following platforms: iOS, Android and macOS. """ self._check_mobile_or_mac_platform() - await self._invoke_method_async("load_file", arguments={"path": path}) + await self._invoke_method_async( + method_name="load_file", + arguments={"path": path}, + ) def load_request(self, url: str, method: RequestMethod = RequestMethod.GET): """ @@ -428,7 +431,10 @@ async def run_javascript_async(self, value: str): Works only on the following platforms: iOS, Android and macOS. """ self._check_mobile_or_mac_platform() - await self._invoke_method_async("run_javascript", arguments={"value": value}) + await self._invoke_method_async( + method_name="run_javascript", + arguments={"value": value}, + ) def load_html(self, value: str, base_url: Optional[str] = None): """ @@ -486,7 +492,10 @@ async def scroll_to_async(self, x: int, y: int): Works only on the following platforms: iOS, Android and macOS. """ self._check_mobile_or_mac_platform() - await self._invoke_method_async("scroll_to", arguments={"x": x, "y": y}) + await self._invoke_method_async( + method_name="scroll_to", + arguments={"x": x, "y": y}, + ) def scroll_by(self, x: int, y: int): """ @@ -514,4 +523,7 @@ async def scroll_by_async(self, x: int, y: int): Works only on the following platforms: iOS, Android and macOS. """ self._check_mobile_or_mac_platform() - await self._invoke_method_async("scroll_by", arguments={"x": x, "y": y}) + await self._invoke_method_async( + method_name="scroll_by", + arguments={"x": x, "y": y}, + ) diff --git a/src/flutter/flet_webview/pubspec.yaml b/src/flutter/flet_webview/pubspec.yaml index 8d947b0..c8f8959 100644 --- a/src/flutter/flet_webview/pubspec.yaml +++ b/src/flutter/flet_webview/pubspec.yaml @@ -30,4 +30,4 @@ dependencies: dev_dependencies: flutter_test: sdk: flutter - flutter_lints: ^2.0.0 \ No newline at end of file + flutter_lints: ^3.0.0 From c6d3269d63ef8091539bbb0e1f27d56676dbef0c Mon Sep 17 00:00:00 2001 From: ndonkoHenri Date: Sun, 27 Jul 2025 17:33:56 +0200 Subject: [PATCH 2/6] remove mdformat from pre-commit --- .pre-commit-config.yaml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0997bef..8f2d1ab 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,13 +8,6 @@ repos: # Run the formatter. - id: ruff-format - - repo: https://github.com/executablebooks/mdformat - rev: 0.7.22 - hooks: - - id: mdformat - additional_dependencies: - - mdformat-ruff - - repo: https://github.com/pre-commit/pre-commit-hooks rev: v5.0.0 hooks: From d0c2e437e431469e3b3b5d5017bd3a1da1ffad80 Mon Sep 17 00:00:00 2001 From: ndonkoHenri Date: Sun, 27 Jul 2025 20:18:51 +0200 Subject: [PATCH 3/6] update mkdocs watch items | unshow `Event.get_event_field_type` --- mkdocs.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mkdocs.yml b/mkdocs.yml index b3b965e..8d1c379 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -95,9 +95,13 @@ validation: unrecognized_links: warn anchors: warn +# Watch watch: - docs - - src + - src/flet_webview + - CHANGELOG.md + - LICENSE + - README.md # Plugins plugins: @@ -134,6 +138,7 @@ plugins: preload_modules: [ flet ] filters: - "!^_" # Exclude private members starting with only one underscore + - "!get_event_field_type" extensions: - griffe_modernized_annotations - griffe_warnings_deprecated From bdbf247cabade3fe827151a4d581ceb25d7f3ed6 Mon Sep 17 00:00:00 2001 From: ndonkoHenri Date: Sun, 27 Jul 2025 20:23:31 +0200 Subject: [PATCH 4/6] fix separate_signature in Enums --- docs/types/log_level_severity.md | 4 ++-- docs/types/request_method.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/types/log_level_severity.md b/docs/types/log_level_severity.md index 4277a51..e47bd02 100644 --- a/docs/types/log_level_severity.md +++ b/docs/types/log_level_severity.md @@ -1,3 +1,3 @@ ::: flet_webview.types.LogLevelSeverity -options: -separate_signature: false + options: + separate_signature: false diff --git a/docs/types/request_method.md b/docs/types/request_method.md index 0d6003a..d5c2475 100644 --- a/docs/types/request_method.md +++ b/docs/types/request_method.md @@ -1,3 +1,3 @@ ::: flet_webview.types.RequestMethod -options: -separate_signature: false + options: + separate_signature: false From 344915f44281cdefcffd990806932438956f1761 Mon Sep 17 00:00:00 2001 From: ndonkoHenri Date: Sun, 27 Jul 2025 21:27:22 +0200 Subject: [PATCH 5/6] fixes --- .github/workflows/docs.yml | 2 +- src/flet_webview/webview.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 51af3a7..2364062 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -53,7 +53,7 @@ jobs: - name: Install dependencies run: | uv pip install -e . - uv pip install --group docs + uv pip install --group all - name: Deploy to GitHub Pages run: uv run mkdocs gh-deploy --force diff --git a/src/flet_webview/webview.py b/src/flet_webview/webview.py index b6703f6..96b5992 100644 --- a/src/flet_webview/webview.py +++ b/src/flet_webview/webview.py @@ -315,7 +315,7 @@ async def get_current_url_async(self) -> Optional[str]: Returns: The current URL that the WebView is displaying or `None` - if no URL was ever loaded. + if no URL was ever loaded. Note: Works only on the following platforms: iOS, Android and macOS. From 82e288e39e63bd163d319c7e2466454b26aaf198 Mon Sep 17 00:00:00 2001 From: ndonkoHenri Date: Sun, 27 Jul 2025 21:51:38 +0200 Subject: [PATCH 6/6] lint dependency group --- .github/workflows/docs.yml | 2 +- pyproject.toml | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 2364062..9a96d99 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -53,7 +53,7 @@ jobs: - name: Install dependencies run: | uv pip install -e . - uv pip install --group all + uv pip install --group docs --group lint - name: Deploy to GitHub Pages run: uv run mkdocs gh-deploy --force diff --git a/pyproject.toml b/pyproject.toml index 059f323..37934f9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,9 +23,12 @@ Issues = "https://github.com/flet-dev/flet-webview/issues" test = [ "pytest >=7.2.0", ] +lint = [ + "ruff >=0.11.7", +] dev = [ "pre-commit >=4.2.0", - "ruff >=0.11.7", + { include-group = 'lint' }, { include-group = 'test' }, ] docs-coverage = [