一个依赖node.js来执行js代码的库
环境
注意 需要安装好node.js, 并添加好环境变量!!!
安装
pip install pyevaljs3
快速开始
import pyevaljs3
js_code = "function f(args) {console.log(args); return 'ok';}; return f('args')"
result = pyevaljs3.eval_(js_code)
print(result) # 'ok'
js_code = "function f(arg1, arg2) {console.log(arg1, arg2); return arg1 + arg2;}"
ctx = pyevaljs3.compile_(js_code)
result = ctx.call('f', 'a', 'b')
print(result) # 'ab'
#另一钟传参方式
result = ctx.call('f', arg_list=['a', 'b'])
print(result) # 'ab'
# 调用js异步函数
js_code = "async function f(arg1, arg2) {console.log(arg1, arg2); return arg1 + arg2;}"
ctx = pyevaljs3.compile_(js_code)
async def main():
result = await ctx.async_call('f', "a", "b", async_js_func=True)
return result
import asyncio
result = asyncio.run(main())
print(result) # 'ab'
API参考
pyevaljs3.JSEval
-
def compile(self, source: str = None, suffix: str = None, cwd: str = None):
编译javascript源代码
:param source: 源代码字符串或要读取的文件路径
:param suffix: js脚本文件名后缀(指定以什么模式执行), 默认是".js", 可选的值还有".cjs", ".mjs"等
:param cwd: 工作目录路径
:return: Context -
def eval(self, code: str = None, ignore_output=False, cwd: str = None):
执行javascript代码, 返回其结果 -
async def async_eval(self, code: str = None, ignore_output=False, cwd: str = None):
执行javascript代码, 返回其结果(异步版本的eval)
pyevaljs3.Context
-
def call(self, func, *args, arg_list: List = None):
调用指定的函数, 返回其结果(若指定了arg_list, 优先使用它作为函数参数)
:param func: 函数名
:param args: 函数的参数列表
:param arg_list: 函数的参数列表 -
async def async_call(self, func, *args, arg_list: List = None, async_js_func: bool = False):
调用指定的函数, 返回其结果(若指定了arg_list, 优先使用它作为函数参数), 参数解释同call
:param async_js_func: 调用的js函数是否是异步js函数
pyevaljs3的模块级函数
-
def compile_(source: str = None, mode: str = None, cwd: str = None) -> Context:
编译js源代码
:param source: 源代码字符串或要读取的文件路径
:param mode: 执行模式, 默认以.js的行为去执行
:param cwd: 工作目录路径
:return: Context -
def eval_(code: str = None, ignore_output=False, cwd: str = None):
执行javascript代码, 返回其结果
:param code: js代码
:param ignore_output: 是否忽略执行过程中的输出, 若为True则仅返回其结果, 默认不忽略(False)
:param cwd: 工作目录路径 -
async def async_eval(code: str = None, ignore_output=False, cwd: str = None):
执行javascript代码, 返回其结果, 参数解释同eval_, 异步版本的eval_
使用自定义版本的node
- 通过设置坏境变量来使用自定义版本的node, 只需设置NODE_PATH、NODE坏境变量即可
import os
# 优先级最高
os.environ['NODE_PATH'] = '/path/to/node.exe'
# 或者
# 优先级其次
os.environ['NODE'] = '/path/to/node.exe'
Metadata
Release files for pyevaljs3 0.2.4
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 |
|---|---|---|---|---|
| pyevaljs3-0.2.4.tar.gz | 10.1 kB | Without provenance | Details for pyevaljs3-0.2.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyevaljs3-0.2.4-py3-none-any.whl | Python 3 | none | any | Details for pyevaljs3-0.2.4-py3-none-any.whl |
Total release size: 19.5 kB
Release files / pyevaljs3-0.2.4.tar.gz
| Download URL | pyevaljs3-0.2.4.tar.gz |
|---|---|
| Size | 10.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
83f131c9c9aceb030628d36a358a17549be06f495722d0f996588588a7cd5c33
|
|
BLAKE2b-256 checksum How to use checksums |
49aff8d16be1b778dbac7fc959e7d096a753c74ab69b7ad3204d2cc5bf288a7f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.7
|
Release files / pyevaljs3-0.2.4-py3-none-any.whl
| Download URL | pyevaljs3-0.2.4-py3-none-any.whl |
|---|---|
| Size | 9.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5a9bf9c3fe77aaf0b4573b579172cc6ad1461194f9244b7ebfda2c80c82b0c74
|
|
BLAKE2b-256 checksum How to use checksums |
a8b727d3dca3098ff5ca29c10e68f9cc4b8e78b5513ce212909ddfe0cf90d482
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.7
|
Security for pyevaljs3 0.2.4
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.