Skip to content

Commit 9265754

Browse files
committed
Add code snippet
1 parent 5507bd7 commit 9265754

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

docs/.vitepress/config.ts

+6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ function sidebarProgrammingLanguage()
6666
{text: "使用 docker 编译静态资源", link: "/programming-language/node/compile-static-resources-using-docker"},
6767
],
6868
},
69+
{
70+
text: "Python",
71+
items: [
72+
{text: 'Python 执行 JS 代码', link: "/programming-language/python/python-executes-js-code"},
73+
]
74+
},
6975
{
7076
text: "Others",
7177
items: [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Python 执行 JS 代码
2+
3+
[`PyExecJS`](https://pypi.org/project/PyExecJS/) 是一个Python库,用于在Python中执行JavaScript代码。
4+
5+
## 安装
6+
7+
```bash
8+
pip install PyExecJS
9+
```
10+
11+
## 示例代码
12+
13+
```python
14+
import execjs
15+
16+
# 定义JavaScript代码
17+
js_code = """
18+
function hello(name) {
19+
return `Hello ${name}!`;
20+
}
21+
"""
22+
23+
# 编译JavaScript代码
24+
context = execjs.compile(js_code)
25+
26+
# 调用JavaScript函数
27+
result = context.call('hello', 'Python')
28+
print(result) # 输出:Hello Python!
29+
```

0 commit comments

Comments
 (0)