Python to Javascript translator
Usage
import py2js
@py2js.js
def main():
# code here
Options: py2js.js
func: t.Optional[t.Callable]
as_function: t.Optional[bool]
python_compatible: t.Optional[bool]
Examples
python
import py2js
import typing
this = typing.Self
@py2js.js
def translated_normal():
'generator'
def generator_func():
yield 1
yield 2
yield 3
yield from [9, 8, 7]
return 0
for item in generator_func():
console.log(item)
'for-else'
for i in [0, 1, 2, 3]:
if i > 2:
break
else:
console.log('else')
'f-string'
a = 4
console.log(f'{a} fstring')
'class'
class Main:
a: int
def constructor():
this.a = 1
def func():
console.log(this.a)
Main().func()
'try catch else finally'
try:
raise SyntaxError('syntax error')
except SyntaxError as e:
console.log('syntax error raised')
except:
console.log('excepted')
else:
console.log('else')
try:
if Boolean(1):
raise SyntaxError('syntax error')
except:
pass
finally:
console.log('finally')
'while'
i = 10
while i > 0:
i -= 1
'comparator'
i = 5
if 0 < i < 9:
console.log('true')
@py2js.js(python_compatible=True)
def translated_compatible():
'class with self args'
class Main:
def __init__(self, value):
self.a = value
def func(self):
console.log(self.a)
Main('hello, world!').func()
with open('translated_normal.js', 'w') as f:
f.write(translated_normal)
with open('translated_compatible.js', 'w') as f:
f.write(translated_compatible)
translated_normal.js
`generator`;
let generator_func = function*() {
yield 1;
yield 2;
yield 3;
yield*[9, 8, 7];
return 0
};
for (let item of generator_func()) {
console.log(item)
};
`for-else`;
__else: {
for (let i of [0, 1, 2, 3]) {
if (i > 2) {
break __else
}
}
console.log(`else`)
};
`f-string`;
let a = 4;
console.log(`${a} fstring`);
`class`;
let Main = class {
constructor() {
this.a = 1
};
func() {
console.log(this.a)
}
a
}
Main = new Proxy(Main, {
apply: (clazz, thisValue, args) => new clazz(...args)
});;
Main().func();
`try catch else finally`;
__else: {
try {
throw SyntaxError(`syntax error`)
} catch (__err) {
if (__err instanceof SyntaxError) {
e = __err;
console.log(`syntax error raised`);
break __else
} {
console.log(`excepted`);
break __else
}
}
console.log(`else`)
};
try {
if (Boolean(1)) {
throw SyntaxError(`syntax error`)
}
} catch (__err) {
{
/* pass */ }
} finally {
console.log(`finally`)
};
`while`;
let i = 10;
while (i > 0) {
i -= 1
};
`comparator`;
i = 5;
if (0 < i < 9) {
console.log(`true`)
}
translated_compatible.js
`class with self args`;
let Main = class {
constructor(...args) {
if ('__init__' in this) this.__init__(...args);
return new Proxy(this, {
apply: (target, self, args) => target.__call__(...args),
get: (target, key) => target.__getitem__(key)
})
}
__init__ = (...__args) => {
((self, value) => {
self.a = value
})(this, ...__args)
};
func = (...__args) => {
((self) => {
console.log(self.a)
})(this, ...__args)
}
}
Main = new Proxy(Main, {
apply: (clazz, thisValue, args) => new clazz(...args)
});;
Main(`hello, world!`).func()
todo
match statement
Metadata
Release files for py2js 1.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| py2js-1.2.1.tar.gz | 11.0 kB | Without provenance | Details for py2js-1.2.1.tar.gz |
Release files / py2js-1.2.1.tar.gz
| Download URL | py2js-1.2.1.tar.gz |
|---|---|
| Size | 11.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0d99dd5a39c80e8e375d6af09fb3a76884f9403a692302e0ed0b946c29ee5df2
|
|
BLAKE2b-256 checksum How to use checksums |
2443a649f92edb3954884f8f728ad5189635ab3c3c3431cef6f9966049d15dde
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Security for py2js 1.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.