Calling (C)Python code from Java: Is it JPype?
foo at bar.com
Thu Sep 30 02:41:18 CEST 2004
F. GEIGER wrote:
> I've dev'ed a Python prototype of an app, that besides the internals making
> it up has a gui. While test-driven dev'ing the app's internals in Python is
> fun as usual, dev'ing the GUI is not so funny, at least for me.
> I guess dev'ing a GUI in a test-driven way is not possible, or is it? I'm
> using wxPython, so if anyone has an idea...
> For now most of the time I extend and change the gui things, then run it, do
> the clicks to go thru the new things and - bang: Missing attribute bla bla
> bla or an error like that. Yes, I use Pychecker, but it isn't of real help
> in gui dev'ing (at least in my GUIs ;-)).
> So, a compiler could ease things a bit here. While I want the complicated
> stuff remaining written in Python, I'd write the GUI things in Java. Java,
> because I plan to run the app on Linux too (dev'ing on Windows for now).
> Otherwise *.NET could be an option (well, perhaps I should look at Mono).
> Now, if I go the Java route, how can I call my Python stuff from Java, if I
> do not want to use Jython, which lags behind CPython 3 minor versions (2.1.
> vs. 2.4)? Google showed up JPype, but this seems to enable me the other way
> round: Call Java code from CPython. Or are there any plans to make this
> possible the other way round too?
> Any other options here?
> Many thanks in advance
> Franz GEIGER
Well, lemme see how you can have you cake and eat it too :)
Right now, JPype does not allow you to call Python code "as is".
Besides, adaptic a static language like java to a dynamic language like
python is "easy". Going the other way is ... impossible.
What JPype allows you to do is use Java classes as though they were
regular Python classes. This includes Standard java classes as well as
classes you've written youself.
Somewhere in the back of my head there are plans to make python classes
available to Java. The goal though wil be to be able to make use of the
many frameworks availabel in the Java worlds, be it server technologies
like servlets and EJBs, or client-side stuff like Eclipse.
Aside form that, your problem is exactly one of the problems that
incited me to write JPype. Python GUI toolkits just seem, to my
java-trained mind, too hard to use.
So, what I would do is have Python be the "host", the controller,
calling the JAVA-built GUI and registering listeners. JPype does not
allow subclassing of Java classes in python, but you can crerate JProxy
that "implement" any Java interface. The standard idiom for Java event
and callbacks is based on the use of interfaces anyway. In fact, baring
the very rare cases where Swing requires subclasses, you could crerate
your whole GUI using Swing and not writing a single line of Java.
Hopefully this answers your question. IF you have any, feel free to
contact me through sourceforge.
More information about the Python-list