Js2Py: JavaScript to Python Translator & Interpreter

0.74 · active · verified Sun Apr 12

Js2Py is a pure Python library that acts as both a JavaScript to Python translator and a JavaScript interpreter. It allows Python developers to execute JavaScript code directly within their Python applications, or convert it to equivalent Python code. The current version is 0.74, and it sees active, though not strictly scheduled, development.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to evaluate a JavaScript code string and interact with defined functions, as well as directly evaluate simple expressions. `eval_js` returns a `PyJs` object or a Python equivalent of the JS return value.

import js2py

# Define a JavaScript function
js_code = """
function greet(name) {
    return 'Hello, ' + name + '!';
}
"""

# Evaluate the JavaScript code and get a Python callable object
js_greet = js2py.eval_js(js_code)

# Call the JavaScript function from Python
result_func = js_greet('World')
print(f"Function result: {result_func}")

# You can also evaluate a simple expression directly
direct_result = js2py.eval_js('10 * 5')
print(f"Direct expression result: {direct_result}")

view raw JSON →