Skip to main content

一个依赖node.js来执行js代码的python库

Project description

一个依赖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):
       编译javascript源代码
       :param source: 源代码字符串或要读取的文件路径
       :param suffix: js脚本文件名后缀(指定以什么模式执行), 默认是".js", 可选的值还有".cjs", ".mjs"等
       :return: Context

  • def eval(self, code: str = None, ignore_output=False):
       执行javascript代码, 返回其结果

  • async def async_eval(self, code: str = None, ignore_output=False):
       执行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) -> Context:
       编译js源代码
       :param source: 源代码字符串或要读取的文件路径
       :param mode: 执行模式, 默认以.js的行为去执行
       :return: Context

  • def eval_(code: str = None, ignore_output=False):
       执行javascript代码, 返回其结果
       :param code: js代码
       :param ignore_output: 是否忽略执行过程中的输出, 若为True则仅返回其结果, 默认不忽略(False)

  • async def async_eval(code: str = None, ignore_output=False):
       执行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'

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

pyevaljs3-0.2.1.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyevaljs3-0.2.1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file pyevaljs3-0.2.1.tar.gz.

File metadata

  • Download URL: pyevaljs3-0.2.1.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.7

File hashes

Hashes for pyevaljs3-0.2.1.tar.gz
Algorithm Hash digest
SHA256 670f56a3e89defff7dc00c5cef30e9221b8a3a96f822d3691bf8d9c7823b12ef
MD5 5ca4ca151faa077a5cfecf3f0f690e38
BLAKE2b-256 1eaff6d1edc1b10f1f98d3917b463a8e4838e797e2f77a838fb3c1e12e01772b

See more details on using hashes here.

File details

Details for the file pyevaljs3-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: pyevaljs3-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.7

File hashes

Hashes for pyevaljs3-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3f3b4ac4edc63aee5eefeda9a065a2e9186c69aa3c020dd71580a8e040a9aab7
MD5 b505f021eae2db151bd6a9516a3dae58
BLAKE2b-256 eb5a152872b30696ed464719c31d20d5bf93cd8c595cc4192cf3f8317284803c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page