Skip to content

Commit

Permalink
Merge pull request #259 from graalvm/fix-graalpy-package-install
Browse files Browse the repository at this point in the history
Attempt to fix graalpy-install-package
  • Loading branch information
bhoran committed Jul 19, 2024
2 parents 26043df + 3063163 commit 14eec59
Show file tree
Hide file tree
Showing 13 changed files with 362 additions and 278 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/graalpy-install-package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: graalpy-install-package
on:
push:
paths:
- 'graalpy-install-package/**'
- '.github/workflows/graalpy-install-package.yml'
pull_request:
paths:
- 'graalpy-install-package/**'
- '.github/workflows/graalpy-install-package.yml'
workflow_dispatch:
permissions:
contents: read
jobs:
run:
name: Run 'graalpy-install-package'
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- uses: graalvm/setup-graalvm@v1
with:
java-version: '21.0.2'
distribution: 'graalvm'
github-token: ${{ secrets.GITHUB_TOKEN }}
cache: 'maven'
native-image-job-reports: 'true'
- name: Set up virtual X11
run: |
sudo apt-get install xvfb
Xvfb :19 -screen 0 1024x768x16 &
echo "DISPLAY=:19" >> "$GITHUB_ENV"
- name: Run 'graalpy-install-package java'
run: |
cd graalpy-install-package
mvn --no-transfer-progress clean package
mvn exec:exec &
PID=$!
sleep 60
kill $PID
# Cannot test native due to https://github.com/graalvm/native-build-tools/issues/584
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,19 @@ cd graalvm-demos
<tbody>
<tr>
<td align="left" width="30%"><a href="/graalpy-notebook-example/">graalpy-notebook-example</a><br><a href="https://github.com/graalvm/graalvm-demos/actions/workflows/graalpy-notebook-example.yml"><img alt="graalpy-notebook-example" src="https://github.com/graalvm/graalvm-demos/actions/workflows/graalpy-notebook-example.yml/badge.svg" /></a></td>
<td align="left" width="70%">Demonstrates how to embed Python in a Java application. It creates a Python `venv`, and installs required Python packages through a Maven configuration. <br><strong>Technologies: </strong>GraalPy</td>
<td align="left" width="70%">Demonstrates how to embed Python in a Java application. It creates a Python <code>venv</code>, and installs required Python packages through a Maven configuration. <br><strong>Technologies: </strong>GraalPy</td>
</tr>
<tr>
<td align="left" width="30%"><a href="/graalpy-embedding-demo/">graalpy-embedding-demo</a><br><a href="https://github.com/graalvm/graalvm-demos/actions/workflows/graalpy-embedding-demo.yml"><img alt="graalpy-embedding-demo" src="https://github.com/graalvm/graalvm-demos/actions/workflows/graalpy-embedding-demo.yml/badge.svg" /></a></td>
<td align="left" width="70%">Demonstrates how to embed GraalPy in a Java application using Maven. <br><strong>Technologies: </strong>GraalPy</td>
</tr>
<tr>
<td align="left" width="30%"><a href="/graalpy-embedded-pillow/">graalpy-embedded-pillow</a><br><a href="https://github.com/graalvm/graalvm-demos/actions/workflows/graalpy-embedded-pillow.yml"><img alt="graalpy-embedded-pillow" src="https://github.com/graalvm/graalvm-demos/actions/workflows/graalpy-embedded-pillow.yml/badge.svg" /></a></td>
</tr>
<tr>
<td align="left" width="70%">Demonstrates how to use GraalPy to create an instance of a proxied Python class from a Java application, built with Maven. <br><strong>Technologies: </strong>GraalPy</td>
<td align="left" width="30%"><a href="/graalpy-install-package/">graalpy-install-package</a><br><a href="https://github.com/graalvm/graalvm-demos/actions/workflows/graalpy-install-package.yml"><img alt="graalpy-embedding-demo" src="https://github.com/graalvm/graalvm-demos/actions/workflows/graalpy-install-package.yml/badge.svg" /></a></td>
<td align="left" width="70%">Demonstrates how to use GraalPy to access a Python class from a Java application, built with Maven. <br><strong>Technologies: </strong>GraalPy</td>
</tr>
</tbody>
</table>
Expand Down
47 changes: 40 additions & 7 deletions graalpy-install-package/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# Using GraalPy to Access a Python Class from a Java Application

