Class PySession

java.lang.Object
com.eco.bio7.pyffm.PySession

public final class PySession extends Object
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 Type
    Method
    Description
    void
    assign(String name, boolean... values)
     
    void
    assign(String name, double... values)
     
    void
    assign(String name, double[][] matrix)
    A 2-d array (rows).
    void
    assign(String name, int... values)
     
    void
    assign(String name, PyValue value)
    Assigns a value to a variable of __main__ (numbers as numpy arrays).
    void
    assign(String name, String... values)
     
    void
    closeFigure(int figure)
    Closes a figure (-1: all).
    void
    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.
    static PySession
    The running session or null.
    eval(String expression)
    Evaluates an expression in __main__ and returns its value.
    void
    exec(String code)
    Executes statements in __main__ (raises an exception with the traceback).
    The executable of the selected Python.
    get(String name)
    The value of a variable of __main__.
    long
    The activity of Python: the number of the calls so far (e.g., to update a display after Python ran code).
    void
    Interrupts the running Python code (KeyboardInterrupt), as Ctrl+C.
    boolean
    Returns true if Python evaluates (a call, a console line, a script).
    boolean
    Returns true in the thread of Python (e.g., in a call of Python to Java).
    The shared library of Python.
    int[]
    The numbers of the open figures (matplotlib).
    void
    Writes the prompt of the console.
    void
    remove(String name)
    Deletes a variable of __main__ (if it exists).
    void
    renderFigure(int figure)
    Draws a figure again (e.g., to show it in the view).
    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).
    void
    Runs code in __main__ like a script (errors printed to the console).
    void
    runAsync(String code, String label)
    As 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).
    void
    runFile(Path file)
    Runs a script file in __main__ (errors are printed to the console), then draws the new figures.
    void
    saveFigure(int figure, Path file, String format, int dpi)
    Saves a figure (matplotlib savefig).
    void
    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.
    void
    setCallHandler(com.eco.bio7.pyffm.PyCallHandler handler)
     
    void
    setPlotListener(com.eco.bio7.pyffm.PyPlotListener listener)
     
    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.
    void
    setWaitLimit(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 PySession
    start(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.
    The version of Python (e.g., "3.12.4").
    static boolean
    Returns true if the embedded Python was started in this process.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public static PySession current()
      The running session or null.
    • wasStarted

      public static boolean wasStarted()
      Returns true if the embedded Python was started in this process.
    • version

      public String version()
      The version of Python (e.g., "3.12.4").
    • executable

      public Path executable()
      The executable of the selected Python.
    • library

      public Path 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

      public void setWaitLimit(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.
    • 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

      public void setActivityListener(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. Called in the thread of Python (after the code, without the GIL): must return at once.
    • exec

      public void exec(String code) throws com.eco.bio7.pyffm.PyException
      Executes statements in __main__ (raises an exception with the traceback).
      Throws:
      com.eco.bio7.pyffm.PyException
    • eval

      public PyValue eval(String expression) throws com.eco.bio7.pyffm.PyException
      Evaluates an expression in __main__ and returns its value.
      Throws:
      com.eco.bio7.pyffm.PyException
    • get

      public PyValue get(String name) throws com.eco.bio7.pyffm.PyException
      The value of a variable of __main__.
      Throws:
      com.eco.bio7.pyffm.PyException
    • assign

      public void assign(String name, PyValue value) throws com.eco.bio7.pyffm.PyException
      Assigns a value to a variable of __main__ (numbers as numpy arrays).
      Throws:
      com.eco.bio7.pyffm.PyException
    • assign

      public void assign(String name, double... values) throws com.eco.bio7.pyffm.PyException
      Throws:
      com.eco.bio7.pyffm.PyException
    • assign

      public void assign(String name, int... values) throws com.eco.bio7.pyffm.PyException
      Throws:
      com.eco.bio7.pyffm.PyException
    • assign

      public void assign(String name, String... values) throws com.eco.bio7.pyffm.PyException
      Throws:
      com.eco.bio7.pyffm.PyException
    • assign

      public void assign(String name, boolean... values) throws com.eco.bio7.pyffm.PyException
      Throws:
      com.eco.bio7.pyffm.PyException
    • assign

      public void assign(String name, double[][] matrix) throws com.eco.bio7.pyffm.PyException
      A 2-d array (rows).
      Throws:
      com.eco.bio7.pyffm.PyException
    • remove

      public void remove(String name) throws com.eco.bio7.pyffm.PyException
      Deletes a variable of __main__ (if it exists).
      Throws:
      com.eco.bio7.pyffm.PyException
    • console

      public void console(String line)
      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

      public void runFile(Path file)
      Runs a script file in __main__ (errors are printed to the console), then draws the new figures. Returns at once.
    • runAsync

      public void runAsync(String code)
      Runs code in __main__ like a script (errors printed to the console). Returns at once.
    • runAsync

      public void runAsync(String code, String label)
      As 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).
    • 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

      public void saveFigure(int figure, Path file, String format, int dpi) throws com.eco.bio7.pyffm.PyException
      Saves a figure (matplotlib savefig).
      Parameters:
      format - png, svg, pdf, eps, jpg, tif
      dpi - the resolution (0: the one of the figure)
      Throws:
      com.eco.bio7.pyffm.PyException
    • variables

      public List<String[]> variables() throws com.eco.bio7.pyffm.PyException
      The variables of __main__ (name and type), e.g., for an object browser.
      Throws:
      com.eco.bio7.pyffm.PyException