前提
Python 3.11.7
インストール
ターミナル上で実行
pip install "fastapi[standard]"
確認
fastapi --version
ソースコード
main.pyをアプリのファイル内に入れて以下のコードをコピペ
main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}
サーバー起動
fastapi dev main.py
こんな感じのが出たら成功
FastAPI CLI - Development mode
の
Serving at:にあるURLで(この場合はhttp://127.0.0.1:8000
)
{"message":"Hello World"}
が出る。
INFO Using path main.py
INFO Resolved absolute path /home/user/code/awesomeapp/main.py
INFO Searching for package file structure from directories with __init__.py files
INFO Importing from /home/user/code/awesomeapp
╭─ Python module file ─╮
│ │
│ 🐍 main.py │
│ │
╰──────────────────────╯
INFO Importing module main
INFO Found importable FastAPI app
╭─ Importable FastAPI app ─╮
│ │
│ from main import app │
│ │
╰──────────────────────────╯
INFO Using import string main:app
╭────────── FastAPI CLI - Development mode ───────────╮
│ │
│ Serving at: http://127.0.0.1:8000 │
│ │
│ API docs: http://127.0.0.1:8000/docs │
│ │
│ Running in development mode, for production use: │
│ │
│ fastapi run │
│ │
╰─────────────────────────────────────────────────────╯
INFO: Will watch for changes in these directories: ['/home/user/code/awesomeapp']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [2265862] using WatchFiles
INFO: Started server process [2265873]
INFO: Waiting for application startup.
INFO: Application startup complete.