|
1 | 1 | # slidev-addon-python-runner |
2 | 2 |
|
3 | | -Python runner for the [Monaco Runner feature](https://sli.dev/features/monaco-run) in [Slidev](https://sli.dev/). Code executed in browser using [Pyodide](https://pyodide.org/). |
| 3 | +[](https://badge.fury.io/js/%40cxphoenix%2Fslidev-addon-python-runner) |
| 4 | +[](https://github.com/CXPhoenix/slidev-addon-python-runner/blob/main/LICENSE) |
| 5 | +[](https://sli.dev/addons/) |
| 6 | +[](https://pyodide.org/) |
| 7 | + |
| 8 | +為 [Slidev](https://sli.dev/) 中的 [Monaco Runner 功能](https://sli.dev/features/monaco-run) 提供的 Python 執行器。程式碼使用 [Pyodide](https://pyodide.org/) 在瀏覽器中執行。 |
4 | 9 |
|
5 | 10 |  |
6 | 11 |
|
7 | | -## Usage |
| 12 | +## 功能特色 |
| 13 | + |
| 14 | +- 🐍 在瀏覽器中執行 Python 程式碼 |
| 15 | +- 📦 支援從 PyPI 安裝套件 |
| 16 | +- ⚡ 自動載入匯入的內建套件 |
| 17 | +- 🔄 支援 stdin 輸入處理 |
| 18 | +- 🎨 支援 `py` 和 `python` 兩種語言識別碼 |
| 19 | +- 🛠️ 豐富的錯誤提示和除錯功能 |
| 20 | + |
| 21 | +## 使用方法 |
8 | 22 |
|
9 | | -Firstly, install the package: |
| 23 | +首先,安裝套件: |
10 | 24 |
|
11 | 25 | ```bash |
12 | | -npm install slidev-addon-python-runner |
| 26 | +npm install @cxphoenix/slidev-addon-python-runner |
13 | 27 | ``` |
14 | 28 |
|
15 | | -Then, add it as an addon in your headmatter in `slides.md`: |
| 29 | +然後,在 `slides.md` 檔案的 headmatter 中加入該 addon: |
16 | 30 |
|
17 | 31 | ```md |
18 | 32 | --- |
19 | 33 | addons: |
20 | 34 | - slidev-addon-python-runner |
21 | 35 |
|
22 | | -# Optional configuration for this runner |
| 36 | +# 此執行器的可選設定 |
23 | 37 | python: |
24 | | - # Install packages from PyPI. Default: [] |
25 | | - installs: ["cowsay"] |
| 38 | + # 從 PyPI 安裝套件。預設值:[] |
| 39 | + installs: ["cowsay", "numpy", "pandas"] |
26 | 40 |
|
27 | | - # Code executed to set up the environment. Default: "" |
| 41 | + # 用於設置環境的程式碼。預設值:"" |
28 | 42 | prelude: | |
29 | 43 | GREETING_FROM_PRELUDE = "Hello, Slidev!" |
30 | 44 |
|
31 | | - # Automatically load the imported builtin packages. Default: true |
| 45 | + # 自動載入已匯入的內建套件。預設值:true |
32 | 46 | loadPackagesFromImports: true |
33 | 47 |
|
34 | | - # Disable annoying warning from `pandas`. Default: true |
| 48 | + # 停用 `pandas` 的煩人警告。預設值:true |
35 | 49 | suppressDeprecationWarnings: true |
36 | 50 |
|
37 | | - # Always reload the Python environment when the code changes. Default: false |
| 51 | + # 程式碼變更時總是重新載入 Python 環境。預設值:false |
38 | 52 | alwaysReload: false |
39 | 53 |
|
40 | | - # Options passed to `loadPyodide`. Default: {} |
| 54 | + # 傳遞給 `loadPyodide` 的選項。預設值:{} |
41 | 55 | loadPyodideOptions: {} |
| 56 | + |
| 57 | + # stdin 輸入設定(可選) |
| 58 | + stdin: ["input1", "input2"] # 預定義輸入陣列 |
| 59 | + # 或者使用多行字串: |
| 60 | + # stdin: | |
| 61 | + # first line |
| 62 | + # second line |
| 63 | + # 或者啟用互動模式: |
| 64 | + # stdin: "interactive" |
42 | 65 | --- |
43 | 66 | ``` |
44 | 67 |
|
45 | | -To add an interactive Python code runner, use the `monaco-run` directive: |
| 68 | +### 基本範例 |
| 69 | + |
| 70 | +若要新增互動式 Python 程式碼執行器,請使用 `monaco-run` 指令: |
46 | 71 |
|
47 | 72 | ````md |
48 | 73 | ```py {monaco-run} |
49 | 74 | from termcolor import colored |
| 75 | +import pandas as pd |
| 76 | +import numpy as np |
50 | 77 |
|
51 | 78 | print(colored("Hello, Slidev!", "blue")) |
| 79 | + |
| 80 | +# 建立簡單的 DataFrame |
| 81 | +df = pd.DataFrame({ |
| 82 | + "A": [1, 2, 3, 4], |
| 83 | + "B": np.random.randn(4) |
| 84 | +}) |
| 85 | +print(df) |
| 86 | +``` |
| 87 | +```` |
| 88 | + |
| 89 | +### stdin 輸入處理 |
| 90 | + |
| 91 | +這個 addon 支援多種方式處理 stdin 輸入: |
| 92 | + |
| 93 | +#### 方法 1:在 frontmatter 中預定義輸入 |
| 94 | + |
| 95 | +````md |
| 96 | +--- |
| 97 | +python: |
| 98 | + stdin: |
| 99 | + - "Alice" |
| 100 | + - "25" |
| 101 | +--- |
| 102 | + |
| 103 | +```py {monaco-run} |
| 104 | +name = input("請輸入姓名: ") |
| 105 | +age = input("請輸入年齡: ") |
| 106 | +print(f"你好 {name},你今年 {age} 歲") |
| 107 | +``` |
| 108 | +```` |
| 109 | + |
| 110 | +#### 方法 2:使用程式碼註解定義輸入 |
| 111 | + |
| 112 | +````md |
| 113 | +```py {monaco-run} |
| 114 | +# stdin: "Bob" |
| 115 | +# stdin: "30" |
| 116 | +name = input("請輸入姓名: ") |
| 117 | +age = input("請輸入年齡: ") |
| 118 | +print(f"你好 {name},你今年 {age} 歲") |
| 119 | +``` |
| 120 | +```` |
| 121 | + |
| 122 | +#### 方法 3:啟用互動模式 |
| 123 | + |
| 124 | +````md |
| 125 | +--- |
| 126 | +python: |
| 127 | + stdin: "interactive" |
| 128 | +--- |
| 129 | + |
| 130 | +```py {monaco-run} |
| 131 | +name = input("請輸入姓名: ") |
| 132 | +print(f"你好 {name}!") |
52 | 133 | ``` |
53 | 134 | ```` |
54 | 135 |
|
55 | | -## Bundle `pyodide` |
| 136 | +## 進階設定 |
| 137 | + |
| 138 | +### 錯誤處理 |
| 139 | + |
| 140 | +該 addon 提供智慧錯誤處理: |
| 141 | + |
| 142 | +- **套件未找到**:自動提示如何安裝 PyPI 套件 |
| 143 | +- **輸入錯誤**:當程式碼需要 stdin 輸入時,提供詳細的設定說明 |
| 144 | +- **語法錯誤**:顯示清晰的錯誤訊息和行號 |
| 145 | + |
| 146 | +### 支援的語言識別碼 |
| 147 | + |
| 148 | +您可以使用以下任一語言識別碼: |
| 149 | + |
| 150 | +````md |
| 151 | +```python {monaco-run} |
| 152 | +print("Hello World") |
| 153 | +``` |
| 154 | + |
| 155 | +```py {monaco-run} |
| 156 | +print("Hello World") |
| 157 | +``` |
| 158 | +```` |
| 159 | + |
| 160 | +### Pyodide 打包選項 |
| 161 | + |
| 162 | +預設情況下,當建置投影片時(即 `slidev build`),`pyodide` 套件會被替換為 CDN 版本。這是因為 https://github.com/pyodide/pyodide/issues/1949 的問題,使用打包版本時會導致已匯入的 Python 套件遺失。 |
| 163 | + |
| 164 | +若要打包本地版本的 `pyodide`,請將 `PYODIDE_BUNDLE` 環境變數設為 `true`: |
| 165 | + |
| 166 | +```bash |
| 167 | +PYODIDE_BUNDLE=true slidev build |
| 168 | +``` |
| 169 | + |
| 170 | +⚠️ **注意**:使用本地打包版本時,無法在靜態建置中匯入 Python 套件。 |
| 171 | + |
| 172 | +## 開發 |
| 173 | + |
| 174 | +本專案使用 TypeScript 開發,主要檔案結構: |
| 175 | + |
| 176 | +- `setup/code-runners.ts` - 主要的 Python 執行器實作 |
| 177 | +- `vite.config.ts` - Vite 設定,處理 Pyodide 的打包邏輯 |
| 178 | +- `package.json` - 專案設定和相依性 |
| 179 | + |
| 180 | +### 本地開發 |
| 181 | + |
| 182 | +```bash |
| 183 | +# 安裝相依性 |
| 184 | +pnpm install |
| 185 | + |
| 186 | +# 啟動開發伺服器 |
| 187 | +pnpm dev |
| 188 | + |
| 189 | +# 建置 |
| 190 | +pnpm build |
| 191 | +``` |
| 192 | + |
| 193 | +## 致謝 |
| 194 | + |
| 195 | +本專案基於 [_Kerman](https://github.com/KermanX) 的原始開發工作。 |
| 196 | + |
| 197 | +**原開發者:** |
| 198 | +- _Kerman ([@KermanX](https://github.com/KermanX)) |
| 199 | + |
| 200 | +**此 Repo 維護者:** |
| 201 | +- CXPhoenix ([@CXPhoenix](https://github.com/CXPhoenix)) |
56 | 202 |
|
57 | | -By default, when building slides (i.e. `slidev build`), the `pyodide` package will be replaced with the CDN version. This is because of https://github.com/pyodide/pyodide/issues/1949, which causes the imported python packages to be lost when using the bundled version. |
| 203 | +## 授權 |
58 | 204 |
|
59 | | -To bundle the local version of `pyodide`, set the `PYODIDE_BUNDLE` environment variable to `true`. Note that in this way you can't import python packages in the static build. |
| 205 | +MIT License |
0 commit comments