본문 바로가기

Framework, Library

(22)
[Fast_api] path parameters and Numeric validations Query parameters에서 Query 와 같은 역할을 Path 가 해준다. from typing import Union from fastapi import FastAPI, Path, Query app = FastAPI() @app.get("/items/{item_id}") async def read_items( item_id: int = Path(title="The ID of the item to get"), q: Union[str, None] = Query(default=None, alias="item-query") ): result = {"item_id": item_id} if q: result.update({"q": q}) return result 다른 메타데이터의 역할을 크게 체감 못하고 ..
[Fast api] Query Parameters and String Validations from typing import Union from fastapi import FastAPI app = FastAPI() @app.get("/items/") async def read_items(q: Union[str, None] = None): results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} if q: results.update({"q": q}) return results 위 코드는 /items/ 로 get방식 요청시, results를 반환하되, 쿼리 파라미터 q도 존재하는 경우, 추가해서 반환해준다. 만약 q가 50자 이상 넘으면 안되게끔 설정하고 싶다면? Query from fastapi Annotated from typing(or t..
[Fast_api] Request Body 파이썬 현재 사용하는 버전이 3.9.11이라서, 문법이 구버전으로 맞춰져있습니다. from typing import Union from fastapi import FastAPI from pydantic import BaseModel class Item(BaseModel): name: str description: Union[str, None] = None price: float tax: Union[float, None] = None app = FastAPI() @app.post("/items/") async def create_item(item: Item): return item 공식 문서에서 Request body를 선언할 때, Pydantic 모델을 사용하는 것을 권장한다. Body를 선언하기 위해 ..
[Fast_api] 쿼리 매개변수 경로 매개변수의 일부가 아닌 다른 함수 매개변수를 선언할 때, "query" parameter로 자동 해석한다. 쿼리는 URL에서 ? 후에 나오고 &으로 구분되는 key-value 쌍의 집합 from fastapi import FastAPI app = FastAPI() fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}] @app.get("/items/") async def read_item(skip: int = 0, limit: int = 10): return fake_items_db[skip : skip + limit] 위와 같이 적으면, skip = 0, limit = 10은 기본값이다. 선택적 매개변수 ..
[Fast_api] 경로 매개변수 from fastapi import FastAPI app = FastAPI() @app.get("/items/{item_id}") def read_root(item_id): return {"item_id": item_id} http://127.0.0.1:8000/items/foo로 가면 다음과 같은 응답을 준다. {"item_id":"foo"} 타입을 지정할수도 있다. from fastapi import FastAPI app = FastAPI() @app.get("/items/{item_id}") def read_root(item_id: int): return {"item_id": item_id} http://127.0.0.1:8000/items/3으로 가면? {"item_id":3} 이전 url로 가면..
[Fast_api] start from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"message": "Hello World"} uvicorn main:app -reload 위 명령을 읽어보면 다음과 같다. main : main.py app : main.py 내부의 app = FastAPI() 로 생성한 오브젝트(인스턴스) --reload : 코드 변경 후 서비 재시작. 개발에서만 사용 http://127.0.0.1:8000를 확인하면, {"message":"Hello World"} http://127.0.0.1:8000/docs 단계별 요약 1) FastAPI import from fastapi import FastAPI 2) FastA..
[Fast_api] fast_api란? FastAPI : API를 만들기 위한 파이썬 웹 프레임워크 주요 특징 빠르다 빠른 코드 작성 적은 버그 직관적 쉬움 짧음 견고함 표준 기반 설치 pip install fastapi pip install "uvicorn[standard]" Test_Code # main.py from typing import Union from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"} @app.get("/items/{item_id}") def read_item(item_id: int, q: Union[str, None] = None): return {"item_id": item_id, "q":..
React_Life Cycle React Life Cycle : 리액트의 각 컴포넌트는 라이프사이클을 가진다. 과정을 단순히 보면, 컴포넌트를 작성해서 화면에 UI가 그려지는 과정 ReactDom.render() const App = () => { return ( Hello World ); }; const root = ReactDOM.createRoot(document.getElementById("root")); root.render( ); 컴포넌트의 생명주기 Mount(생성) Update(업데이트) Unmount(제거) - Mount - constructor : 컴포넌트 생성자 메서드, 컴포넌트가 생성되면 가장 먼저 실행되는 메서드(this.props, this.state)에 접근 가능하고 리액트 요소 반환 - getDerivedS..