-
Is anyone interested in seeing Jbang support Jython? An extension for Python to use JBang already exists, but I'm proposing the inverse of the functionality provided by that project. Recently I wrote a Jython (Python) script to process a JSON file that contains amongst other data structures several persistent images of HdrHistograms. The output generated by the report.py program is an HTML report. The report.py file is executed with Java and Jython as follows:
If JBang supported Jython, the Jbang augmented report.py file would look something like this:
The report.py file can then be executed as:
|
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 10 replies
-
problem with using jython is that jython only supports python 2 afaik ? are you aware of the graalpy integration? See https://github.com/oracle/graalpython/blob/master/graalpython/graalpy-jbang/examples/hello.java I've always wondered if something as you suggest above would be feasible via graalpy as graalpy does support later more recent python versions. |
Beta Was this translation helpful? Give feedback.
-
Yes, Jython 2.7.4 is the latest stable release, but it contains at least some Python 3 functionality like the print() function and so forth. But it is not Python 3.x A few years ago, I did look at graalpython as a potential Jython replacement, but it still had many rough edges. It should have improved a lot by now. Worth having another look. It seems that adding Jython support would be relatively simple, and very similar to how Jshell is supported. |
Beta Was this translation helpful? Give feedback.
-
Documentation discussing support for Jython in the GraalVM - https://www.graalvm.org/jdk17/reference-manual/python/Jython/ |
Beta Was this translation helpful? Give feedback.
-
It is a bit cumbersome to use graalpy (for me), but I have found a workaround solution using jpm (https://github.com/codejive/java-jpm) which incidently can be installed via jbang. |
Beta Was this translation helpful? Give feedback.
-
#!/usr/bin/env python-jvm
# restclient_jython.py
#
# Run: jbang run python-jvm@wfouche restclient_jython.py
#
# /// jbang
# requires-jython = "==2.7.4"
# requires-java = ">=21"
# debug = false
# dependencies = [
# "org.springframework.boot:spring-boot-starter-web:3.4.4"
# ]
# [java]
# runtime-options = "-server -Xms2g -Xmx2g -XX:+UseZGC -XX:+ZGenerational"
# ///
import java.lang.String as String
import org.springframework.web.client.RestClient as RestClient
def restApiCall(uri, id):
restClient = RestClient.create()
rsp = restClient.get().uri(uri, id).retrieve().body(String)
print(rsp)
def main():
restApiCall("https://jsonplaceholder.typicode.com/todos/{id}", 1)
main() Output:
|
Beta Was this translation helpful? Give feedback.
-
Running $ jbang run python-jvm@wfouche
python-jvm/1/2025-04-24T17:58:24
Jython 2.7.4 (tags/v2.7.4:3f256f4a7, Aug 18 2024, 10:30:53)
[OpenJDK 64-Bit Server VM (Eclipse Adoptium)] on java21.0.6
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> print(1+1)
2
>>>
>>> |
Beta Was this translation helpful? Give feedback.
python-jvm@wfouche
is a JBang app that streamlines the process of executing Jython programs within a JVM environment.