4 tools to convert Python to JavaScript (and back again)

Love Python? JavaScript, not so much? Here are four tools that turn the former to the latter for use in Web applications

4 tools to convert Python to JavaScript (and back again)
Credit: Thinkstock

Python or JavaScript? While we're still arguing over which has the upper hand or the brighter future, little doubt exists as to which runs the Web's front end. It's JavaScript in the browser or nothing.

Well, maybe not nothing. JavaScript is quite welcoming as a target language for others to be converted into (see: TypeScriptEmscripten, CorCheerp). And Python's huge following and wealth of existing libraries make it a great candidate to be converted, or transpiled, into JavaScript.

Here are four current projects for making Python useful in the JavaScript world; one stands out by being able to convert in both directions.

Transcrypt

A newly minted Python-to-JS transpiler, Transcrypt makes intriguing promises about the quality of the code it generates.  For one, it attempts to preserve the structure of the original Python code as closely as possible, including the likes of multiple inheritances and lambdas. The source Python code can also make calls to objects in JavaScript's namespace as-is. If you try to access document.getElementById in Python, the converted code will use the actual document.getElementById in JavaScript.

According to the documentation, Transcrypt accomplishes these tasks with CPython's Abstract Syntax Tree module, which allows programmatic access to the way Python parses its own code. Though the project is still in alpha, it'll be fascinating to see how it shapes up.

Jiphy

Jiphy -- meaning "JavaScript in, Python out" -- converts in both directions. Plus, code from both languages can be intermixed before being converted to either target language.

The biggest drawback to Jiphy right now is that it supports only a subset of Python's features. Classes or default arguments aren't available yet, although decorators and exceptions are. Much of this is because Jiphy insists on establishing as close to a line-to-line relationship as possible between the source and target code, but its developers are eyeing the new features in ES6 for more advanced Python feature support.

Brython

Someday, when WebAssembly becomes a reality, it may be possible to develop for the Web in any language we choose. Brython's philosophy, at least as far as Python 3 is concerned: Why wait?

Brython implements a version of Python 3 for client-side Web programming via a JavaScript library that emulates all the keywords and most of the built-ins for Python 3. Scripts written in Python can be included directly in a Web page, and Brython supplies a high-level Python module interface (browser) for performing the work with the DOM and the browser normally done directly in JavaScript.

However, Brython also maintains the restrictions imposed on JavaScript in the browser -- for example, there's no support for dealing with the local filesystem.

RapydScript

RapydScript promises "Pythonic JavaScript that doesn't suck." The project is conceptually similar to CoffeeScript: Take in code written in a flavor of Python, generate JavaScript, and leverage the best of both words. In Python's case, it's the cleanliness of syntax; in JavaScript's case, it's anonymous functions, manipulation of the DOM, and being able to use existing JavaScript libraries like jQuery or the Node.js core.

To comment on this article and other InfoWorld content, visit InfoWorld's LinkedIn page, Facebook page and Twitter stream.
From CIO: 8 Free Online Courses to Grow Your Tech Skills
Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.