Skip to main content

A simple library to run JavaScript code based on Python

Project description

js_code

A simple Python library to run JavaScript code based on Python.

特性 / Features

  • Execute JavaScript code directly from Python.

  • Supports running scripts or single-line JavaScript code.

  • Easy integration with Node.js using execjs.

  • Provides a simple API to interact with JavaScript functions.

  • 直接从 Python 执行 JavaScript 代码。

  • 支持运行脚本或单行 JavaScript 代码。

  • 通过 execjs 实现与 Node.js 的简易集成。

  • 提供简单的 API 与 JavaScript 函数交互。

安装 / Installation

You can install js_code using pip:

pip install js_code

试例 / Examples

1. Python 调用 js_read 无参数调用

  • text.js
// 定义一个函数 fn,返回数字 123
function fn() {
    return 123;
};
  • Python 代码
from js_code import js_read

# 读取 JavaScript 文件并调用 fn 函数
js = js_read('text.js')
data = js.call('fn')  # 调用 JavaScript 中的 fn 函数
print(data)  # 输出 123

2. Python 调用 js_read 传参调用

  • text.js
// 定义一个函数 fn,接受两个参数 a 和 b,并返回它们的和
function fn(a, b) {
    return a + b;
};
  • Python 代码
from js_code import js_read

# 读取 JavaScript 文件并调用 fn 函数,传递参数 1 和 2
js = js_read('text.js')
data = js.call('fn', 1, 2)  # 调用 JavaScript 中的 fn 函数并传递参数
print(data)  # 输出 3

3. Python 调用 js_read 头部添加 JavaScript 代码调用

  • text.js
// 定义一个函数 fn,返回变量 a 的值
function fn() {
    return a;
};
  • Python 代码
from js_code import js_read

# 读取 JavaScript 文件,并在文件开头添加 var a = 10;
js = js_read('text.js', 'var a = 10;')
data = js.call('fn')  # 调用 JavaScript 中的 fn 函数
print(data)  # 输出 10

4. Python 调用 js_read 直接在 Python 中调用

  • Python 代码
# 在 Python 中直接定义 JavaScript 代码
js = js_read('''var a = 10;
function fn() {
    return a;
};''')

# 调用 JavaScript 中的 fn 函数
data = js.call('fn')
print(data)  # 返回 10

5. Python 调用 js_run 无参数调用

  • text.js
// 定义一个变量 a 和一个函数 fn,函数返回变量 a 的值
var a = 10;
function fn() {
    return a;
};

console.log(fn());  // 输出函数调用结果
  • Python 代码
from js_code import js_run

# 调用 js_run 执行 text.js 并返回控制台输出
data = js_run('text.js')
print(data)  # 返回 ['10'],按 
  
 切割的列表

6. Python 调用 js_run 传参,Node.js 接收使用

  • text.js
// Node.js 使用 process.argv 获取传递的参数
var arr = process.argv.slice(2);
console.log("接收到的arr", arr[0]);  // 输出第一个参数
console.log("接收到的arr", typeof arr[0]);  // 输出类型

arr = JSON.parse(arr[0]);  // 解析参数为 JSON 对象
console.log("转换后的arr", arr);  // 输出转换后的对象
console.log("转换后的arr", typeof arr);  // 输出对象类型
  • Python 代码
from js_code import js_run

# 调用 js_run 执行 text.js 并传递多个参数
data = js_run('text.js', 1, "2", [3, "4"], {'key': "value"})
print(data)  # 返回按 
  
 切割的列表
# ['接收到的arr [1,"2",[3,"4"],{"key":"value"}]', '接收到的arr string', 
# "转换后的arr [ 1, '2', [ 3, '4' ], { key: 'value' } ]", '转换后的arr object']

7. Python 调用 js_run 传参,Node.js 接收使用,并指定切割

  • text.js
// 接收参数并按指定格式输出
var arr = process.argv.slice(2);
console.log(`**接收到的arr${arr[0]}**`);
console.log(`**接收到的arr${typeof arr[0]}**`);

arr = JSON.parse(arr[0]);
console.log(`**转换后的arr${arr}**`);
console.log(`**转换后的arr${typeof arr}**`);
  • Python 代码
from js_code import js_run

# 调用 js_run 执行 text.js 并传递参数,使用 "**" 分隔输出
data = js_run('text.js', 1, "2", [3, "4"], {'key': "value"}, split="**")
print(data)
# 返回按 
  
 切割的列表拼接成字符串后按传入的 ** 切割
# ['', '接收到的arr[1,"2",[3,"4"],{"key":"value"}]', '', '接收到的arrstring', '', 
# '转换后的arr1,2,3,4,[object Object]', '', '转换后的arrobject', '']

贡献 / Contributing

We welcome contributions to improve js_code. Feel free to open issues and pull requests! 我们欢迎对 js_code 的贡献。欢迎提出问题和提交拉取请求!

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

js_code-2.0.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

js_code-2.0.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file js_code-2.0.0.tar.gz.

File metadata

  • Download URL: js_code-2.0.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.13

File hashes

Hashes for js_code-2.0.0.tar.gz
Algorithm Hash digest
SHA256 85c41cc9e9da260920c2e5ec4ef9a581be137ad89d2271bcb4e3990382e31622
MD5 53b5c4f8a0a8bd54de4d67f9d5077488
BLAKE2b-256 8dcaa8332f860d0d3e71970abd4ea9df706296cad117fde5396fae1a26a40cd1

See more details on using hashes here.

File details

Details for the file js_code-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: js_code-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.13

File hashes

Hashes for js_code-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d86f1828af56bba0d1fc679f1ab69c5de82e8ce59912c8afb09a96a5c0eeb089
MD5 9e0ac50e2c896ae8d930a8a5250148d8
BLAKE2b-256 91b800aa0eb852b097f6a3fb59b02a7da8bc5a1c9dd11028e7963c8ec1234895

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