Jython - Python with Java

Feature image: 

One of my goals in exploring the use of Python is the ability to create calculators and examples easily with the Python language, then host the finished product on the web here on RFMentor or for use in our online classes.
The problem of course is that Python is not built in to browsers as a scripting language, so one would have to deploy some form of "web application" on the server, where the Python scripts could be interpreted natively. Another alternative is to use a tool called "Jython," which is a Python interpreter that is written and implemented in Java. Since it is written in Java, it could be packaged as an applet that is then embedded in a web page. I've tried this and it works reasonably well. There's even a good plotting capability using a native Java plotting library.
The picture attached to this blog shows an applet that I made that can run a Jython script, specifically one that is suited to plotting formulas. Jython has most of the same libraries as the desktop version, and also access to Java objects. At the moment the screen capture was taken, the formula in the text box does not match the formula that is plotted in the graph, but in principle this applet will plot whatever you type into the formula box (assuming there's no reason to crash first). Jython does not have access to all of the libraries that come with the desktop Python environment because some of those libraries are written in C, and they haven't been rewritten in Java. On the other hand, Jython has access to Java and a Jython script can readily communicate back and forth to the applet that has been coded in Java. It works pretty well.
The problem in recent times is that the Java plug in that runs applets has been vulnerable from a security standpoint. The plug-in had many flaws that allowed programs to execute code that they were not supposed to be authorized to run. As a result, many or most people have disabled Java in their browsers out of security concerns, since hackers have widely exploited the vulnerabilities. Furthermore, the Chrome browser from Google does not even support the Java plug in at all, nor do most mobile devices. These factors make embedding Java applets in web pages less appealing looking forward, unfortunately.
For this reason, I've been also looking at solutions that use Javascript to interpret a Python program in some fashion, either by translating the file to Javascript and then hosting it, or implementing a Python interpreter in Javascript directly.
Share your opinion on using Java applets in the comments below - should I bother to create new Java applets or just focus on Javascript/HTML5?

Category: