chaquo / chaquopy

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

How to send a java snippet to python function as callback parameter #341

Closed mDadashi closed 4 years ago

mDadashi commented 4 years ago

I have a python class like this:

Client(username, password, [callback])

the callback is optional though. I have a kotlin function which I need to be called when client's job finished. Let's say clientConnectionCallback(info: String) I create an instance of the above class through Kotlin like this:

val api = module.callAttr("Client","username",, "password", "HOW DO I PUT clientConnectionCallback HERE?")

I see that dynamic proxy allows to call python code from java. I need to call Java code from python

Am I missing something here? Thanks in advance

mhsmith commented 4 years ago

The normal Kotlin syntax for this would be:

module.callAttr("Client", "username", "password", ::clientConnectionCallback)

If you use that syntax, your Client class will receive an object with an invoke method, which it can call like this:

def __init__(self, username, password, callback=None):
    calback.invoke("hello")

However, if you need callback to be directly callable, then you can do that with a simple adapter like this:

def make_callback(callback):
    return lambda *args: callback.invoke(*args)

Then your Kotlin code will look like this:

module.callAttr("Client", "username", "password", 
                module.callAttr("make_callback", ::clientConnectionCallback))

And your Client class like this:

def __init__(self, username, password, callback=None):
    callback("hello")
mDadashi commented 4 years ago

Thanks.

mhsmith commented 4 years ago

I understand you were annoyed that this wasn't answered on StackOverflow. But if you look at the question, you'll see they aren't asking the same thing as you. They don't even mention callbacks. They just asked "how do I call a Java function from Python", which could mean many different things. So there was nothing more I could do except give them a link to the documentation.

By contrast, you asked a specific question, so I was able to give you a specific answer.

I hope that makes sense. I think it's probably too late to undo your StackOverflow downvote, but perhaps you would at least consider removing your comment.

mDadashi commented 4 years ago

Hey there Thanks for your answer. I removed the comment on stackoverflow and if you could edit your answer any bit at all, I would be happy to remove the downvote too or otherwise, stackoverflow won't allow me to do so. I didn't mean to harm your reputation on SO but wanted to take your attention to the matter that we need your help when we ask questions. Thank you for your quick response and again, sorry for the harm done.

Best Regards Mason

mhsmith commented 4 years ago

OK, I've edited my answer. Thanks very much.

mDadashi commented 4 years ago

Done.

mhsmith commented 3 years ago

In Chaquopy 9.0.0 and later, Kotlin and Java method references and lambdas are now directly callable by Python code. So the Kotlin code can look like this:

module.callAttr("Client", "username", "password", ::clientConnectionCallback)

And the Python code like this:

class Client:
    def __init__(self, username, password, callback=None):
        callback("hello")