使用csv直接渲染到html不太合理尤其是访问量很大的时候。
现在数据库支持csv很好,为什么不先导入一下呢,然后就以数据库的方式与html模板交互。
简单做了一个,大量数据会有性能问题,看看自己用生成器改下吧
views.pyfrom __future__ import division
from flask import Flask,request,url_for,g,render_template
import csv
import os
from math import ceil
app = Flask(__name__)
@app.before_request
def load_csv():
g.path = os.path.join(os.path.abspath(os.path.dirname(__file__)),'data.csv')
@app.route('/read_csv/page/')
def read_csv(page_num):
'''
目标url
http://127.0.0.1:5000/read_csv/page/1?limit=100
'''
# 若不指定limits默认为100
limits = request.args.get('limits')
if limits:
limits = int(limits)
else:
limits=100
# 根据limits和所在页数生成数据
def show_csv(reader,page=page_num,limits=limits):
df = []
for row in reader:
if page_num*limits >= reader.line_num > (page_num-1)*limits:
df.append(row)
return df
# 计算页面数
with open(g.path,'r+') as f:
row_length = len(f.readlines())
pages = int(ceil(row_length/limits))
# 计算数据
with open(g.path,'r+') as f:
reader = csv.reader(f)
df = show_csv(reader,page_num,limits)
return render_template('main.html',df=df,pages=pages,page_num=page_num,limits=limits)
if '__main__' == __name__:
app.run(debug=True)
main.html
{% for row in df %}
{{row}}{% endfor %}