Does anyone know how to run Jython with "interactive mode" (or whatever the correct terminology is, this is the terminology in Iron Python)? Basically, interactive mode is similar to running a regular script using the eval() method, but statements which evaluate to a result print out the result.
So, for example:
for i in range(5): i * i
In interactive mode, i * i would print out to the script context output stream, so the output would look like:
In non-interactive mode, nothing would be printed out.0
1
4
9
16
The higher-level purpose of this is I'm trying to create a Swing "command console" which emulates the standard Python command console, but can be added to Swing applications.
Here's a short blurb of how I'm creating the Jython script engine and running the test script (note: you will need the Jython library, you can find it here. Java SE6 or newer is also required for the Java scripting classes).
// runs the script, but interactive mode is not turned on ScriptEngine engine = new ScriptEngineManager().getEngineByName("python"); String command = "for i in range(5):\r\n\ti * i"; engine.eval(command);
edit:
I figured out a work-around to this.
Instead of using the Java API scripting, I created an InteractiveInterpreter part of the Jython package.
InteractiveInterpreter engine = new InteractiveInterpreter(); String command = "for i in range(5):\r\n\ti * i"; engine.runsource(commands);
Hopefully next month-ish I will put the whole code in the tips section (still a few things to work out)