dart_monty 0.3.4
dart_monty: ^0.3.4 copied to clipboard
Dart and Flutter bindings for Monty, a Rust-built embeddable sandbox that runs a restricted subset of Python. Native FFI and browser WASM.
dart_monty #
Live Demo — try it in your browser
Flutter plugin that exposes the Monty sandboxed Python interpreter to Dart and Flutter apps.
Run Python code from Dart — on desktop, mobile, and web — with resource limits, iterative execution, and snapshot/restore support.
Platform Support #
| Platform | Status |
|---|---|
| macOS | Supported |
| Linux | Supported |
| Web (browser) | Supported |
| Windows | Planned |
| iOS | Planned |
| Android | Planned |
Installation #
flutter pub add dart_monty
Usage #
import 'package:dart_monty/dart_monty.dart';
// Simple execution
final monty = MontyPlatform.instance;
final result = await monty.run('2 + 2');
print(result.value); // 4
// With resource limits
final limited = await monty.run(
'fib(30)',
limits: MontyLimits(timeoutMs: 5000, memoryBytes: 10 * 1024 * 1024),
);
// Iterative execution (external functions)
var progress = await monty.start(
'fetch("https://example.com")',
externalFunctions: ['fetch'],
);
if (progress is MontyPending) {
print('Python called: ${progress.functionName}');
progress = await monty.resume(myResult);
}
final complete = progress as MontyComplete;
print(complete.result.value);
// Error injection
progress = await monty.resumeWithError('network timeout');
// Cleanup
await monty.dispose();
Architecture #
Federated plugin with six packages:
| Package | Type | Description |
|---|---|---|
dart_monty |
Flutter plugin | App-facing API |
dart_monty_platform_interface |
Pure Dart | Abstract contract — no Flutter dependency |
dart_monty_ffi |
Pure Dart | Native FFI bindings (dart:ffi -> Rust) |
dart_monty_wasm |
Pure Dart | WASM bindings (dart:js_interop -> Web Worker) |
dart_monty_desktop |
Flutter plugin | Desktop platform (macOS/Linux, Isolate) |
dart_monty_web |
Flutter plugin | Web platform (browser, script injection) |
The three pure-Dart packages can be used without Flutter (e.g. in CLI tools or server-side Dart).
Native Path (desktop) #
Dart app -> MontyDesktop (Isolate)
-> MontyFfi (dart:ffi)
-> libdart_monty_native.{dylib,so}
-> Monty Rust interpreter
Web Path (browser) #
Dart app (compiled to JS) -> DartMontyWeb
-> MontyWasm (dart:js_interop)
-> Web Worker -> @pydantic/monty WASM
The Web Worker architecture bypasses Chrome's 8 MB synchronous WASM compilation limit.
Web Setup #
The web backend requires COOP/COEP HTTP headers for SharedArrayBuffer support:
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
Contributing #
See CONTRIBUTING.md for development setup, gate scripts, and CI details.
See PLAN.md for engineering milestones and quality gates.
License #
MIT License. See LICENSE.