# Python version manger - [pyenv](https://github.com/pyenv/pyenv) ## Virtual env ( package ) manager - [pipenv](https://pipenv.pypa.io/en/latest/) - [poetry](https://python-poetry.org/) # Container dev - [devcontainer](https://code.visualstudio.com/docs/devcontainers/containers) - [vscode devcontainer extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) - `ctrl + shift + p` to open Command Palette / or click left-btm button - select `Dev Container: Add devcontainer configuration file` - Choose `Python` select python version like `3.10-buster` - Select features - black: code formater - isort: import formater - [poetry](https://python-poetry.org/): package manager - After create `.devcontainer/devcontainer.json` - `ctrl + shift + p` to open Command Palette / or click left-btm button - select `Dev Container: Reopen in Container` ## API framwork steps - 打開dev container - add features 註: 如果features有缺漏,用pipx install - 開一個新的terminal(in Dev container) - init project `poetry init` - install virtualenv `poetry install` - [FastApi](https://fastapi.tiangolo.com/) - `poetry add fastapi` - `poetry add "uvicorn[standard]"` - [uvicorn app factory](https://www.uvicorn.org/#application-factories) - Add `packages = [{include = "app"}]` to `project.toml` `tool.poetry` block - Add `app/__init__.py` with content below ``` from typing import Union from fastapi import FastAPI fapp = FastAPI() @fapp.get("/") def read_root(): return {"Hello": "World"} @fapp.get("/hehe") def read_root(): return {"Hello": "HeHe"} @fapp.get("/items/{item_id}") def read_item(item_id: int, q: Union[str, None] = None): return {"item_id": item_id, "q": q} ``` - run app `uvicorn app:fapp --reload` - Access http://127.0.0.1:8000 in browser - Access docs http://127.0.0.1:8000/docs for OpenApi docs/Sweagger - [poetry scripts](https://python-poetry.org/docs/pyproject/#scripts) - e.g. from `xxx.zip` `poetry run dev` - https://medium.com/@eric248655665/%E4%BB%80%E9%BA%BC%E6%98%AF-wsgi-%E7%82%BA%E4%BB%80%E9%BA%BC%E8%A6%81%E7%94%A8-wsgi-f0d5f3001652 // Chris 補充說明 uvicorn app:fapp --reload <--跑之前必須先進入poetry shell 環境中才能執行
×
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