Skip to content

Commit

Permalink
Add Pygal on GraalPy demos.
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Oct 7, 2024
1 parent 428dd80 commit 433c403
Show file tree
Hide file tree
Showing 39 changed files with 2,193 additions and 0 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/graalpy-micronaut-pygal-charts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Test GraalPy Micronaut Pygal Charts
on:
push:
paths:
- 'graalpy/graalpy-micronaut-pygal-charts/**'
- '.github/workflows/graalpy-micronaut-pygal-charts.yml'
pull_request:
paths:
- 'graalpy/graalpy-micronaut-pygal-charts/**'
- '.github/workflows/graalpy-micronaut-pygal-charts.yml'
workflow_dispatch:
permissions:
contents: read
env:
NATIVE_IMAGE_OPTIONS: '-J-Xmx16g'
jobs:
run:
name: 'graalpy-micronaut-pygal-charts'
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- uses: graalvm/setup-graalvm@v1
with:
java-version: '23.0.0'
distribution: 'graalvm'
github-token: ${{ secrets.GITHUB_TOKEN }}
cache: 'maven'
native-image-job-reports: 'true'
- name: Build, test, and run 'graalpy-micronaut-pygal-charts' using Maven
run: |
cd graalpy/graalpy-micronaut-pygal-charts
./mvnw --no-transfer-progress clean test -Dmicronaut.http.client.read-timeout=1m
./mvnw --no-transfer-progress mn:run &
mnpid="$!"
sleep 30
curl -s -D - -o /dev/null http://localhost:8080/java
kill $mnpid
- name: Build and run native 'graalpy-micronaut-pygal-charts' using Maven
run: |
cd graalpy/graalpy-micronaut-pygal-charts
./mvnw --no-transfer-progress clean package -DskipTests -Dpackaging=native-image
./target/graalpy-micronaut &
mnpid="$!"
sleep 20
curl -s -D - -o /dev/null http://localhost:8080/python
kill $mnpid
47 changes: 47 additions & 0 deletions .github/workflows/graalpy-spring-boot-pygal-charts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Test GraalPy Spring Pygal Charts
on:
push:
paths:
- 'graalpy/graalpy-spring-boot-pygal-charts/**'
- '.github/workflows/graalpy-spring-boot-pygal-charts.yml'
pull_request:
paths:
- 'graalpy/graalpy-spring-boot-pygal-charts/**'
- '.github/workflows/graalpy-spring-boot-pygal-charts.yml'
workflow_dispatch:
permissions:
contents: read
env:
NATIVE_IMAGE_OPTIONS: '-J-Xmx16g'
jobs:
run:
name: 'graalpy-spring-boot-pygal-charts'
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- uses: graalvm/setup-graalvm@v1
with:
java-version: '23.0.0'
distribution: 'graalvm'
github-token: ${{ secrets.GITHUB_TOKEN }}
cache: 'maven'
native-image-job-reports: 'true'
- name: Build, test, and run 'graalpy-spring-boot-pygal-charts' using Maven
run: |
cd graalpy/graalpy-spring-boot-pygal-charts
./mvnw --no-transfer-progress clean test -Dspring.mvc.async.request-timeout=60000
./mvnw --no-transfer-progress spring-boot:run &
sbpid="$!"
sleep 30
curl -s -D - -o /dev/null http://localhost:8080/java
kill $sbpid
- name: Build and run native 'graalpy-spring-boot-pygal-charts' using Maven
run: |
cd graalpy/graalpy-spring-boot-pygal-charts
./mvnw --no-transfer-progress clean -DskipTests -Pnative native:compile
./target/graalpy-springboot &
sbpid="$!"
sleep 20
curl -s -D - -o /dev/null http://localhost:8080/python
kill $sbpid
15 changes: 15 additions & 0 deletions graalpy/graalpy-micronaut-pygal-charts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Thumbs.db
.DS_Store
.gradle
build/
target/
out/
.micronaut/
.idea
*.iml
*.ipr
*.iws
.project
.settings
.classpath
.factorypath
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
wrapperVersion=3.3.2
distributionType=only-script
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip
45 changes: 45 additions & 0 deletions graalpy/graalpy-micronaut-pygal-charts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
## SVG Charts with GraalPy and Micronaut

