Skip to main content

A Python C extension for interacting with the Dart SDK from Python

Project description

dart_bridge

dart_bridge is a Python C extension that enables bi-directional communication between embedded Python and a Dart application (such as a Flutter app) via Dart's FFI (Foreign Function Interface) and ports.

It is designed for use cases where you want to embed Python inside a Dart app, send messages between Python and Dart, and run long-lived, message-driven Python scripts that cooperate with Dart logic.


🚀 Features

  • ✅ Send messages (bytes) from Python to Dart
  • ✅ Receive messages from Dart into Python via FFI
  • ✅ Cross-thread safe using PyGILState_Ensure
  • ✅ Fully compatible with Dart's dart_api_dl.h via FFI
  • ✅ Portable across macOS, Linux, Windows, Android, and iOS
  • ✅ Compatible with Python 3.12+ and the abi3 stable ABI

💡 Usage Scenarios

  • Embedding Python in Flutter for dynamic scripting, ML, or automation
  • Integrating Python data pipelines or AI models into mobile/desktop apps
  • Building hybrid apps where UI is Flutter but business logic is Python
  • Real-time event or message processing in an embedded Python runtime

🔄 Communication Overview

Dart → Python:
- Dart calls native function: `DartBridge_EnqueueMessage(...)`
- Python handler receives bytes via a registered Python function

Python → Dart:
- Python calls `dart_bridge.send_bytes(bytes)`
- Dart receives data via a `ReceivePort` attached to FFI `SendPort`

Python API

dart_bridge.send_bytes(data: bytes) -> bool

Sends bytes to Dart via a previously registered Dart port.
Returns True if successful.
Raises RuntimeError if the port is not set or Dart_PostCObject_DL fails.


dart_bridge.set_enqueue_handler_func(func: Callable[[bytes], None])

Registers a Python function that will be called when Dart sends a message using FFI.
The function should accept a single bytes argument.

This enables Dart → Python messaging.


Dart FFI Requirements

On the Dart side, the following native functions are available via FFI:

// Initialize Dart dynamic linking (must be called once)
intptr_t DartBridge_InitDartApiDL(void* data);

// Register a Dart SendPort for messages from Python
void DartBridge_SetSendPort(int64_t port);

// Send message from Dart into Python (calls the Python handler)
void DartBridge_EnqueueMessage(const char* data, size_t len);

These are typically loaded in Dart using DynamicLibrary.lookupFunction.


Installation

Install from PyPI:

pip install dart_bridge

Or build from source with:

pip install .

Make sure to include dart_api_dl.h from the Dart SDK in your compiler's include path.


Example Usage

Dart side

final dylib = DynamicLibrary.open('dart_bridge.so');

final initApi = dylib.lookupFunction<
  IntPtr Function(Pointer<Void>),
  int Function(Pointer<Void>)
>('DartBridge_InitDartApiDL');

final setPort = dylib.lookupFunction<
  Void Function(Int64),
  void Function(int)
>('DartBridge_SetSendPort');

final enqueue = dylib.lookupFunction<
  Void Function(Pointer<Char>, IntPtr),
  void Function(Pointer<Char>, int)
>('DartBridge_EnqueueMessage');

initApi(NativeApi.initializeApiDLData);
setPort(sendPort.nativePort);

final msg = 'hello from Dart';
final ptr = msg.toNativeUtf8().cast<Char>();
enqueue(ptr, msg.length);
calloc.free(ptr);

Python side

import asyncio
import dart_bridge

queue = asyncio.Queue()

def enqueue_from_dart(data: bytes):
    print("Received:", data)
    queue.put_nowait(data)

dart_bridge.set_enqueue_handler_func(enqueue_from_dart)

async def main():
    while True:
        msg = await queue.get()
        dart_bridge.send_bytes(b"ECHO: " + msg)

asyncio.run(main())

Thread Safety

  • DartBridge_EnqueueMessage is safe to call from Dart threads or isolates.
  • It uses PyGILState_Ensure() internally.
  • Your Python handler will run in a background thread. Use loop.call_soon_threadsafe() if needed.

License

MIT


Contributing

Pull requests are welcome! This project helps bridge Dart and Python through portable, async-safe FFI communication.

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

dart_bridge-0.4.2.tar.gz (50.1 kB view details)

Uploaded Source

Built Distributions

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

