Easily run javascript expressions using python.
Project description
pyrunjs
使用python简单地调用js,获取js表达式返回值
note: 系统需要安装node.js,node命令可用,尝试用node -v查询node版本:
$ node -v
v18.20.5
安装
pdm
pdm add pyrunjs
pip
pip install pyrunjs
使用示例
import pyrunjs
js_script = """function HelloWorld() { return 'Hello World!'; } """
result = pyrunjs.run_js(js_script, "HelloWorld()")
print(result) # output: Hello World!
import pyrunjs
result = pyrunjs.run_js('', "1 + 2")
print(result) # output: 3
import pyrunjs
js_script = """
function add(a, b) {
return new Promise((resolve, reject) => {
let sum = a + b;
resolve(sum);
}).then(sum => {
return sum * sum;
});
}
"""
result = pyrunjs.run_js(js_script, "add(1, 2)")
print(result) # output: 9
版本更新概要信息
- 0.1.2
run_js函数可用 - 0.1.3
- 新增
call_js函数,可自动编写调用function的表达式。 - 解决不同系统默认编码不同导致的乱码问题。(例:windows系统默认编码为GBK时,现在可能正常返回结果了)
- 新增
- 0.1.4 添加对异步函数Promise的支持。run_js表达式返回异步函数时,可以获取resolve中的结果了。
- 0.1.5 添加nox测试多版本的python, python支持的版本可降低到 python3.8
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pyrunjs-0.1.5.tar.gz
(4.1 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
File details
Details for the file pyrunjs-0.1.5.tar.gz.
File metadata
- Download URL: pyrunjs-0.1.5.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.21.0 CPython/3.12.7 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ddfef82409bbd6b32a7516946d2c70ac5b55722e18eca8586030c8b2de3e443c
|
|
| MD5 |
060483726ba751f24da9b07f87e1e03f
|
|
| BLAKE2b-256 |
cf27085516b15f3a524bb069623a7236a949a01c7e0957cad4407c56ac5aa8ad
|
File details
Details for the file pyrunjs-0.1.5-py3-none-any.whl.
File metadata
- Download URL: pyrunjs-0.1.5-py3-none-any.whl
- Upload date:
- Size: 3.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.21.0 CPython/3.12.7 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7095e16d73d418ec67a164e516a69fe53a2ddaa452b67b776239c7bfe268def9
|
|
| MD5 |
557547d1faadef881202b2f8531edc81
|
|
| BLAKE2b-256 |
3b4777e2b4147ef7e30caf1e8eabddf49b9ffc6e7c70e0a073f1107a07f36d1a
|