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 25 and set the value of JAVA_HOME accordingly.
We recommend using SDKMAN!. (For other download options, see GraalVM Downloads.)
sdk install java 25.0.2-graalTo start the demo, simply run:
./mvnw package spring-boot:runWhen 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:
PyGalServicePureJavainteracts with Pygal and Python using Java interfaces andValue.as(Class<T> targetType). This is the recommended approach.PyGalServicePurePythonembeds the Python sample code from the Pygal documentation.PyGalServiceMixeduses a Python function which is invoked with Java values.PyGalServiceValueAPIDynamicuses 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-photonillustrate how to pool multiple contexts.