python-javabridge icon indicating copy to clipboard operation
python-javabridge copied to clipboard

What all things are possible with this javabridge.

Open Mahesha999 opened this issue 7 years ago • 3 comments

I want to call python from java. I have some requirements:

  • call should be transparent, in the sense that it should not require modifying .py file simply to enable it to be called from java
  • I want to be able to send arguments of both primitive types (int, String, floats etc.) or non primitive types (HashMap,ArrayList) from java to python function and receive back the returned object (which may of primitive types or non-primitive types) from python to java.
  • I preferably want to stick to CPython instead of Jython

I earlier tried with Jython which allows very clean call of python function from java without requiring to change python script. I looked something like this

py1.py

def square2(list):
    squares = []
        for i in list:
            squares.append(i*i)
    return squares

JythonTest.groovy

PythonInterpreter pi = new PythonInterpreter();
pi.exec("from py1 import square2");
pi.set("integer", new PyList([1,2,3,4]));   //ArrayList to be passed to python function
pi.exec("result = square2(integer)");
PyList result = (PyList)pi.get("result");
println result    //[1, 4, 9, 16]
println result[2] //9

Is similar thing possible with javabridge? Also is there any detailed tutorial about this? What all I found is this.

Mahesha999 avatar Sep 09 '16 09:09 Mahesha999

Hi Mahesha, thanks for considering Javabridge. Java objects are wrapped very nicely using Javabridge, but the same is not true for Python. It would be a nice addition and certainly possible to add code to the Java wrapper of a Python object to call its methods and access its attributes, but I doubt I'll have time to do so, but contributions are welcome.

The tutorial you found is probably the best way to access Python from Java - it's clumsy but there is a way to do just about anything you want to do in Python from Java using it.

LeeKamentsky avatar Sep 09 '16 12:09 LeeKamentsky

Thanks for the reply... May be a bit not on-topic question: Then what next best options can you suggest for my requirements: (1) call python functions from .py file from java code and send arguments and receive return values from it transparently without requiring to modify given .py file. (2) use pandas and numpy and other standard/widely used libraries

Mahesha999 avatar Sep 14 '16 12:09 Mahesha999

If your computer's Python distribution has pandas and numpy installed, you should be able to go ahead and use them from Java. If you're using a virtual environment, things are more difficult, but possible - I'd rather not go into that unless you need help, but the basic idea is to get things on your sys.path.

For your own python function, the best solution is to make a distutils package and install it to your local Python - I'm guessing you just want to load the functions from a single .py file, though. To do that, put the .py file in a directory, e.g. "/home/mahesha/sources" and have the following in the Python script that you run:

import sys
script_path = "/home/mahesha/sources"
if script_path not in sys.path:
    sys.path.append(script_path)

Then later on you can do:

from my_file import my_function
my_function()

Hope this is enough to get things working.

LeeKamentsky avatar Sep 14 '16 14:09 LeeKamentsky