Skip to content

Commit

Permalink
Upgrade Java and Python Integration Example for GraalPy 23.1 unchaine…
Browse files Browse the repository at this point in the history
…d integration
  • Loading branch information
timfel committed Dec 14, 2023
1 parent 2f99a72 commit c27faf4
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 188 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/graalpy-notebook-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ jobs:
with:
java-version: ${{ matrix.java-version }}
distribution: 'graalvm'
components: 'python'
github-token: ${{ secrets.GITHUB_TOKEN }}
cache: 'maven'
- name: Run 'graalpy-notebook-example'
run: |
cd graalpy-notebook-example
mvn --no-transfer-progress compile
#./install-deps.sh (only for local running)
# mvn exec:java (skip running the example)
# mvn exec:java -Dexec.mainClass=com.oracle.example.javapython.Main (skip running the example, it is interactive)
45 changes: 6 additions & 39 deletions graalpy-notebook-example/README.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,20 @@
# Java and Python Integration Example for GraalVM
# Java and Python Integration Example

This example demonstrates how to integrate Python in a Java application and run on GraalVM.

### Prerequisites

- [GraalVM](https://www.graalvm.org/)
- [GraalPy](https://www.graalvm.org/latest/reference-manual/python/)

>Note: As of GraalVM for JDK 21, the Python runtime (GraalPy) is available as a standalone distribution.
## Preparation

1. Download and install the latest GraalVM JDK using [SDKMAN!](https://sdkman.io/).
```bash
sdk install java 21.0.1-graal
```
2. Download and install GraalPy using `pyenv`:
```bash
pyenv install graalpy-23.1.0
```
Alternatively, download a compressed GraalPy installation file appropriate for your platform from [GitHub releases](https://github.com/oracle/graalpython/releases).

3. Set the `GRAALPY_HOME` environment variable:
```bash
export GRAALPY_HOME=/.pyenv/versions/graalpy-23.1.0
```
2. Download or clone GraalVM demos repository and navigate into the `graalpy-notebook-example` directory:
```bash
git clone https://github.com/graalvm/graalvm-demos
```
```bash
cd graalvm-demos/graalpy-notebook-example
```

## Build and Run Demo

1. Compile the example:
```bash
mvn compile
```

2. Install the dependencies:
2. Run the example:
```bash
./install-deps.sh
```

3. Run the example:
```bash
mvn exec:java
mvn exec:java -Dexec.mainClass=com.oracle.example.javapython.Main
```

In the application, try loading the _test1.py_ example notebook and experiment with it.

This demo works on Open JDK, Oracle JDK or GraalVM.
GraalVM offers (much) better performance than the other two.
23 changes: 23 additions & 0 deletions graalpy-notebook-example/graalpy.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@echo off
REM Invoke the GraalPy launcher through Maven, passing any arguments passed to
REM this script via GRAAL_PYTHON_ARGS. To avoid having to deal with multiple
REM layers of escaping, we store the arguments into GRAAL_PYTHON_ARGS delimited
REM with vertical tabs.

REM Since BAT files cannot easily generate vertical tabs, we create a helper
REM script to do it for us. We're calling Maven soon anyway, so Java must be
REM available.
set JAVA="%JAVA_HOME%/bin/java"
if not defined JAVA_HOME set JAVA=java
echo class VTabCreator { public static void main(String[] args) { System.out.print('\013'); } } > VTabCreator.java
for /f "delims=" %%i in ('%JAVA% VTabCreator.java') do set VTAB=%%i
del VTabCreator.java

REM Store each argument separated by vtab
set GRAAL_PYTHON_ARGS=
:loop
set GRAAL_PYTHON_ARGS=%GRAAL_PYTHON_ARGS%%VTAB%%~1
shift /1
if not "%~1"=="" goto loop

mvn -f "%~dp0pom.xml" exec:exec -Dexec.executable=java -Dexec.args="--module-path %%classpath -Dorg.graalvm.launcher.executablename=%~0 --module org.graalvm.py.launcher/com.oracle.graal.python.shell.GraalPythonMain"
27 changes: 27 additions & 0 deletions graalpy-notebook-example/graalpy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env bash

source="${BASH_SOURCE[0]}"
while [ -h "$source" ] ; do
prev_source="$source"
source="$(readlink "$source")";
if [[ "$source" != /* ]]; then
# if the link was relative, it was relative to where it came from
dir="$( cd -P "$( dirname "$prev_source" )" && pwd )"
source="$dir/$source"
fi
done
location="$( cd -P "$( dirname "$source" )" && pwd )"

if [ -z "$JAVA_HOME" ]; then
JAVA=java
else
JAVA="${JAVA_HOME}/bin/java"
fi

for var in "$@"; do
args="${args}$(printf "\v")${var}"
done

curdir=`pwd`
export GRAAL_PYTHON_ARGS="${args}$(printf "\v")"
mvn -f "${location}/pom.xml" exec:exec -Dexec.executable="${JAVA}" -Dexec.workingdir="${curdir}" -Dexec.args="--module-path %classpath '-Dorg.graalvm.launcher.executablename=$0' --module org.graalvm.py.launcher/com.oracle.graal.python.shell.GraalPythonMain"
10 changes: 0 additions & 10 deletions graalpy-notebook-example/install-deps.sh

This file was deleted.

Loading

0 comments on commit c27faf4

Please sign in to comment.