FastAPI

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 - 템플릿 (Templates)
Advanced FastAPI - 웹소켓 (WebSockets)
Advanced FastAPI - 수명 이벤트 (Lifespan Events)
Advanced FastAPI - 웹소켓 테스트 (Testing WebSockets)
Advanced FastAPI - 테스트 이벤트: 시작 - 종료 (Testing Events: startup - shutdo…
Advanced FastAPI - 재정의를 통한 종속성 테스트 (Testing Dependencies with Overrid…
Advanced FastAPI - 데이터베이스 테스트 (Testing a Database)
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)
2/2
목록
 홈  PC버전 로그인 일본어
그누앞단언어
그누뒷단언어
그외코딩언어
그누보드
제작의뢰
Q&A
커뮤니티
웹유틸
회원센터
홈짱닷컴 PC버전 로그인