Groovy (Python-to-JavaScript Transpiler)
Groovy is a Python-to-JavaScript transpiler that converts Python functions to their JavaScript equivalents. It is primarily used within the Gradio library, allowing developers to write Python functions that execute efficiently as client-side JavaScript. The library prioritizes clear error reporting for unsupported Python code over full Python feature coverage. The current version is 0.1.2, with development appearing active due to its integration with Gradio.
Warnings
- gotcha This 'groovy' library (version 0.1.2) is a Python-to-JavaScript transpiler. Be aware of potential name collisions with other Python packages also named 'groovy' on PyPI that serve different purposes (e.g., a workflow automation tool or a Groovy language parser). Ensure you are installing and using the correct library for your needs.
- gotcha Groovy does not provide full Python feature coverage; it supports only a subset of the Python standard library and some Gradio-specific classes. Attempting to transpile unsupported Python syntax will result in a `TranspilationError`.
- gotcha While Groovy aims to guarantee identical return values for transpiled functions, the internal JavaScript implementation may differ slightly from the Python version (e.g., Python tuples might be converted to JavaScript arrays). This can lead to unexpected behavior if introspection or strict type checking of the transpiled JavaScript's internal structure is performed.
Install
-
pip install groovy
Imports
- transpile
from groovy import transpile
Quickstart
from groovy import transpile
def sum_range(n: int) -> int:
total = 0
for i in range(n):
total = total + i
return total
js_code = transpile(sum_range)
print(js_code)
# Expected output (may vary slightly in formatting):
# function sum_range(n) {
# let total = 0;
# for (let i of Array.from({length: n}, (_, i) => i)) {
# total = (total + i);
# }
# return total;
# }