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', ['a', 'b'])
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代码, 返回其结果


pyevaljs3.Context

  • def call(self, func, *args):
       调用指定的函数, 返回其结果
       :param func: 函数名
       :param args: 函数的参数列表

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)


使用自定义版本的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.1.3.tar.gz (6.5 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.1.3-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyevaljs3-0.1.3.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/6.0.0 pkginfo/1.9.6 requests/2.26.0 requests-toolbelt/0.10.1 tqdm/4.65.0 CPython/3.9.13

File hashes

Hashes for pyevaljs3-0.1.3.tar.gz
Algorithm Hash digest
SHA256 7b2b54728de02f656af6955c6ad58a6e960ad99cefd17917eaf6b2fc53f237ce
MD5 cfb0feeb5b5691026b9e91c4e7bd4c9c
BLAKE2b-256 f36a53520187cd2d3fa0291649e8892fde31da42915a1a968ff8850d6fe8d0f7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyevaljs3-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/6.0.0 pkginfo/1.9.6 requests/2.26.0 requests-toolbelt/0.10.1 tqdm/4.65.0 CPython/3.9.13

File hashes

Hashes for pyevaljs3-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 23471c61d358978861be23a17c25afa6286ad2804da501c58cfbd76768d5b7b5
MD5 6780f66b482747389701718a103f8ee9
BLAKE2b-256 bff02c57b144686a0128d3384254d736a1e040f274ce926b7d3f1efe881c50fa

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