Skip to main content

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中的结果了。

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.4.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

pyrunjs-0.1.4-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file pyrunjs-0.1.4.tar.gz.

File metadata

  • Download URL: pyrunjs-0.1.4.tar.gz
  • Upload date:
  • Size: 4.0 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

Hashes for pyrunjs-0.1.4.tar.gz
Algorithm Hash digest
SHA256 d710c86bc4fb8f32343768442a6ed18582903a352367fbd1444493a4859f5933
MD5 a16859b6da64298ee00da8352779f882
BLAKE2b-256 a187f44d110a2f04ee4006b70152134bac3f5abca0b067d2a6abe52e38809a9c

See more details on using hashes here.

File details

Details for the file pyrunjs-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: pyrunjs-0.1.4-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

Hashes for pyrunjs-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2a257d43416a63042c94c15aae9f50aedcc3ef954c31a61c3e1cfbe1eede320f
MD5 b77db0b7cc345c45b985bdd8bbdb407a
BLAKE2b-256 13559645f35678a793b2dcaecd95d07d5c69ae8ca4477e4a81387c6a04da30a5

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