build("io.micronaut.micronaut-graal-languages:micronaut-graalpy")
Micronaut Graal languages
Integrate Graal based dynamic languages with Micronaut Framework
Version: 1.0.0
1 Introduction
Micronaut Graal Languages is a collection of components for integration of Graal based dynamic languages with Micronaut Framework.
Note: Micronaut React server-side rendering, which is based on GraalJS, is part of Micronaut Views.
2 Release History
For this project, you can find a list of releases (with release notes) here:
3 GraalPy
Micronaut GraalPy is an annotation-based feature to expose Python modules as Java Beans.
3.1 Getting Started
To use the GraalPy integration:
1) Add the micronaut-graalpy
dependency on your classpath:
<dependency>
<groupId>io.micronaut.micronaut-graal-languages</groupId>
<artifactId>micronaut-graalpy</artifactId>
<scope>build</scope>
</dependency>
2) Add the GraalPy Maven plugin
Gradle plugin not available yet |
<build>
<plugins>
<plugin>
<groupId>org.graalvm.python</groupId>
<artifactId>graalpy-maven-plugin</artifactId>
<version>${graalpy.version}</version>
<executions>
<execution>
<configuration>
<packages>
<package>termcolor==2.2</package> // (1)
</packages>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<!-- ... -->
1 | Optional list of Python Package Index (PyPI) packages to be automatically installed during the build. |
3) Add the dealer.py
Python script to the resources folder. The location follows the convention
recognized by the GraalPy support library.
import random
def shuffle(data):
random.shuffle(data)
return data
4) Create a Java interface for the Python module
import io.micronaut.graal.graalpy.annotations.GraalPyModule;
@GraalPyModule("dealer") // (1)
public interface DealerService {
Object[] shuffle(Object[] cards); // (2)
}
1 | Use GraalPyModule annotation to map this interface to a Python module. The value of the annotation is the name of the Python module that should be used. |
2 | Methods in the interface will be automatically mapped to Python functions of the same name. The mapping follows the polyglot type mapping rules. |
5) This library will provide an injectable implementation of the interface backed by the Python module.
import io.micronaut.context.annotation.Bean;
import jakarta.inject.Inject;
@Bean
public class DocExample {
@Inject DealerService dealerService;
public Object[] play() {
Object[] cards = new Object[] { 1, 2, 3 };
cards = dealerService.shuffle(cards);
// ...
return cards;
}
}
See GraalPyModule documentation for more details about global state, threading, and limitations. |
4 Repository
You can find the source code of this project in this repository: