認識雲端平台與部署流程
實作步驟教學
使用鍵盤 ← → 或點擊下方按鈕切換投影片
平衡性能與易用性
前端優化的首選
開發者友善的新星
| 項目 | Render | Vercel | Railway |
|---|---|---|---|
| 免費方案 | 750 小時/月 | 無限部署 | $5 額度 |
| 最適合 | Flask 後端 | React 前端 | 全端 + 資料庫 |
| 冷啟動 | 有(幾秒) | 極快 | 無 |
| 資料庫 | 需另外設定 | 不支援 | 內建支援 |
雲端平台需要知道你的應用需要哪些套件。
1在 CMD 中執行(在虛擬環境內):
pip freeze > requirements.txt
2檢查產生的檔案內容:
flask==3.0.0
openai==1.12.0
python-dotenv==1.0.0
requests==2.31.0
...
.gitignore 檔案告訴 Git 哪些檔案不要上傳。
# .gitignore 內容
.env
venv/
__pycache__/
*.pyc
包含 API Key 等敏感資訊
虛擬環境,太大且不需要
Python 快取檔案
1初始化 Git(如果還沒有)
git init
2加入所有檔案
git add .
3建立 commit
git commit -m "Initial commit"
4推送到 GitHub
git push -u origin main
由於 .env 不會上傳,需要在雲端平台單獨設定。
OPENAI_API_KEYsk-proj-你的API金鑰1註冊 Render 帳號(可用 GitHub 登入)
2點擊「New +」→「Web Service」
3連接你的 GitHub Repository
4設定 Build Command: pip install -r requirements.txt
5設定 Start Command: python app.py
6設定環境變數 OPENAI_API_KEY
7點擊「Create Web Service」
你的專案資料夾/
├── venv/ # 虛擬環境(不上傳)
├── templates/ # HTML 範本
│ └── index.html
├── app.py # Flask 主程式
├── requirements.txt # 套件清單 ✓
├── .env # 環境變數(不上傳)
├── .env.example # 範例檔案 ✓
└── .gitignore # Git 忽略清單 ✓
檢查 requirements.txt 是否正確
確認套件名稱沒有打錯
檢查 Start Command
確認 app.py 的路徑正確
確認環境變數已設定
API Key 格式正確
檢查 templates 資料夾
確認 index.html 存在
🎉 課程完成!期待你的下一個作品!