I have no idea why you would actually do this, but the fact that you can is super cool.
1st we download Jython , the Java implementation of Python
2nd we drop the jython.jar file in the same dir as the below code.
3rd we use the JavaLoader.cfc to load up the jar file.
4th watch Python run in ColdFusion!
This is a CF translation of the Java embedding tutorial at http://www.jython.org/docs/embedding.html
<cfscript>
//set he path
paths = ArrayNew(1);
paths[1] = expandPath("jython.jar");
//create the loader
loader = createObject("component", "JavaLoader").init(paths);
//create the Python Iterpreter
interp = loader.create("org.python.util.PythonInterpreter").init();
//set standard output and errors out to the screen
interp.setOut(getPageContext().getResponse().getOutputStream());
interp.setErr(getPageContext().getResponse().getOutputStream());
//top line
writeoutput("Hello, brave new world <br/>");
//do some python
interp.exec("import sys");
interp.exec("print sys");
pyInt = loader.create("org.python.core.PyInteger").init(42);
interp.set("a", pyInt);
//this now prints out to the screen!
interp.exec("print a");
interp.exec("x = 2+2");
//get some values back out of python
x = interp.get("x");
writeoutput("x: " & x & "<br/>");
writeoutput("Goodbye, cruel world <br/>");
</cfscript>
Okay, so I'm just hyped up about using the JavaLoader to do some weird and neat stuff – but hey, it's really cool, allright!
Comments
Heh, I did the same thing back then with CF6.0. I wrapped Jython in <cf_python>python code here</cf_python> custom tag. Didn’t find any real practical uses for it though 😐
This is a great example of the fact that you can use more than one language through the JVM. I know SUN is adding support to the next version of Java to allow developers to use different languages, and run the code as Java bytecode. We will probably see more languages that we will be able to run through the JVM once the next version of Java ships.
Following this example, and Erki’s comment, the use is if you already have Python scripts that need to perform action. Can Erki elaborate on how he wrote the <cf_python> tag….I’d like a nice clean way to call a python module, select a function in the module to run, pass in variables, and get the response (in my case the function return is usually a tuple, but I could write another wrapper if needed). Your <cf_python> tag approach might be what I’m looking for, but I’m struggling with how to do it…
Maybe there isn’t any obvious practice for this yet. However, it’s a very innovative idea!
Hi Mark,
Very old post, but I’ve found a usecase, at last!
We use Python 4 to read a pre-designed and pre-built Word document which also contains many placeholders for where data from the database should be inserted. Python then gets a JSON-file from the CFML-code with all the data (matchd to each placeholder) and passes over the JSON-file replcaing each placeholder with the appropriate data. Then Python return the Word-doc as a data-object which we then serve to the browser as a cfcontent type attachment. Voila!
A lot easier than using Jython or any other librarry out there that can eprform tasks on Office-documents. Spreadsheets etc are a breeze with Lucee-Spreadsheets library, but Word docs were a hassle with libs available. Learning some Python basics and now we have this in our toolbelt as well.
If anyone is curious about our code, please feel free to get in touch!