Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use FunctionalInterface for Python function. #12

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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