Close

How to convert Python to JavaScript (and back again)

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

Well, maybe not nothing. JavaScript is a favorite target language for “transpilers” that convert one programming language into another (see: TypeScriptEmscriptenCorCheerp). And Python’s huge following and wealth of existing libraries make it a great candidate to be converted, i.e. 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

If you hear the name Transcrypt and think TypeScript, you’re not far off the mark. Transcrypt follows the same basic idea – it transpiles Python to JavaScript. It also tries to preserve, wherever possible, the structure and the idioms of the original Python code, including constructs like lambdas and multiple inheritance across classes.

What’s more, source maps can be generated for the transpiled code that point back to the original Python, so developers can debug using that code instead of the generated 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.

One of Transcrypt’s biggest advantages is automatic access to JavaScript’s Document Object Model (DOM). If you try to access document.getElementById in Python, for instance, the converted code will use the actual document.getElementById in JavaScript.

An associated project, and one still heavily under wraps, is Numscrypt, which ports the NumPy math-and-stats library to JavaScript. So far Numscrypt provides only a subset of NumPy’s features, though these features (e.g., matrix math) are among the most commonly used.

Jiphy

The Jiphy name is an abbreviation of “JavaScript in, Python out.” In other words, Jiphy converts in both directions between the two languages. Plus, code from both languages can be intermixed before being converted to either target language.

Before you dive in and start converting all of OpenStack to JavaScript, take heed: Jiphy is not about full-blown codebase conversion. Rather, its function is, as the README puts it, “to reduce the context switching necessary for a Python developer to write JavaScript code and vice versa.”

The biggest drawback to Jiphy is that it supports only a subset of Python’s features. Neither classes nor default arguments are available yet, although decorators and exceptions are supported. 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 have eyed the new features in ES6 for more advanced Python feature support.

Note, however, that the project hasn’t been updated since early 2016. Jiphy should be considered strictly experimental until work on it resumes.

Brython

Someday, when WebAssembly becomes a reality, it may be possible to develop for the web in any language we choose. The philosophy behind Brython, at least as far as Python 3 is concerned, is why wait?

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

Plenty of live code examples and a gallery of mini-applicationsdemonstrate how it all works. It’s even possible to use Brython to write a native Android app in Python. Plans are in the works to support Python’s async functionality and to eventually use WebAssembly as a compilation target.

Brython does not escape the restrictions imposed on JavaScript in the browser. For example, there is no support for dealing with the local filesystem. There is, however, support for using HTML5 local storage, if all you need is some way to persist data on a per-application basis.

RapydScript

RapydScript promises “Pythonic JavaScript that doesn’t suck.” The project is similar to CoffeeScript in that it ingests code written in an alternative language – in this case, a flavor of Python – and generates JavaScript that can run anywhere as-is.

Thus RapydScript provides the best of both worlds, bringing Python’s clean syntax to JavaScript capabilities like anonymous functions, DOM manipulation, and the ability to leverage JavaScript libraries like jQuery or the Node.js core. That’s right—you can use Rapydscript-generated code to drive webpages or Node apps.

Another convenient feature of RapydScrypt: It offers both Python and JavaScript nomenclatures for certain operations when possible. For instance, the $ special symbol used by jQuery works as-is in RapydScript, and arrays can support both the .push (JavaScript) and .append (Python) methods.