[TOC] ## 第 1 堂 - 建立虛擬環境 在windows本機下載Python 網址:https://www.python.org/downloads/windows/  查看python版本: ``` python --version ``` 進入python demo專案資料夾: ``` cd "demo專案資料夾路徑" ``` 在demo資料夾建立python環境: ``` python -m venv 檔案名稱 python -m venv sample_env ``` 轉換到虛擬環境: ``` 檔案名稱\Scripts\activate sample_env\Scripts\activate ``` 查看pip 現有套件及版本: ``` pip list ``` 從pip下載fastapi、uvicorn: pip install 套件1 套件2 `pip install fastapi uvicorn` **套件補充:** fastapi - web 框架 unicorn - 下載多執行緒套件 (彌補Python單執行緒特性) ## 第 2-1 堂 - 使用 Fastapi 模組 本堂課我們學到如何使用Fastapi模組,來建立web架構 接續前次課堂,這次我們新建需要的檔案和檔案夾 (一) 在最外層新增主程式 main.py  (二) 新增 Folder "routers" , 並在 Folder 中建立 test_get_items.py  (三) 創建成功後輸入程式碼 以下為 main.py 主程式 程式碼  開頭先輸入需要引用的模組及方法 ( 切記要在檔案的最前面,後方程式碼才會引用成功 ) ``` # from 模組 import 方法 (可以調用py檔中的其中一個方法,也可以調用整個檔案的方法) ``` ``` app = FastAPI() # 宣告 app , 指派FastAPI()方法給app # 有點像中文的簡稱,EX:基隆女子高級中學,簡稱(基女) # 後面需要呼叫FastAPI()方法時,可以直接使用route ``` ``` app.include_router(test_get_items.router) # 使用FastAPI()內的include_router()方法 # 並在方法帶入 test_get_items.router 方法 (這裡的方法為APIRouter()) ``` 接著我們看 test_get_items.py 內的程式碼  創建 router 路由器 ``` router = APIRouter() ``` 以下一個Blob為一個路由中的 function 及 return ------------------------------------ 定義路由 / ``` @router.get("/") # ("/") 路徑,可另外命名,例如 @router.get("/Hello world") # 127.0.0.1:8000/Hello world 可以直接顯示,下面指令之頁面 ``` 定義方法 read_root() ``` def read_root(): return {"Hello": "World"} # 在路由 127.0.0.1:8000/ 中 回傳"Hello": "World" ``` ------------------------------------ 定義路由 /calculator ``` @router.get("/calculator") ``` 定義方法 ``` def calculator ( first: int=0, second: int=0, ): # 定義輸入為int(整數),起始值=0 # return first + second ( retrun int 形態) return f"加起來為:{first + second}" # return string ``` --------------------------------------------------- retrun 形態小筆記 ``` # return f"{int轉string}" # return f"string {型態轉變}" ``` ### 方法介紹 #### FastAPI( ) #### APIRouter( ) #### include_router( ) 在主 FastAPI 應用程式中掛載或嵌套其他 APIRouter 實例 可將路由組織成模塊和子模塊,從而使程式碼更具模塊化和可維護性。 ## 第 2-2 堂 - Git 入門 Git介紹影片: https://www.youtube.com/watch?v=N6YQlPuAamw Git專有名詞 Repository (git 倉庫) Branch 分支 Master 默認分支 merge 合併分支
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up