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

Ngoc Huynh

1. 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.

2. 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.

3. 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.

4. 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.

Share the news now

Source : http://www.infoworld.com/