Class PySession
java.lang.Object
com.eco.bio7.pyffm.PySession
An embedded CPython in the Java process (Foreign Function and Memory API):
one per process, it can't be stopped and started again. All Python code runs
in one thread ("Python"); the calls of other threads wait for it.
PySession py = PySession.start(PyStartOptions.of(Path.of("/usr/bin/python3")).withConsole((t, e) -> System.out.print(t)));
py.assign("x", PyValue.of(1.0, 2.0, 3.0));
PyValue mean = py.eval("float(sum(x) / len(x))");
py.console("print(x)");
-
Method Summary
Modifier and TypeMethodDescriptionvoidvoidvoidA 2-d array (rows).voidvoidAssigns a value to a variable of __main__ (numbers as numpy arrays).voidvoidcloseFigure(int figure) Closes a figure (-1: all).voidA line typed in the console (statements of several lines are completed with the next lines): the output and the prompt go to the console.static PySessioncurrent()The running session or null.Evaluates an expression in __main__ and returns its value.voidExecutes statements in __main__ (raises an exception with the traceback).The executable of the selected Python.The value of a variable of __main__.longThe activity of Python: the number of the calls so far (e.g., to update a display after Python ran code).voidInterrupts the running Python code (KeyboardInterrupt), as Ctrl+C.booleanisBusy()Returns true if Python evaluates (a call, a console line, a script).booleanReturns true in the thread of Python (e.g., in a call of Python to Java).library()The shared library of Python.int[]The numbers of the open figures (matplotlib).voidprompt()Writes the prompt of the console.voidDeletes a variable of __main__ (if it exists).voidrenderFigure(int figure) Draws a figure again (e.g., to show it in the view).voidresizePlot(int figure, int width, int height, double scale) Sets the size of the plot view and draws the figure again in that size (in the thread of Python, after the running code).voidRuns code in __main__ like a script (errors printed to the console).voidAs runAsync(code); label: the name of the run for the run listener (null: not shown as running, e.g., a debug session waiting at breakpoints).voidRuns a script file in __main__ (errors are printed to the console), then draws the new figures.voidsaveFigure(int figure, Path file, String format, int dpi) Saves a figure (matplotlib savefig).voidsetActivityListener(Runnable listener) Sets a listener called after user code ran (a console line, a script, code of runAsync), e.g., to update a list of the variables.voidsetCallHandler(com.eco.bio7.pyffm.PyCallHandler handler) voidsetPlotListener(com.eco.bio7.pyffm.PyPlotListener listener) voidsetRunListener(com.eco.bio7.pyffm.PyRunListener listener) Sets a listener called when user code starts and ends (a console line, a script, code of runAsync with a label), e.g., to show a job.voidsetWaitLimit(BooleanSupplier isLimited, long millis) Limits the time a thread waits for Python (e.g., the UI thread): the call fails with an exception instead of blocking longer.static PySessionstart(com.eco.bio7.pyffm.PyStartOptions options) Starts the embedded Python (once per process).The variables of __main__ (name and type), e.g., for an object browser.version()The version of Python (e.g., "3.12.4").static booleanReturns true if the embedded Python was started in this process.
-
Method Details
-
start
public static PySession start(com.eco.bio7.pyffm.PyStartOptions options) throws com.eco.bio7.pyffm.PyException Starts the embedded Python (once per process).- Throws:
com.eco.bio7.pyffm.PyException- if Python can't be found or started
-
current
The running session or null. -
wasStarted
public static boolean wasStarted()Returns true if the embedded Python was started in this process. -
version
The version of Python (e.g., "3.12.4"). -
executable
The executable of the selected Python. -
library
The shared library of Python. -
getActivity
public long getActivity()The activity of Python: the number of the calls so far (e.g., to update a display after Python ran code). -
isBusy
public boolean isBusy()Returns true if Python evaluates (a call, a console line, a script). -
isPythonThread
public boolean isPythonThread()Returns true in the thread of Python (e.g., in a call of Python to Java). -
setWaitLimit
Limits the time a thread waits for Python (e.g., the UI thread): the call fails with an exception instead of blocking longer. -
setPlotListener
public void setPlotListener(com.eco.bio7.pyffm.PyPlotListener listener) -
setCallHandler
public void setCallHandler(com.eco.bio7.pyffm.PyCallHandler handler) -
openFigures
public int[] openFigures()The numbers of the open figures (matplotlib). -
setRunListener
public void setRunListener(com.eco.bio7.pyffm.PyRunListener listener) Sets a listener called when user code starts and ends (a console line, a script, code of runAsync with a label), e.g., to show a job. Called in the thread of Python (without the GIL): must return at once. -
setActivityListener
Sets a listener called after user code ran (a console line, a script, code of runAsync), e.g., to update a list of the variables. Called in the thread of Python (after the code, without the GIL): must return at once. -
exec
Executes statements in __main__ (raises an exception with the traceback).- Throws:
com.eco.bio7.pyffm.PyException
-
eval
-
get
-
assign
-
assign
- Throws:
com.eco.bio7.pyffm.PyException
-
assign
- Throws:
com.eco.bio7.pyffm.PyException
-
assign
-
assign
- Throws:
com.eco.bio7.pyffm.PyException
-
assign
A 2-d array (rows).- Throws:
com.eco.bio7.pyffm.PyException
-
remove
Deletes a variable of __main__ (if it exists).- Throws:
com.eco.bio7.pyffm.PyException
-
console
A line typed in the console (statements of several lines are completed with the next lines): the output and the prompt go to the console. Returns at once. -
prompt
public void prompt()Writes the prompt of the console. -
runFile
Runs a script file in __main__ (errors are printed to the console), then draws the new figures. Returns at once. -
runAsync
Runs code in __main__ like a script (errors printed to the console). Returns at once. -
runAsync
-
interrupt
public void interrupt()Interrupts the running Python code (KeyboardInterrupt), as Ctrl+C. Has no effect if Python is idle. -
resizePlot
public void resizePlot(int figure, int width, int height, double scale) Sets the size of the plot view and draws the figure again in that size (in the thread of Python, after the running code).- Parameters:
figure- the figure to draw (0: none)
-
renderFigure
public void renderFigure(int figure) Draws a figure again (e.g., to show it in the view). -
closeFigure
public void closeFigure(int figure) Closes a figure (-1: all). -
saveFigure
-
variables
-