728x90
내일배움캠프 4기_프로젝트 개인공부, 4주차 WIL
Bootstrap Pagination in Flask
100개의 리스트를 다음과 같이 표시!
per_page 지정 페이지당 몇개의 게시물을 보여줄지 결정!
📄 app.py
from flask import Flask, render_template
from flask_paginate import Pagination, get_page_args
app = Flask(__name__)
app.template_folder = 'templates'
# generating data for pagination
users = list(range(100))
def get_users(offset, per_page):
return users[offset: offset+per_page]
@app.route('/')
def index():
page, per_page, offset = get_page_args(page_parameter="page", per_page_parameter="per_page")
total = len(users)
pagination_users = get_users(offset=offset, per_page=per_page)
pagination = Pagination(page=page, per_page=per_page,
total=total, css_framework='bootstrap4')
return render_template('index.html',
users=pagination_users,
page=page,
per_page=per_page,
pagination=pagination)
if __name__ == "__main__":
app.run(debug=True)
📄 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
<title>flask bootstrap pagination</title>
</head>
<body>
<h1>Bootstrap Pagination in Flask</h1>
<div class="container">
<!-- {{pagination.links}}-->
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>
#
</th>
<th>
Value
</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>
{{loop.index + (page-1) * per_page}}
</td>
<td>
{{user}}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{{pagination.links}}
</div>
</body>
</html>
📄 mySQL db list
users list를 mySQL 데이터로 가져오면 되는 것 같다.
@app.route('/images', defaults={'page':1})
@app.route('/images/page/<int:page>')
def abc(page):
perpage=20
startat=page*perpage
db = mysql.connect('localhost', 'root', 'password', 'img')
cursor = db.cursor()
cursor.execute('SELECT Id,Title,Img FROM image limit %s, %s;', (startat,perpage))
data = list(cursor.fetchall())
한 페이지에 20개의 데이터를 가져오는 코드.
flask logging system
import logging
logging.basicConfig(
level=logging.WARNING,
format="{asctime} {levelname:<8} {message}",
style='{',
filename='mylog.log',
filemode='w'
)
logging.debug("This is a debug msg")
logging.info("This is an info msg")
logging.warning("This is a warning msg")
logging.error("This is an error msg")
logging.critical("This is a critical msg")
level이 낮은 debug로 설정할 경우, 모든 log남길 수 있음
지금은 코드는 warning으로 설정해, warning보다 낮은 레벨인 debug, info 은 기록(log)이 남지 않는다.
모든 기록을 보는게 비효율적이므로 원하는 로그만 볼 수 있게끔 레벨 설정!
에러메세지를 다 남기는게 아니라, 원하는 에러에 대한 기록만 볼 수 있게끔.
서버 용량이 제한적이므로 원하는 텍스트만 log에 담아 남기게 하는 시스템.
서버는 24시간 돌아야하는데, 사람은 이 기록을 실시간으로 보기 힘듦.
'WIL' 카테고리의 다른 글
내일배움캠프 4기_Node 숙련, 6주차 WIL (0) | 2022.12.26 |
---|---|
내일배움캠프 4기_Node 입문, 5주차 WIL (0) | 2022.12.18 |
내일배움캠프 4기_개인진도&두번째 프로젝트 시작, 3주차 WIL (0) | 2022.12.04 |
내일배움캠프 4기_개인진도 2주차 WIL (0) | 2022.11.27 |
내일배움캠프 4기_미니프로젝트 1주차 WIL (0) | 2022.11.20 |