Python으로 간단한 REST API를 만들려면 뭘 써야 하나요? 가장 빠른 방법이요.
FastAPI로 가장 빠르게 API 서버 만들기:
# 설치pip install fastapi uvicorn# main.pyfrom fastapi import FastAPI, HTTPExceptionfrom pydantic import BaseModelapp = FastAPI()# 메모리 DBtodos = []class Todo(BaseModel): title: str done: bool = False@app.get("/todos")def get_todos(): return todos@app.post("/todos", status_code=201)def create_todo(todo: Todo): todos.append(todo.dict()) return todo@app.delete("/todos/{idx}")def delete_todo(idx: int): if idx >= len(todos): raise HTTPException(404, "Not found") return todos.pop(idx)# 실행# uvicorn main:app --reload# http://localhost:8000/docs 에서 Swagger UI 자동 생성
FastAPI는 Swagger 문서가 자동으로 만들어져서 프로토타이핑에 최고입니다.
댓글 1
FastAPI로 가장 빠르게 API 서버 만들기:
FastAPI는 Swagger 문서가 자동으로 만들어져서 프로토타이핑에 최고입니다.