Skip to main content

Type-safe, bidirectional bridge between native Python (WebKitGTK) and JavaScript via the Aspectly protocol.

Project description

aspectly-bridge (Python / WebKitGTK)

PyPI

Type-safe, bidirectional bridge between native Python code and JavaScript running in a WebKitGTK web view (Linux desktop). Speaks the Aspectly protocol, so the same @aspectly/core web content runs unchanged across every host.

WebKitGTK uses the same JS mechanism as WKWebView (window.webkit.messageHandlers.aspectly), so the embedded web content auto-detects this host through the existing @aspectly/transports WebKit transport — no Flutter/Android-style extra transport needed.

Install

pip install aspectly-bridge

# For the WebKitGTK browser bridge (Linux):
#   system packages: gobject-introspection, gir1.2-webkit2-4.1 (or 4.0)
pip install "aspectly-bridge[webkitgtk]"

The core (BridgeHost) is pure Python with no dependencies. Only the WebKitGTKBrowserBridge needs PyGObject + WebKit2GTK (Linux).

Usage

from aspectly_bridge import BridgeHost
from aspectly_bridge.webkitgtk import WebKitGTKBrowserBridge

bridge = BridgeHost(WebKitGTKBrowserBridge(web_view))

# Register handlers JS can call (before initialize).
bridge.register_handler("ping", lambda params: "pong")
bridge.register_handler("add", lambda params: params["a"] + params["b"])

# Initialize (returns a Future; resolves when the handshake completes).
bridge.initialize().result()

# Call a JS method (returns a Future).
result = bridge.send("greet", {"name": "Python"}).result()
print(result["message"])

send and initialize return concurrent.futures.Future. In a GTK app, the result is delivered on the GLib main thread, so use future.add_done_callback or GLib.idle_add rather than blocking .result() on the UI thread.

API

BridgeHost(browser_bridge, logger=None, timeout_ms=100000)

register_handler(method, handler)     # handler(params) -> result
unregister_handler(method)
initialize(handlers=None) -> Future
send(method, params=None, timeout_ms=None) -> Future
process_message(message_json)

is_initialized        # bool
supported_methods     # list[str]  (JS-side methods)
registered_methods    # list[str]  (Python-side handlers)
on_initialized        # callable or None
dispose()

send / handlers surface BridgeException with error_type one of BridgeErrorType.{UNSUPPORTED_METHOD, METHOD_EXECUTION_TIMEOUT, REJECTED, BRIDGE_NOT_AVAILABLE}.

Testing

cd python
python -m pytest          # the pure-Python core (no GTK required)

See examples/webkitgtk for a runnable GTK app.

Other platforms

This is the Python / WebKitGTK host (PyPI aspectly-bridge, 2.1.0). The same Aspectly protocol ships for Web (@aspectly/web), React Native (@aspectly/react-native), React Native Web/Expo (@aspectly/react-native-web), .NET CefSharp/WebView2 (Aspectly.Bridge.CefSharp / Aspectly.Bridge.WebView2), iOS/macOS/visionOS (AspectlyBridge), Android (io.github.jeanisahakyan:aspectly-bridge), and Flutter (aspectly_bridge) — all at version 2.1.0. See the repository README for the full platform matrix.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aspectly_bridge-2.1.0.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aspectly_bridge-2.1.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file aspectly_bridge-2.1.0.tar.gz.

File metadata

  • Download URL: aspectly_bridge-2.1.0.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for aspectly_bridge-2.1.0.tar.gz
Algorithm Hash digest
SHA256 8d9c004b33d184db31cf586082cb9d8615f9fca7b7c9029468a52eaa72dcf537
MD5 a636cb9488060c271cf667f4f7017eed
BLAKE2b-256 e44fa6d68548991d32c0e348dc87dfb91128ea5e9edec43dc05be3202992cc46

See more details on using hashes here.

Provenance

The following attestation bundles were made for aspectly_bridge-2.1.0.tar.gz:

Publisher: release.yml on JeanIsahakyan/aspectly

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file aspectly_bridge-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: aspectly_bridge-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for aspectly_bridge-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c774bcf8c76132156b1b661e46e2187f764d10af7127161d847565b802d5697d
MD5 308656470aab2d52130df32a5879f276
BLAKE2b-256 bb7d9e7df20e9dc4a74a8edccdbf5bb5226f76ce07618faa6fe0f8dbf280e178

See more details on using hashes here.

Provenance

The following attestation bundles were made for aspectly_bridge-2.1.0-py3-none-any.whl:

Publisher: release.yml on JeanIsahakyan/aspectly

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page