Demo: Running Python using the new java integration in Lucee 6.2

Here’s a quick example of how to run Python using Graalvm with Lucee 6.2

Lucee will dynamically download the java libraries defined in the javaSettings (when required), it’s that simple!

The jars are stored in the lucee-server\context\mvn\ directory.

We also added support for the more concise Graal style syntax
"org.graalvm.polyglot:polyglot:24.1.2"

Note the use of static syntax :: when calling java functions directly

<cfscript>
	python = new component 
		javaSettings = '{ maven: [
			"org.graalvm.polyglot:polyglot:24.1.2",
			"org.graalvm.polyglot:python:24.1.2",
			"org.graalvm.python:python-language:24.1.2"
		]}'
	{
		import org.graalvm.polyglot.Source;
		import org.graalvm.polyglot.Context;
		import java.io.File;

		function executePython(src) {
			var c = Context::newBuilder(javaCast("String[]",["python"]))
				.allowAllAccess(true)
				.allowExperimentalOptions(true)
				.build();
			
			//echo(c.eval("python", "print('Hello from GraalPy!')"));
			script = Source::create("python", src);
			c.eval(script);
			result = c.getPolyglotBindings().getMember("hello").execute(["hello lucee"]); 
			return result.asString();
		}
	};
</cfscript>

<cfsavecontent variable="python_src">
import polyglot
@polyglot.export_value
def hello(externalInput):
	return 'Got output from graal python: ' + externalInput
</cfsavecontent>
<cfscript>
	dump(python.executePython(python_src));
</cfscript>

image

Here’s are some posts relating to the new functionality being used in this example

5 Likes