templates 폴더에 index.html 파일을 생성한다.
이때 <table class="table table-primary table-striped"> 부분과 같이 디자인이 들어간 테이블을 생성 하기 위해서는 부트스트랩 사이트에 들어가 테이블을 생성하는 css를 복사하여 붙여넣어 준다.
!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
<table class="table table-primary table-striped">
<!-- 테이블 만들기-->
<tr>
<th>No</th>
<th>등록일시</th>
<th>사망자</th>
<th>확진자</th>
<th>누적 의심자</th>
<th>누적확진률</th>
<th>일일 확진자</th>
<th>일일 사망자</th>
</tr>
{% for i in range(0, cnt, 1)%}
<tr>
<td>{{i+1}}</td>
<td>{{result["등록일시"][i]}}</td>
<td>{{result["사망자"][i]}}</td>
<td>{{result["확진자"][i]}}</td>
<td>{{result["누적 의심자"][i]}}</td>
<td>{{result["누적확진률"][i]}}</td>
<td>{{result["일일 확진자"][i]}}</td>
<td>{{result["일일 사망자"][i]}}</td>
</tr>
{% endfor%}
</table>
<!-- for문을 활용하여 등록일시 출력-->
{%
for i in range(0, cnt, 1)
%}
{{result["등록일시"][i]}}
{%
endfor
%}
</body>
</html>
이후 아래와 같이 .py 파일을 생성하여 작성하여 flask를 통해 확인해보면 정상적으로 테이블이 나오는 것을 확인 할 수 있다.
from flask import Flask, render_template, send_file
import pandas as pd
import matplotlib.pyplot as plt
from io import BytesIO
import datetime
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/corona')
def corona():
corona_df = pd.read_csv('corona.csv')
corona_df.columns=["인덱스", "등록일시","사망자", "확진자", "게시글 번호", "기준일", "기준시간", "수정일시", "누적 의심자", "누적확진률"]
corona_df.sort_values("등록일시", inplace=True)
corona_df["일일 확진자"]=(corona_df["확진자"] - corona_df["확진자"].shift()).fillna(0)
corona_df["일일 사망자"]=corona_df["사망자"].diff().fillna(0)
corona_df.drop(['인덱스', '기준일', '게시글 번호', '기준시간','수정일시'], axis=1, inplace = True)
corona_df.reset_index(drop=True, inplace=True)
corona_dict=corona_df.head(50).to_dict()
cnt=len(corona_dict["등록일시"].keys()) # 10개에 대해서만 나온다.
return render_template('corona.html', result = corona_dict, cnt = cnt)
728x90
'web' 카테고리의 다른 글
고객 정보 회원가입 로그인 창 만들기 (0) | 2022.03.11 |
---|