Skip to content

Commit a1db3d4

Browse files
author
=
committed
260114 기본 웹사이트 생성 (FastAPI)
1 parent 921074d commit a1db3d4

4 files changed

Lines changed: 56 additions & 1 deletion

File tree

.DS_Store

0 Bytes
Binary file not shown.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
*.db
1+
*.db
2+
*.html

src/.DS_Store

0 Bytes
Binary file not shown.

src/main.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from fastapi import FastAPI, Request, Query
2+
from fastapi.templating import Jinja2Templates
3+
from fastapi.responses import HTMLResponse
4+
import sqlite3
5+
from typing import Optional
6+
7+
app = FastAPI()
8+
9+
# 템플릿 설정
10+
templates = Jinja2Templates(directory="templates")
11+
DB_NAME = "part_data.db"
12+
13+
def get_db_rows(manufacturer: str = None, part_name: str = None):
14+
conn = sqlite3.connect(DB_NAME)
15+
conn.row_factory = sqlite3.Row # 컬럼명으로 접근 가능하게 설정
16+
cursor = conn.cursor()
17+
18+
query = "SELECT * FROM part_details WHERE 1=1"
19+
params = []
20+
21+
if manufacturer:
22+
query += " AND manufacturer LIKE ?"
23+
params.append(f"%{manufacturer}%")
24+
if part_name:
25+
query += " AND part_name LIKE ?"
26+
params.append(f"%{part_name}%")
27+
28+
query += " ORDER BY id DESC LIMIT 200" # 최신 데이터 200개만 출력
29+
cursor.execute(query, params)
30+
rows = cursor.fetchall()
31+
conn.close()
32+
return rows
33+
34+
@app.get("/view", response_class=HTMLResponse)
35+
async def read_dashboard(
36+
request: Request,
37+
manufacturer: Optional[str] = None,
38+
part_name: Optional[str] = None
39+
):
40+
# DB에서 데이터 가져오기
41+
rows = get_db_rows(manufacturer, part_name)
42+
43+
# HTML 템플릿 렌더링
44+
return templates.TemplateResponse("index.html", {
45+
"request": request,
46+
"rows": rows,
47+
"manufacturer": manufacturer,
48+
"part_name": part_name
49+
})
50+
51+
if __name__ == "__main__":
52+
import uvicorn
53+
# 서버 실행 (자동 재시작 모드)
54+
uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)

0 commit comments

Comments
 (0)