• 회원가입
  • 로그인
  • 구글아이디로 로그인

FastAPI - 문법 핵심정리

목차
  1. FastAPI 설치하기
  2. /main.py 파일 생성
  3. 서버 실행 (= /main.py 파일의 app 객체 실행)
  4. URL 주소 접속해 웹페이지 확인

 

※ 설치 환경: Python 3.8+ / 웹 부분: Starlette / 데이터 부분: Pydantic

 

FastAPI 설치하기

(리눅스 shell / 윈도우 cmd ) 모드에서 아래 명령어 실행.

 

# FastAPI 설치

pip install fastapi

 

# 프로덕션을 위해 Uvicorn (또는 Hypercorn)과 같은 ASGI 서버도 필요

pip install "uvicorn[standard]"

 

 

/main.py 파일 생성

 

from typing import Union

from fastapi import FastAPI


app = FastAPI()


@app.get("/")

def read_root():

    return {"홈짱닷컴": "Homzzang.com"}


@app.get("/items/{item_id}")

def read_item(item_id: int, q: Union[str, None] = None):

    return {"item_id": item_id, "q": q}

 

PS. 회원 님 코드가 async/await 사용한다면, async def 사용.

 

서버 실행 (= /main.py 파일의 app 객체 실행)

# (리눅스 Shell / 윈도우 cmd)에서 아래 명령어 실행.

 

uvicorn main:app --reload


 

URL 주소 접속해 웹페이지 확인

※ uvicorn 경우, 기본적으로 8000 포트 사용. 

 


 

방문 감사합니다. (즐겨찾기 등록: Ctrl + D)

분류 제목
Recipes FastAPI - 일반 - 방법 - 레시피 (General - How To - Recipes)
Recipes FastAPI - 그래프큐엘 (GraphQL)
Recipes FastAPI - 사용자 정의 요청 및 APIRoute 클래스 (Custom Request and APIRo…
Recipes FastAPI - 조건부 OpenAPI (Conditional OpenAPI)
Recipes FastAPI - OpenAPI 확장 (Extending OpenAPI)
Recipes FastAPI - 입력과 출력을 위한 별도의 OpenAPI 스키마 (Separate OpenAPI Schem…
Recipes FastAPI - 사용자 정의 Docs UI 정적 자산(셀프 호스팅) (Custom Docs UI Stati…
Recipes FastAPI - Swagger UI 구성 (Configure Swagger UI)
목록
찾아주셔서 감사합니다. Since 2012