Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Oracle
- mybatis
- JSP
- form
- 파이썬
- PYTHON
- ibatis
- 비교표현식
- eclipse
- sqlMapClient
- select문
- fastapi
- anaconda
- directivesTag
- pagination
- 드라이브 연결
- pyqt
- sqlMapConfig
- Git
- javascript
- java
- sqldeveloper
- spring
- pymssql
- form태그
- DB 스케쥴러
- sql
- pl/sql
- error
- androidstudio
Archives
- Today
- Total
기록하는 코더
[Python] FastAPI 사용해보기 본문
FastAPI를 사용하기 위해서
1. cmd 창에
pip install fastapi
pip install uvicorn
를 입력해 라이브러리를 설치해준다.
2. Path 환경 변수에 uvicorn 경로를 추가해준다.
uvicorn.exe 설치되어있는 경로
c드라이브 > 사용자 > AppData\Roaming\Python\Python39\Scripts
경로에 설치되어있다.
내 컴퓨터에 설치되어있던 경로
C:\Users\PC-13\AppData\Roaming\Python\Python39\Scripts
예제 - 01
main.py
from fastapi import FastAPI,Form ,HtmlResponse, Request
import uvicorn
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
@app.get("/")
async def root():
return {"message" : "Hello World"}
@app.get("/forw", response_class=HtmlResponse)
async def forw(request : request):
return templates.TemplateResponse("forw.html", {"request": request, "id":id})
@app.get("/param")
async def param(a):
return {"param":a}
@app.post("/post")
async def post(b: str = Form()):
return {"post" : b}
if __name__ == "__main__":
uvicorn.run(app, host="localhost", port=8000)
post.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="http://localhost:8000/post" method="post">
<input type="text" name="b" value="888" />
<input type="submit" value="포스트로보냄" />
</form>
</body>
</html>
예제 - 02
main.py
from fastapi import FastAPI,Request
import uvicorn
import pymssql
from starlette.responses import HTMLResponse
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.get("/forw", response_class=HTMLResponse)
async def forw(request: Request):
a = "홍길동"
b = ["이명박","김경수","김기춘"]
c = [
{"e_id" : "1", "e_name" : "1", "sex" : "1", "addr" : "1"},
{"e_id" : "2", "e_name" : "2", "sex" : "2", "addr" : "2"},
{"e_id" : "3", "e_name" : "3", "sex" : "3", "addr" : "3"},
{"e_id" : "4", "e_name" : "4", "sex" : "4", "addr" : "4"}
]
return templates.TemplateResponse("forw.html", {"request": request, "id":"1", "a":a, "b":b, "c":c})
@app.get("/emp", response_class=HTMLResponse)
async def emp(request: Request):
conn = pymssql.connect(server='192.168.145.16', database='PYTHON', user='sa', password='python')
cursor = conn.cursor() # 쿼리 생성과 결과 조회를 위해 사용
cursor.execute('SELECT * FROM EMP')
list = cursor.fetchall()
return templates.TemplateResponse("emp.html", {"request": request, "list": list })
@app.get("/param")
async def param(a):
return {"param": a}
if __name__ == "__main__":
uvicorn.run(app, host="localhost", port=8000)
forw.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
FORWARD
<h1> Item ID : {{ id }} </h1>
<h1> Item a : {{ a }} </h1>
<h1> Item b : {{ b }} </h1>
<ul id='b' style="list-style: none;">
{% for bitem in b %}
<li>
<h3> {{bitem}} </h3>
</li>
{% endfor %}
</ul>
<table border='1' style="text-align: center;">
<tr style="font-weight: bold;">
<td>사번</td>
<td>이름</td>
<td>성별</td>
<td>주소</td>
</tr>
{% for item in c %}
<tr>
<td>{{item.e_id}}</td>
<td>{{item.e_name}}</td>
<td>{{item.sex}}</td>
<td>{{item.addr}}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
emp.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
FORWARD
<h1> Item ID : {{ id }} </h1>
<h1> Item a : {{ a }} </h1>
<h1> Item b : {{ b }} </h1>
<ul id='b' style="list-style: none;">
{% for bitem in b %}
<li>
<h3> {{bitem}} </h3>
</li>
{% endfor %}
</ul>
<table border='1' style="text-align: center;">
<tr style="font-weight: bold;">
<td>사번</td>
<td>이름</td>
<td>성별</td>
<td>주소</td>
</tr>
{% for item in c %}
<tr>
<td>{{item.e_id}}</td>
<td>{{item.e_name}}</td>
<td>{{item.sex}}</td>
<td>{{item.addr}}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
'Python' 카테고리의 다른 글
[Python] pymssql 활용 - 01 (0) | 2023.01.04 |
---|---|
[Python] PyQt 기본 예제 (0) | 2022.12.30 |
[Python] PyQt 이용하기 (0) | 2022.12.30 |
[Python] OOP(Object Oriented Programming) (0) | 2022.12.26 |
[Python] function (1) | 2022.12.24 |