在 Visual Studio Code 使用 Google Colab

在 Visual Studio Code 使用 Google Colab 有兩種主要方式:

  1. 官方 Colab VS Code 插件(最簡單,Google 已正式推出)。
  2. 第三方工具(如 colabcode 或 colab_ssh),透過遠端連線把 Colab 當作後端伺服器,在 VS Code 中編輯。

🔹 方法一:官方 Colab VS Code 插件(推薦)

Google 在 2025 年正式釋出 Colab VS Code Extension:

  • 在 VS Code 的 Extensions 市集中搜尋 Colab,安裝官方插件。
  • 打開 Notebook 時,可以選擇 Kernel → Colab,直接連線到 Colab 的雲端環境。
  • 支援快速連線 GPU/TPU,並能使用 VS Code 的完整編輯體驗(如 Copilot、Lint、Debugger)。
  • 限制:部分原生 Colab 功能(如 drive.mount()、files.upload())暫時無法使用,需要改用 Google API 或替代方案。
    👉 適合想要在 VS Code 中享受 Colab 的運算資源,同時保留 VS Code 的編輯輔助工具。

🔹 方法二:第三方工具(進階玩法)

  1. 使用 colabcode 套件
  • 在 Colab Notebook 中執行:
!pip install colabcode
  from colabcode import ColabCode
  ColabCode(port=10000, password="yourpassword")

`

  • 這會啟動一個 VS Code Server,並提供瀏覽器連結。
  • 你可以在 Colab 的 GPU 環境中直接使用 VS Code(網頁版)。
  1. 使用 colab_ssh + VS Code Remote
  • 在 Colab Notebook 安裝:
    !pip install colab_ssh --upgrade
    from colabssh import launchssh_cloudflared
    launchsshcloudflared(password="yourpassword")
    
`
- 取得 SSH 連線資訊後,在 VS Code 使用 Remote-SSH 插件連線。  
- 這樣就能把 Colab 當作遠端主機,在 VS Code 本地編輯並執行。  
👉 適合需要完整 VS Code 桌面體驗,並且熟悉 SSH/遠端開發的使用者。  

---
## 📌 總結
- 最簡單方式:安裝 Google 官方 Colab VS Code 插件,直接在 VS Code 中選擇 Colab Kernel。  
- 進階方式:用 colabcode 或 colab_ssh,把 Colab 當作遠端伺服器,透過 VS Code Remote 連線。  
- 注意:Colab 的免費資源有使用限制(例如 GPU 使用時間),在 VS Code 中使用時也會受到相同規範。  

# Also See
```dataview
table file.name as "File", date(file.mtime) as "Last Modified"
from ""
where file.path != this.file.path  and file.folder = this.file.folder and file.path != null
sort file.mtime desc