This small application demonstrates how to use GraalPy to access a Python class from a Java application, built with Maven.
It is based on the [GraalPy Maven Archetype](https://www.graalvm.org/latest/reference-manual/python/#maven) modified for the specific example.

It includes the following steps:
1. Install the [Pyfiglet](https://github.com/pwaller/pyfiglet) module. (Pyfiglet is an ASCII art module to create styled text.)
2. Create a Python class ([PyfigletWrapper.py](src/main/resources/PyfigletWrapper.py)) that acts as a wrapper for the pyfiglet module.
2. Create a Python class ([pyfigletwrapper.py](src/main/resources/vfs/proj/pyfigletwrapper.py)) that acts as a wrapper for the pyfiglet module.
3. Create a Java interface ([PyfigletProxy.java](src/main/java/com/oracle/example/graalpy/PyfigletProxy.java)) that acts as a proxy for the wrapper.
4. Create a Swing JFrame ([PyfigletFrame.java](src/main/java/com/oracle/example/graalpy/PyfigletFrame.java)) that calls the proxy's `format` method.
5. Create a [Main](src/main/java/com/oracle/example/graalpy/Main.java) class that sets up the GraalPy environment, creates an instance of `PyfigletProxy`, then creates and opens an instance of `PyfigletFrame`.

## Preparation

Install a JDK 21, ensure that it is on your classpath, and set the value of `JAVA_HOME` accordingly.
We recommend GraalVM for JDK 21 that can be easily installed using [SDKMAN!](https://sdkman.io/). (For other download options, see [GraalVM Downloads](https://www.graalvm.org/downloads/).)
We recommend GraalVM for JDK 21 that can be easily installed on macOS and Linux using [SDKMAN!](https://sdkman.io/). (For other download options, see [GraalVM Downloads](https://www.graalvm.org/downloads/).)
```bash
sdk install java 21.0.2-graal
```
Expand All @@ -20,16 +22,47 @@ sdk install java 21.0.2-graal

1. Build the application:
```bash
mvn compile
mvn package
```
2. Run the application:
```bash
mvn exec:exec -Prun
```

mvn exec:exec
```

![Pyfiglet Java Application](Pyfiglet%20GUI.gif)

## (Optional) Build and Run a Native Executable

If you [installed GraalVM](https://www.graalvm.org/downloads/), you can use GraalVM Native Image to build this Java-Python application into a native executable and then run it for reduced footprint and better startup performance.

> Note: The Native Image build configuration files are in [src/main/resources/META-INF/native-image](src/main/resources/META-INF/native-image).
For more information, see [Native Image Build Configuration](https://www.graalvm.org/latest/reference-manual/native-image/overview/BuildConfiguration/).

1. Build the application as above.
```bash
mvn package
```

2. Run the application to prepare it for the GraalVM Native Image build.
This enables the [GraalVM Native Image Tracing Agent](https://www.graalvm.org/latest/reference-manual/native-image/guides/configure-with-tracing-agent/) to collect metadata required to build the native executable.
Use the application by changing fonts and clicking buttons so that the agent collects the required runtime information.
```bash
mvn exec:exec -Pnative
```

3. Build a native executable using the command below.
(This uses the metadata you collected in step **2**.)
```bash
mvn package -Pnative
```
It creates the native executable called `package-graalpy` in the _target/_ directory.

4. Run the native executable:
```bash
./target/package-graalpy
```
Because the application uses AWT, you need to set the value of the `JAVA_HOME` environment variable to a valid JDK distribution, so that AWT can load resources that are shipped with the JDK.

See the [pom.xml](./pom.xml) file for configuration details.

> For more details on how to embed GraalPy in your Java applications, see [Embedding Languages](https://www.graalvm.org/latest/reference-manual/embed-languages/).
> For more details on how to embed GraalPy in your Java applications, see [Embedding Languages](https://www.graalvm.org/latest/reference-manual/embed-languages/).
23 changes: 0 additions & 23 deletions graalpy-install-package/graalpy.cmd

This file was deleted.

27 changes: 0 additions & 27 deletions graalpy-install-package/graalpy.sh

This file was deleted.

Loading

0 comments on commit 14eec59

Please sign in to comment.