pscript Python to JavaScript Compiler

0.8.1 · active · verified Fri Apr 17

pscript is a Python-to-JavaScript compiler that allows developers to write Python code that runs directly in a JavaScript environment, such as a web browser or Node.js. It supports converting Python functions and basic data structures into their JavaScript equivalents, enabling seamless integration of Python logic into web applications. The current version is 0.8.1, with releases occurring periodically, often tied to Python version support updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a Python function decorated with `@pyscript` to automatically generate its JavaScript equivalent. The generated JavaScript source code can then be embedded and executed in a JavaScript runtime environment.

from pscript import pyscript

@pyscript
def hello_js(name):
    # This Python code will be transpiled to JavaScript
    print(f'Hello, {name} from JavaScript!')
    return f'Greetings, {name}'

# The generated JavaScript source code
js_code = hello_js.source
print(js_code)

# Example of using the generated function in a browser context (conceptual)
# You would typically inject js_code into an HTML <script> tag
# For example, in browser console:
# hello_js('World'); // Should print 'Hello, World from JavaScript!' to console

view raw JSON →