dart_bridge-0.4.2-cp312-cp312-win_amd64.whl (56.3 kB view details)

Uploaded CPython 3.12Windows x86-64

dart_bridge-0.4.2-cp312-cp312-musllinux_1_2_x86_64.whl (70.2 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

dart_bridge-0.4.2-cp312-cp312-musllinux_1_2_aarch64.whl (71.1 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ ARM64

dart_bridge-0.4.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (71.4 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

dart_bridge-0.4.2-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (70.9 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64manylinux: glibc 2.5+ x86-64

dart_bridge-0.4.2-cp312-cp312-macosx_11_0_arm64.whl (56.2 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

dart_bridge-0.4.2-cp312-cp312-macosx_10_13_x86_64.whl (55.6 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

File details

Details for the file dart_bridge-0.4.2.tar.gz.

File metadata

  • Download URL: dart_bridge-0.4.2.tar.gz
  • Upload date:
  • Size: 50.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for dart_bridge-0.4.2.tar.gz
Algorithm Hash digest
SHA256 79cd8c4b8b358d7f548cc9a7207d5da0f3996ef2c462065d671c83bcb2f9e9f3
MD5 db14a5c2da8894632954bd1f9e901b12
BLAKE2b-256 b6f1a162252ecf0d7fcb846be7b60528e3f4c1f2c5695d28b68788a717b143f4

See more details on using hashes here.

File details

Details for the file dart_bridge-0.4.2-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for dart_bridge-0.4.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 31be5f30260b9c022550e48ca443b07bcd42492d04eae6b859f52ee86e4f3d3e
MD5 090334014ee7cc8bfa75fe369404dce3
BLAKE2b-256 ab59ed0739a79d14d287c26e6762ab12a0cc3a4ab92cd65bb317322dc24d1297

See more details on using hashes here.

File details

Details for the file dart_bridge-0.4.2-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for dart_bridge-0.4.2-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 496e9f0a1677aad3bef53e01bf48ba9b930d472ce19e3f139daf4a2fb4df5431
MD5 7493c545d3e7429a415966bae9b780d4
BLAKE2b-256 c0e2b9630ea078948b2dbedddc4d123547acd6f00b73dce1646fc045ebbd9517

See more details on using hashes here.

File details

Details for the file dart_bridge-0.4.2-cp312-cp312-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for dart_bridge-0.4.2-cp312-cp312-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 bbd6a7741f51e9b6c56782ad361095f5af771526bc97da632155a320cb9255e5
MD5 fa9acc70b552c9913839ff1a2052cd2f
BLAKE2b-256 71af066fc068f6eff6be795a44654b026769c0a58c16ac32cd403f98900a191a

See more details on using hashes here.

File details

Details for the file dart_bridge-0.4.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for dart_bridge-0.4.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 16b57248d32c4577c809afe985dd1a626bdc498c428eafcc960029473430713a
MD5 1761b04b49a01e3d71ee2c771f311185
BLAKE2b-256 0cfa87b2e9101b9068d2809dfe876bb92fbe92219983a58f1559495d6bdabc35

See more details on using hashes here.

File details

Details for the file dart_bridge-0.4.2-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for dart_bridge-0.4.2-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5fa96b9924888746cb8934bb9526344c12ad065c94863c10dd44b133f9c3de43
MD5 2abe86ea67ea3933f58f83758f83a3cd
BLAKE2b-256 4617a4c09b21b99a591f10b1801ebe1cac034a094c2226361f8bcf508e8362cd

See more details on using hashes here.

File details

Details for the file dart_bridge-0.4.2-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dart_bridge-0.4.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0f40da822cb17f76bb48a7d4cf3cfdc2688fc85e4c13114ae98494ed4cefb5f1
MD5 77c85e44a28e0f12df08015e11256613
BLAKE2b-256 eadc004f931df5a63baa72960b9cfccb282d5af799b05994bfa406ae07cb55b9

See more details on using hashes here.

File details

Details for the file dart_bridge-0.4.2-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for dart_bridge-0.4.2-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 fa138269c5150351d0a50c814c55878b684f14a647ec0a32c72392ef5affca72
MD5 ccb112f27d7782a2f597dd4acd365e3e
BLAKE2b-256 030066523f2e06be865600e896298a4afcef8d7195f611751cf279421d320da0

See more details on using hashes here.

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