Skip to content

Commit

Permalink
Add code snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
curder committed Aug 16, 2024
1 parent 5507bd7 commit 9265754
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ function sidebarProgrammingLanguage()
{text: "使用 docker 编译静态资源", link: "/programming-language/node/compile-static-resources-using-docker"},
],
},
{
text: "Python",
items: [
{text: 'Python 执行 JS 代码', link: "/programming-language/python/python-executes-js-code"},
]
},
{
text: "Others",
items: [
Expand Down
29 changes: 29 additions & 0 deletions docs/programming-language/python/python-executes-js-code.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Python 执行 JS 代码

[`PyExecJS`](https://pypi.org/project/PyExecJS/) 是一个Python库,用于在Python中执行JavaScript代码。

## 安装

```bash
pip install PyExecJS
```

## 示例代码

```python
import execjs

# 定义JavaScript代码
js_code = """
function hello(name) {
return `Hello ${name}!`;
}
"""

# 编译JavaScript代码
context = execjs.compile(js_code)

# 调用JavaScript函数
result = context.call('hello', 'Python')
print(result) # 输出:Hello Python!
```

0 comments on commit 9265754

Please sign in to comment.