Skip to content

Commit

Permalink
Use FunctionalInterface for Python function.
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Nov 15, 2024
1 parent 718c0c8 commit 691ac11
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 12 deletions.
2 changes: 1 addition & 1 deletion graalpy/graalpy-micronaut-pygal-charts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ To start the demo, simply run:
./mvnw package mn:run
```

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

| URL | Service |
|:------------------------------|:------------------------------|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@
package com.example;

import jakarta.inject.Singleton;
import org.graalvm.polyglot.Value;

import java.util.List;
import java.util.stream.IntStream;

@Singleton
public class PyGalServiceMixed implements PyGalService {
private final Value pythonFunctionXY;
private final RenderXYFunction renderXYFunction;

PyGalServiceMixed(GraalPyContext graalPyContext) {
pythonFunctionXY = graalPyContext.eval(
renderXYFunction = graalPyContext.eval(
// language=python
"""
import pygal
Expand All @@ -29,12 +28,17 @@ def render_xy(title, label_datapoint_entries):
xy_chart.add(entry.label(), entry.dataPoints())
return xy_chart.render().decode()
render_xy""");
render_xy""").as(RenderXYFunction.class);
}

public record Entry(String label, double[][] dataPoints) {
}

@FunctionalInterface
public interface RenderXYFunction {
String apply(String title, List<Entry> labelDatapointEntries);
}

@Override
public String renderXYChart() {
String title = "XY Cosinus";
Expand All @@ -46,6 +50,6 @@ public String renderXYChart() {
new Entry("y = 1", new double[][]{{-5, 1}, {5, 1}}),
new Entry("y = -1", new double[][]{{-5, -1}, {5, -1}})
);
return pythonFunctionXY.execute(title, labelDatapointEntries).asString();
return renderXYFunction.apply(title, labelDatapointEntries);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
{
"type": "com.example.PyGalServiceMixed$Entry"
},
{
"type": "com.example.PyGalServiceMixed$RenderXYFunction"
},
{
"type": "com.example.PyGalServicePureJava$BytesIO"
},
Expand All @@ -12,6 +15,13 @@
{
"type": "com.example.PyGalServicePureJava$XY"
},
{
"type": {
"proxy": [
"com.example.PyGalServiceMixed$RenderXYFunction"
]
}
},
{
"type": {
"proxy": [
Expand Down
2 changes: 1 addition & 1 deletion graalpy/graalpy-spring-boot-pygal-charts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ To start the demo, simply run:
./mvnw package spring-boot:run
```

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

| URL | Service |
|:------------------------------|:------------------------------|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
package com.example.demo;

import com.example.demo.GraalPyContextConfiguration.GraalPyContext;
import org.graalvm.polyglot.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.IntStream;

@Service
public class PyGalServiceMixed implements PyGalService {
private final Value pythonFunctionXY;
private final RenderXYFunction renderXYFunction;

PyGalServiceMixed(GraalPyContext graalPyContext) {
pythonFunctionXY = graalPyContext.eval(
renderXYFunction = graalPyContext.eval(
// language=python
"""
import pygal
Expand All @@ -30,12 +29,17 @@ def render_xy(title, label_datapoint_entries):
xy_chart.add(entry.label(), entry.dataPoints())
return xy_chart.render().decode()
render_xy""");
render_xy""").as(RenderXYFunction.class);
}

public record Entry(String label, double[][] dataPoints) {
}

@FunctionalInterface
public interface RenderXYFunction {
String apply(String title, List<Entry> labelDatapointEntries);
}

@Override
public String renderXYChart() {
String title = "XY Cosinus";
Expand All @@ -47,6 +51,6 @@ public String renderXYChart() {
new Entry("y = 1", new double[][]{{-5, 1}, {5, 1}}),
new Entry("y = -1", new double[][]{{-5, -1}, {5, -1}})
);
return pythonFunctionXY.execute(title, labelDatapointEntries).asString();
return renderXYFunction.apply(title, labelDatapointEntries);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
{
"type": "com.example.demo.PyGalServiceMixed$Entry"
},
{
"type": "com.example.demo.PyGalServiceMixed$RenderXYFunction"
},
{
"type": "com.example.demo.PyGalServicePureJava$BytesIO"
},
Expand All @@ -12,6 +15,13 @@
{
"type": "com.example.demo.PyGalServicePureJava$XY"
},
{
"type": {
"proxy": [
"com.example.demo.PyGalServiceMixed$RenderXYFunction"
]
}
},
{
"type": {
"proxy": [
Expand Down

0 comments on commit 691ac11

Please sign in to comment.