Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/python return enhancement #50

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/libraries/Dart_SDK.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 26 additions & 10 deletions android/src/main/kotlin/com/chaquopy/chaquopy/ChaquopyPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,44 @@ class ChaquopyPlugin : FlutterPlugin, MethodCallHandler {
val _sys: PyObject = _python.getModule("sys")
val _io: PyObject = _python.getModule("io")

return try {
try {
val _textOutputStream: PyObject = _io.callAttr("StringIO")
_sys["stdout"] = _textOutputStream
_console.callAttrThrows("mainTextCode", code)
_returnOutput["textOutputOrError"] = _textOutputStream.callAttr("getvalue").toString()
_returnOutput
val returnValue: PyObject = _console.callAttrThrows("mainTextCode", code)
_returnOutput["output"] = _textOutputStream.callAttr("getvalue").toString()
_returnOutput["returnValueJson"] = returnValue.toString()
} catch (e: PyException) {
_returnOutput["textOutputOrError"] = e.message.toString()
_returnOutput
val (errorType, errorMessage) = e.message?.split(":", limit = 2) ?: listOf("UnknownError", e.message ?: "Unknown Message")
_returnOutput["errorType"] = errorType.trim()
_returnOutput["errorMessage"] = errorMessage?.trim()
_returnOutput["traceback"] = e.stackTrace.joinToString("\n")
}
return _returnOutput
}

override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (call.method == "runPythonScript") {
try {
val code: String = call.arguments() ?: ""
val _result: Map<String, Any?> = _runPythonTextCode(code)
result.success(_result)
if (_result.containsKey("errorType")) {
val errorDetails: MutableMap<String, Any?> = HashMap()
errorDetails["errorType"] = _result["errorType"]
errorDetails["errorMessage"] = _result["errorMessage"]
errorDetails["traceback"] = _result["traceback"]
result.success(errorDetails)
} else {
val successDetails: MutableMap<String, Any?> = HashMap()
successDetails["output"] = _result["output"]
successDetails["returnValueJson"] = _result["returnValueJson"]
result.success(successDetails)
}
} catch (e: Exception) {
val _result: MutableMap<String, Any?> = HashMap()
_result["textOutputOrError"] = e.message.toString()
result.success(_result)
val errorDetails: MutableMap<String, Any?> = HashMap()
errorDetails["errorType"] = e::class.java.simpleName
errorDetails["errorMessage"] = e.message.toString()
errorDetails["traceback"] = e.stackTrace.joinToString("\n")
result.success(errorDetails)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
154 changes: 79 additions & 75 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,90 +1,77 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
archive:
dependency: transitive
description:
name: archive
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.6"
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c"
url: "https://pub.dev"
source: hosted
version: "2.8.2"
version: "2.11.0"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
sha256: "6cfb5af12253eaf2b368f07bacc5a80d1301a071c73360d746b7f2e32d762c66"
url: "https://pub.dev"
source: hosted
version: "2.1.0"
version: "2.1.1"
chaquopy:
dependency: "direct main"
description:
path: ".."
relative: true
source: path
version: "0.0.17"
version: "0.0.18"
characters:
dependency: transitive
description:
name: characters
url: "https://pub.dartlang.org"
sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605"
url: "https://pub.dev"
source: hosted
version: "1.2.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
version: "1.3.0"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.dartlang.org"
sha256: cb6d7f03e1de671e34607e909a7213e31d7752be4fb66a86d29fe1eb14bfb5cf
url: "https://pub.dev"
source: hosted
version: "1.1.0"
version: "1.1.1"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
crypto:
dependency: transitive
description:
name: crypto
url: "https://pub.dartlang.org"
sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c"
url: "https://pub.dev"
source: hosted
version: "3.0.1"
version: "1.17.1"
cupertino_icons:
dependency: "direct main"
description:
name: cupertino_icons
url: "https://pub.dartlang.org"
sha256: caac504f942f41dfadcf45229ce8c47065b93919a12739f20d6173a883c5ec73
url: "https://pub.dev"
source: hosted
version: "1.0.2"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.dartlang.org"
sha256: "511392330127add0b769b75a987850d136345d9227c6b94c96a04cf4a391bf78"
url: "https://pub.dev"
source: hosted
version: "1.2.0"
version: "1.3.1"
file:
dependency: transitive
description:
name: file
url: "https://pub.dartlang.org"
sha256: "1b92bec4fc2a72f59a8e15af5f52cd441e4a7860b49499d69dfa817af20e925d"
url: "https://pub.dev"
source: hosted
version: "6.1.2"
version: "6.1.4"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -110,46 +97,60 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
js:
dependency: transitive
description:
name: js
sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3
url: "https://pub.dev"
source: hosted
version: "0.6.7"
matcher:
dependency: transitive
description:
name: matcher
url: "https://pub.dartlang.org"
sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb"
url: "https://pub.dev"
source: hosted
version: "0.12.11"
version: "0.12.15"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724
url: "https://pub.dev"
source: hosted
version: "0.1.3"
version: "0.2.0"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3"
url: "https://pub.dev"
source: hosted
version: "1.7.0"
version: "1.9.1"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917"
url: "https://pub.dev"
source: hosted
version: "1.8.0"
version: "1.8.3"
platform:
dependency: transitive
description:
name: platform
url: "https://pub.dartlang.org"
sha256: "4a451831508d7d6ca779f7ac6e212b4023dd5a7d08a27a63da33756410e32b76"
url: "https://pub.dev"
source: hosted
version: "3.1.0"
process:
dependency: transitive
description:
name: process
url: "https://pub.dartlang.org"
sha256: "53fd8db9cec1d37b0574e12f07520d582019cb6c44abf5479a01505099a34a09"
url: "https://pub.dev"
source: hosted
version: "4.2.4"
sky_engine:
Expand All @@ -161,79 +162,82 @@ packages:
dependency: transitive
description:
name: source_span
url: "https://pub.dartlang.org"
sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250
url: "https://pub.dev"
source: hosted
version: "1.8.1"
version: "1.9.1"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.dartlang.org"
sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5
url: "https://pub.dev"
source: hosted
version: "1.10.0"
version: "1.11.0"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.dartlang.org"
sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8"
url: "https://pub.dev"
source: hosted
version: "2.1.0"
version: "2.1.1"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.dartlang.org"
sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde"
url: "https://pub.dev"
source: hosted
version: "1.1.0"
version: "1.2.0"
sync_http:
dependency: transitive
description:
name: sync_http
url: "https://pub.dartlang.org"
sha256: "7f0cd72eca000d2e026bcd6f990b81d0ca06022ef4e32fb257b30d3d1014a961"
url: "https://pub.dev"
source: hosted
version: "0.3.0"
version: "0.3.1"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
sha256: a29248a84fbb7c79282b40b8c72a1209db169a2e0542bce341da992fe1bc7e84
url: "https://pub.dev"
source: hosted
version: "1.2.0"
version: "1.2.1"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.8"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
sha256: eb6ac1540b26de412b3403a163d919ba86f6a973fe6cc50ae3541b80092fdcfb
url: "https://pub.dev"
source: hosted
version: "1.3.0"
version: "0.5.1"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803"
url: "https://pub.dev"
source: hosted
version: "2.1.1"
version: "2.1.4"
vm_service:
dependency: transitive
description:
name: vm_service
url: "https://pub.dartlang.org"
sha256: f6deed8ed625c52864792459709183da231ebf66ff0cf09e69b573227c377efe
url: "https://pub.dev"
source: hosted
version: "7.5.0"
version: "11.3.0"
webdriver:
dependency: transitive
description:
name: webdriver
url: "https://pub.dartlang.org"
sha256: "3c923e918918feeb90c4c9fdf1fe39220fa4c0e8e2c0fffaded174498ef86c49"
url: "https://pub.dev"
source: hosted
version: "3.0.0"
version: "3.0.2"
sdks:
dart: ">=2.14.0 <3.0.0"
dart: ">=3.0.0-0 <4.0.0"
flutter: ">=1.20.0"
9 changes: 8 additions & 1 deletion lib/chaquopy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ class Chaquopy {

/// This function execute your python code and returns result Map.
/// Structure of result map is :
/// result['textOutput'] : The original output / error
/// If the execution is successful:
/// - result['output']: Contains the standard output from the Python code.
/// - result['returnValueJson']: Contains a JSON string representing the return values from the Python code.
///
/// If there's an error during execution:
/// - result['error']: Contains the error message.
/// - result['errorType']: Contains the type of the error.
/// - result['traceback']: Contains the traceback of the error.
static Future<Map<String, dynamic>> executeCode(String code) async {
dynamic outputData = await _channel.invokeMethod('runPythonScript', code);
return Map<String, dynamic>.from(outputData);
Expand Down
Loading