0
0

Fast API構築

Posted at

1, ディレクトリ作成

mkdir fast-api-test
cd fast-api-test

2, pythonの仮想環境構築。ライブラリの構成を環境ごとに分けるため

python -m venv venv
-m venv: Pythonのモジュールを指定するオプション。-mは「モジュール」を意味し、venvモジュールを使って仮想環境を作成するよう指示。
venv: 作成する仮想環境のディレクトリ名。
. venv/bin/activate

で仮想環境に入る。

3, ライブラリインストール

pip install fastapi
pip install "uvicorn[standard]"

で必要なライブラリのインストール

4, 簡単なアプリケーションの例

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}


@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": q}

以下のコマンドでサーバー起動

uvicorn main:app --reload

各コマンドの意味はこちら
main: main.pyファイル
app: main.pyapp = FastAPI()の行で生成されたオブジェクト
--reload:コードを変更したらサーバーを再起動する

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0