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

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 포트 사용. 

 


 


분류 제목
Advanced FastAPI - 비동기 테스트 (Async Tests)
Advanced FastAPI - 설정 및 환경 변수 (Settings and Environment Variables)
Advanced FastAPI - OpenAPI 콜백 (OpenAPI Callbacks)
Advanced FastAPI - OpenAPI 웹훅 (OpenAPI Webhooks)
Advanced FastAPI - WSGI 포함 - 플라스크, 장고 등 (Including WSGI - Flask, Djan…
Advanced FastAPI - 클라이언트 생성 (Generate Clients)
Deployment FastAPI - FastAPI 버전 정보 (About FastAPI versions)
Deployment FastAPI - HTTPS 정보 (About HTTPS)
Deployment FastAPI - 수동으로 서버 실행 - Uvicorn (Run a Server Manually - Uvic…
Deployment FastAPI - 배포 개념 (Deployments Concepts)
Deployment FastAPI - 클라우드 제공업체에 FastAPI 배포 (Deploy FastAPI on Cloud Pro…
Deployment FastAPI - 서버 워커 - Gunicorn과 Uvicorn (Server Workers - Gunico…
Deployment FastAPI - 컨테이너의 FastAPI - 도커 (FastAPI in Containers - Docker…
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…
4/5
목록
찾아주셔서 감사합니다. Since 2012