chaquo / chaquopy

Chaquopy: the Python SDK for Android
https://chaquo.com/chaquopy/
MIT License
748 stars 127 forks source link

exec and eval with local_var #1163

Closed anonyein closed 4 weeks ago

anonyein commented 1 month ago

Ready check

Chaquopy version

15.0.1

Relevant parts of your build.gradle file

Describe your issue

I want the Scripts more flexibility, and don't want to write python script before the apk built

  1. how to realize the function of exec / eval with local_var and can be used outside the exec sandbox (https://github.com/chaquo/chaquopy/issues/1119)
  2. how does chaquopy treat with the HashMap or Set by fromJava, I could not creat PyObject of just "{}" Could you help me?
mhsmith commented 4 weeks ago

how to realize the function of exec / eval with local_var and can be used outside the exec sandbox (https://github.com/chaquo/chaquopy/issues/1119)

You can modify the example in #1119 to keep a reference to the variable dictionary:

val vars = py.builtins.callAttr("dict")
py.builtins.callAttr("eval", "1+4", vars)

how does chaquopy treat with the HashMap or Set by fromJava, I could not creat PyObject of just "{}"

Chaquopy doesn't currently support accessing Java Maps or Sets with Python syntax (#1048), so they will appear as generic Java objects. You can access them by calling their Java methods directly.

I don't really understand your example, so please provide a complete code sample if you need more help.

anonyein commented 4 weeks ago

how to realize the function of exec / eval with local_var and can be used outside the exec sandbox (#1119)

You can modify the example in #1119 to keep a reference to the variable dictionary:

val vars = py.builtins.callAttr("dict")
py.builtins.callAttr("eval", "1+4", vars)

how does chaquopy treat with the HashMap or Set by fromJava, I could not creat PyObject of just "{}"

Chaquopy doesn't currently support accessing Java Maps or Sets with Python syntax (#1048), so they will appear as generic Java objects. You can access them by calling their Java methods directly.

I don't really understand your example, so please provide a complete code sample if you need more help.

I did it by using "exec" and "eval" with globals "{}" in one py file of directory "/python" as python class. Thanks for your help!!