Run JavaScript code from Python.
Project description
ExeJS
Run JavaScript code from Python.
Supported Runtime
| ID | Runtime | Browser Engine | Team |
|---|---|---|---|
| 1 | Node | Chrome | |
| 2 | JavaScriptCore | Safari | Apple |
| 3 | SpiderMonkey | Firefox | Mozilla |
| 4 | JScript | IE | Microsoft |
| 5 | PhantomJS | Webkit* | Apple |
| 6 | SlimerJS | Gecko* | Mozilla |
| 7 | Nashorn | Java* | Oracle |
Installation
# PYPI
pip install --upgrade exejs
# Conda
conda install conda-forge::exejs
# Source
git clone https://github.com/UlionTse/exejs.git
cd exejs
pip install .
Getting Started
import asyncio
import exejs
# evaluate (one-shot expression):
print(exejs.evaluate("'red yellow blue'.split(' ')"))
# ['red', 'yellow', 'blue']
# execute (one-shot statements; use `return` to send a value back):
print(exejs.execute('var x = 40 + 2; return x;'))
# 42
# compile + call (reuse a JS context across multiple calls):
ctx = exejs.compile('function add(x, y) { return x + y; }')
print(ctx.call('add', 1, 2)) # 3
# call an object method (key may be a property path):
ctx = exejs.compile('var calc = { mul: function(a, b) { return a * b; } };')
print(ctx.call('calc.mul', 3, 4)) # 12
# call with structured arguments (auto JSON-serialized):
ctx = exejs.compile('function greet(u) { return "hi " + u.name + ", age " + u.age; }')
print(ctx.call('greet', {'name': 'Tom', 'age': 18})) # hi Tom, age 18
# async evaluate:
print(asyncio.run(exejs.evaluate_async("'red yellow blue'.split(' ')")))
# timeout (kill a runaway script instead of hanging forever):
try:
exejs.execute('while (true) {}', timeout=2.0)
except exejs.ExejsTimeoutError as e:
print('killed:', str(e))
Reference
Why
- We need to run JavaScript from Python, but PyExecJS has been EOL since 2018. Issue#1
- Package builds that rely on PyExecJS fail or get cancelled. Issue#2
- PyExecJS writes compiled code to a temp file by default, which triggers antivirus alerts and blocks execution. Issue#3
Improvement and Change
- Stop writing compiled code to a temp file (except
JScript); compile and run just-in-time via stdin. - Drop Python 2 support.
- Add an async API.
Notable Usage
ExeJS is a core dependency of Translators, where it handles the JavaScript execution layer that translation engines rely on.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
exejs-1.0.0.tar.gz
(18.8 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
exejs-1.0.0-py3-none-any.whl
(13.4 kB
view details)
File details
Details for the file exejs-1.0.0.tar.gz.
File metadata
- Download URL: exejs-1.0.0.tar.gz
- Upload date:
- Size: 18.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d01f5e791d26d27630b61b68ed006193fae9629d3cccba0de24fc05cd24f95bc
|
|
| MD5 |
e8b31f723e195a76290bd614a8ccf220
|
|
| BLAKE2b-256 |
f4e6097937f44358f4fa822fcde132982268582ae0b377e653220a5924334f16
|
File details
Details for the file exejs-1.0.0-py3-none-any.whl.
File metadata
- Download URL: exejs-1.0.0-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e46b76d6fdc4e3a6c8a2d72924b921c805a900f910cbc48f88ad6b5f973c314
|
|
| MD5 |
28685dca33324e11d39b6ad2561e9ba4
|
|
| BLAKE2b-256 |
21d72e26c3f523a4e60c05f6a91607cbbce6702f74730f3161de411258324f50
|