This demo illustrates how GraalPy can be used to embed [Pygal](https://github.com/Kozea/pygal), a dynamic SVG charting library written in Python, in a Micronaut application.
In particular, this demo shows four different approaches to interact with Pygal from Java.

## Preparation

Install GraalVM for JDK 23 and set the value of `JAVA_HOME` accordingly.
We recommend using [SDKMAN!](https://sdkman.io/). (For other download options, see [GraalVM Downloads](https://www.graalvm.org/downloads/).)

```bash
sdk install java 23-graal
```

## Run the Application

To start the demo, simply run:

```bash
./mvnw package mn:run
```

When the demo runs, open the follwing URLs in a browser:

| URL | Service |
|:------------------------------|:------------------------------|
| http://localhost:8080/java | [`PyGalServicePureJava`](src/main/java/com/example/PyGalServicePureJava.java) |
| http://localhost:8080/python | [`PyGalServicePurePython`](src/main/java/com/example/PyGalServicePurePython.java) |
| http://localhost:8080/mixed | [`PyGalServiceMixed`](src/main/java/com/example/PyGalServiceMixed.java) |
| http://localhost:8080/dynamic | [`PyGalServiceValueAPIDynamic`](src/main/java/com/example/PyGalServiceValueAPIDynamic.java) |


## Implementation Details

The `DemoController` uses four services that all render the same XY chart using different implementations:

- [`PyGalServicePureJava`](src/main/java/com/example/PyGalServicePureJava.java) interacts with Pygal and Python using Java interfaces and `Value.as(Class<T> targetType)`. This is the recommended approach.
- [`PyGalServicePurePython`](src/main/java/com/example/PyGalServicePurePython.java) embeds the Python sample code from the Pygal documentation.
- [`PyGalServiceMixed`](src/main/java/com/example/PyGalServiceMixed.java) uses a Python function which is invoked with Java values.
- [`PyGalServiceValueAPIDynamic`](src/main/java/com/example/PyGalServiceValueAPIDynamic.java) uses the [Value](https://www.graalvm.org/sdk/javadoc/org/graalvm/polyglot/Value.html) API from the GraalVM SDK.


The `DemoTest` ensures that all four service implementations render the same XY chart. Run it with `./mvnw test`.

> Note: This demo uses a single [`GraalPyContext`](src/main/java/com/example/GraalPyContext.java), which can be accessed from only one thread at a time to evaluate Python code. 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 illustrate how to pool multiple contexts.
37 changes: 37 additions & 0 deletions graalpy/graalpy-micronaut-pygal-charts/aot-jar.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# AOT configuration properties for jar packaging
# Please review carefully the optimizations enabled below
# Check https://micronaut-projects.github.io/micronaut-aot/latest/guide/ for more details

# Caches environment property values: environment properties will be deemed immutable after application startup.
cached.environment.enabled=true

# Precomputes Micronaut configuration property keys from the current environment variables
precompute.environment.properties.enabled=true

# Replaces logback.xml with a pure Java configuration
logback.xml.to.java.enabled=true

# Converts YAML configuration files to Java configuration
yaml.to.java.config.enabled=true

# Scans for service types ahead-of-time, avoiding classpath scanning at startup
serviceloading.jit.enabled=true

# Scans reactive types at build time instead of runtime
scan.reactive.types.enabled=true

# Deduces the environment at build time instead of runtime
deduce.environment.enabled=true

# Checks for the existence of some types at build time instead of runtime
known.missing.types.enabled=true

# Precomputes property sources at build time
sealed.property.source.enabled=true

# The list of service types to be scanned (comma separated)
service.types=io.micronaut.context.env.PropertySourceLoader,io.micronaut.inject.BeanConfiguration,io.micronaut.inject.BeanDefinitionReference,io.micronaut.http.HttpRequestFactory,io.micronaut.http.HttpResponseFactory,io.micronaut.core.beans.BeanIntrospectionReference,io.micronaut.core.convert.TypeConverterRegistrar,io.micronaut.context.env.PropertyExpressionResolver

# A list of types that the AOT analyzer needs to check for existence (comma separated)
known.missing.types.list=io.reactivex.Observable,reactor.core.publisher.Flux,kotlinx.coroutines.flow.Flow,io.reactivex.rxjava3.core.Flowable,io.reactivex.rxjava3.core.Observable,io.reactivex.Single,reactor.core.publisher.Mono,io.reactivex.Maybe,io.reactivex.rxjava3.core.Single,io.reactivex.rxjava3.core.Maybe,io.reactivex.Completable,io.reactivex.rxjava3.core.Completable,io.methvin.watchservice.MacOSXListeningWatchService,io.micronaut.core.async.publisher.CompletableFuturePublisher,io.micronaut.core.async.publisher.Publishers.JustPublisher,io.micronaut.core.async.subscriber.Completable

6 changes: 6 additions & 0 deletions graalpy/graalpy-micronaut-pygal-charts/micronaut-cli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
applicationType: default
defaultPackage: com.example
testFramework: junit
sourceLanguage: java
buildTool: maven
features: [app-name, http-client-test, java, java-application, junit, logback, maven, maven-enforcer-plugin, micronaut-aot, micronaut-http-validation, netty-server, properties, readme, serialization-jackson, shade, static-resources]
Loading

0 comments on commit 433c403

Please sign in to comment.