프레임워크 종류
백엔드 프레임워크
- 스프링 (자바)
- 스프링부트 (자바)
- node.js (자바스크립트)
- express.js (자바스크립트, 빠름)
- nest.js (자바스크립트)
- Django (파이썬)
- flask (파이썬)
- Fastapi (파이썬)
프론트 프레임워크
- react.js
- angular.js
- next.js
- view.js
FastAPI란?
1. 타 프레임워크에 비해 간결하다.
2. 의존성 주입 위주의 설계로, DB 등 의존성 관리가 편리하고 버그 위험이 적다.
3. 동시성 기반의 비동기 동작으로 충분히 빠른 성능을 낸다.
4. 성능 Node.JS와 대등할 정도로 매우 높은 성능
(최고의 퍼포먼스인 Nest JS와 함께 차세대 프레임 워크)
html과 py연결
.vscode / setting.json
{
"python.languageServer": "Pylance",
"python.analysis.autoImportCompletions": true,
"python.analysis.extraPaths": ["./src", "./my_modules"]
}
main.py
자동으로 웹서버 키는 방법
as-is(cmd): uvicorn main:app --reload
to-be(py코드): if __name__ == '__main__':
uvicorn.run(app, host="localhost", port=8000)
# fastapi 라이브러리에서 FastAPI 클래스를 가져옴
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
import uvicorn
templates = Jinja2Templates(directory="templates")
# FastAPI 클래스로 웹 객체를 만듦
app = FastAPI()
# 웹 객체에다가 주소창에 http://xxx.xxx.xxx.xxx/ 로 사이트 들어올때 맵핑 선언, 그때 hello 함수 실행됨.
@app.get("/")
def hello():
return {"message":"안녕하세요 fastAPI입니다."} # http://xxx.xxx.xxx.xxx/ 주소창에 입력한 대가로, 메세지를 보여줌.
@app.get("/test")
def test(request: Request):
print(request)
return templates.TemplateResponse("test.html", context = {'request':request, 'a':2})
if __name__ == '__main__':
uvicorn.run(app, host="localhost", port=8000)
test.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>테스트페이지</title>
</head>
<body>
<h1>{{a}}</h1>
<h1>안녕하세요 html 입니다.</h1>
</body>
</html>
Get 방식으로 값 넘기기
@app.get("/test1/{name}/{age}")
def test(request: Request, name: str, age: int): # 캐스팅되서 들어온다.
print(name, age)
return templates.TemplateResponse("test.html", context = {'request':request, 'a':2})
@app.get("/test2")
def test(request: Request, name: str, age: str):
print(name, age)
return templates.TemplateResponse("test.html", context = {'request':request, 'a':2})
Post 방식으로 값 넘기기
get(test_get) > post_test.html > post(test_post)
@app.get("/test_get")
def test_get(request: Request):
return templates.TemplateResponse("post_test.html", {'request': request})
@app.post("/test_post")
def test_post(name: Annotated[str, Form()], pwd: Annotated[int, Form()]):
print(name, pwd)
post_test.html
<form action="/test_post" method="post">
<p>이름: <input type="text" name="name"></p>
<p>비밀번호: <input type="password" name="pwd"></p>
<p>이름, 비밀번호를 입력하고 제출버튼을 누르세요. <input type="submit" value="제출"></p>
</form>
Tags:
클라우드