This demo illustrates how GraalPy can be used to embed Pygal, a dynamic SVG charting library written in Python, in a Spring Boot application. In particular, this demo shows four different approaches to interact with Pygal from Java.
Install GraalVM for JDK 23 and set the value of JAVA_HOME
accordingly.
We recommend using SDKMAN!. (For other download options, see GraalVM Downloads.)
sdk install java 23-graal
To start the demo, simply run:
./mvnw package spring-boot:run
When the demo runs, open the following URLs in a browser:
The DemoController
uses four services that all render the same XY chart using different implementations:
PyGalServicePureJava
interacts with Pygal and Python using Java interfaces andValue.as(Class<T> targetType)
. This is the recommended approach.PyGalServicePurePython
embeds the Python sample code from the Pygal documentation.PyGalServiceMixed
uses a Python function which is invoked with Java values.PyGalServiceValueAPIDynamic
uses the Value API from the GraalVM SDK.
The DemoApplicationTests
ensures that all four service implementations render the same XY chart. Run it with ./mvnw test
.
Note: This demo uses a single
GraalPyContext
, which can execute Python code in only one thread at a time. Threads running Python code are internally scheduled in round-robin fashion. Pure Python packages including Pygal can be used in multiple GraalPy contexts, for example one context per thread, to improve the throughput of the application. Other demos such asgraalwasm-micronaut-photon
illustrate how to pool multiple contexts.