<div style="text-align: right; background-color: #f5f5f5; padding: 4px 12px; border-radius: 5px; display: inline-block; float: right; font-size: 1.4rem;">
Last Updated:2025/07/01
</div>
# Code Server 操作範例說明
[TOC]
## 安裝 Extension 步驟
:::warning
<i class="fa fa-bullhorn" aria-hidden="true"></i> **備註**:
由於 Open OnDemand 是透過 **計算節點** 取得運算資源,而計算節點無對外網路連線,因此需透過其他方式來安裝 Extension。
:::
**1. 登入 Login Node**
<div style="border: 1.5px solid #B7B7B7; padding: 1rem 1rem; border-radius: 5px; color: #333;">
🔗 <strong>詳細教學請參閱</strong>
<a href="https://man.twcc.ai/@AI-Pilot/ByTJJNVz1g" target="_blank" style="color: #007ACC;">
主機登入/登出教學連結
</a>
</div>
<br>
**2. 載入 Code Server 模組**
```bash
[user@ilgn01]$ ml purge # 清空目前已載入的模組
[user@ilgn01]$ ml tools/code-server # load 模組
```
**3. 安裝 Extension**
以常用的 Python 套件 [ms-python.python](https://marketplace.visualstudio.com/items?itemName=ms-python.python) 為例:

- 於 Installation 查找對應安裝程式碼,並將其複製
- 回到 Login Node 執行安裝指令:
```
[user@ilgn01]$ code-server --install-extension ms-python.python
```
<font color="red">➤ **若從其他 Extension Marketplace 安裝,只要確認 Extension Identifier 的格式為</font>** `${publisher}.${name}` <font color="red">**即可。**</font>
**4. 返回 Open OnDemand 的 Code Server 畫面**,即可在 Extension 分頁中看到剛剛安裝完成的擴充套件

<br>
---
<br>
## 程式範例執行說明
本範例將示範如何使用 Code Server 撰寫並執行經典的鳶尾花(Iris)分類問題。
**1. 先於 Login Node 建立開發環境**
- 請先登入 login node 並完成相關套件的安裝
- 此範例所需套件為`pandas`及 `scikit-learn`
- 若尚未安裝套件,請參考此教學來建立虛擬環境 👉 [Miniconda 安裝與虛擬環境教學](https://man.twcc.ai/@f1-manual/App_Jupyter_Lab)
**2. 撰寫 Iris 鳶尾花範例程式碼**
```
[user@ilgn01]$ vi iris.py
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
iris = load_iris()
data = pd.DataFrame(data=iris.data, columns=iris.feature_names)
print(data.describe())
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3)
model = KNeighborsClassifier(n_neighbors=3)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy * 100:.2f}%')
```
<br>
---
<br>
## 回到 Open OnDemand 開啟終端機
完成程式撰寫後,您可以透過 Code Server 介面內建的終端機,直接執行程式碼。
1. 於 Code Server 中,按下 <code>Ctrl + Shift + \` </code>鍵,即可開啟內建終端機視窗。
2. `python3 [your python script]` 來執行程式。
```
[user@icpnp301 ]$ conda activate ood_test_env
(ood_test_env)[user@icpnp301 ]$ python3 iris.py
```

<br>
<br>
<!-- 作者資訊 -->
<div style="border-top: 2px solid #eee; padding: 28px 20px ; display: flex; justify-content: space-between; align-items: center; font-family: sans-serif;">
<div>
<div style="font-size: 20px; font-weight: bold; margin-bottom: 8px;"> Code Server 操作範例說明</div>
<div style="max-width: 640px; color: #444; line-height: ;">
Author:Pei-Hsun Kuan<br>Date:2025-02-13</div>
</div>
<div>
<a href="https://man.twcc.ai/@f1-manual/manual" target="_blank" style="display: inline-block;">
<img src=" https://iservice.nchc.org.tw/nchc_service/images/nchc/pi4.png" alt="創進一號使用說明" style="height: 80px;">
</a>
</div>
</div>