-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
docs/programming-language/python/python-executes-js-code.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! | ||
``` |