Android_dev
Android_dev

Reputation: 101

Call java function from python - Chaquopy

I went through Chaquopy docs and found; with Chaquopy, java and python code can be called as user wishes.

I went through example apps and found examples for either calling python from java or running Android code through python and not the other way like calling java function from python.

Is there some example available or some reference for calling java function from python?

It would be better to add such example on Github examples if not already available.

Referred Chaquopy sample app on Github

Upvotes: 1

Views: 780

Answers (2)

Brian Langay
Brian Langay

Reputation: 1

Incase any one has any issues with chequepy

with chequopy usage to your project what you can mostly do by default is pass is data from java to python

Although as for your question

"calling java function from python"

what you can with this is that pass in data from java to python which is something i have done lots of time so intead of trying to find way to run the function there why dont you pass in data that this function has to do to => to python then you can manipulate this data and send python result back to your java project

i have some examples on this like translate, photo editing getting data from android using edit test or any of your choice then send this to an awaiting python fuction that takes some data and return response where by when you run chequopy function to call this python script and fuction

its result will do the whole asnc for you and you can get creative even more if you like

here is same examples done ready to see what you can do with this collaboration

translator translator app

https://github.com/brianlangay4/Translator?tab=readme-ov-file

and here is photo editing app example https://github.com/brianlangay4/Photo-Editor-Android

here is to save your time hope you know how to intergrate it to your project

so you will call the fuction

# from python file  
def translate_and_print(phrase, language):
try:
    translated_phrase = GoogleTranslator(target=language).translate(phrase)
    return translated_phrase  # Return instead of print
except Exception as e:
    return str(e)  # Return error message if any

so then in java were going to use this function like this

translateButton.setOnClickListener(v -> {
        //start result scenes
        String textToTranslate = editText.getText().toString();
        text.setText(textToTranslate);
        result_view.setVisibility(View.VISIBLE);
        PyObject translate = Python.getInstance().getModule("transp1")
                .callAttr("translate_and_print", textToTranslate, "zh-TW");
        lastTranslatedText = translate.toString(); // Store the translated text
        resultTextView.setText(lastTranslatedText); // Display the translated text
    });

with that you have fetch data from device send to python applied some python function to it get back and render to device

Upvotes: 0

mhsmith
mhsmith

Reputation: 8121

The API reference is here, and there is a large example here.

If you have a specific problem, please ask another question and explain exactly what you're trying to do.

Upvotes: 2

Related Questions