webpy没有分页类。按照php的思路。自己编了一个。数据库用的是sqlite。
class Page(object):
'''分页类'''
def __init__(self,page_size,data_count,page_current):
import math
self.size = page_size
self.data_count = data_count
self.page_current = int(page_current)
self.page_max = int(math.ceil(self.data_count * 0.1 * 10 / self.size )) self.page_current = 1 if self.page_current < 1 else self.page_current
self.page_current = self.page_max if self.page_current > self.page_max else self.page_current self.offset = ( self.page_current - 1) * self.size
def set_url(self,url):
if "?" in url:
f_url = url.split("page")[0]
if "&" in url:
f_url += "&"
else:
f_url = url + "?"
self.url = f_url
def get_html(self):
self.page_pre = self.page_current - 1
self.page_next = self.page_current + 1
if self.page_max in (0,1) :
html = u'''
<span>首页</span>
<span>上一页</span>
<span>下一页</span>
<span>尾页</span>
'''
elif self.page_current <= 1:
html = u'''
<span>首页</span>
<span>上一页</span>
<span><a href="{self.url}page={self.page_next}">下一页</a></span>
<span><a href="{self.url}page={self.page_max}">尾页</a></span>
'''.format(self=self)
elif self.page_current >= self.page_max:
html = u'''
<span><a href="{self.url}page=1">首页</a></span>
<span><a href="{self.url}page={self.page_pre}">上一页</a></span>
<span>下一页</span>
<span>尾页</span>
'''.format(self=self)
else:
html = u'''
<span><a href="{self.url}page=1">首页</a></span>
<span><a href="{self.url}page={self.page_pre}">上一页</a></span>
<span><a href="{self.url}page={self.page_next}">下一页</a></span>
<span><a href="{self.url}page={self.page_max}">尾页</a></span>
'''.format(self=self)
banner = u'''第<span id="spanPageNum">{self.page_current}</span>页/共<span id="spanTotalPage">{self.page_max}</span>页'''.format(self=self)
html = "<div>%s</div>"%(html + banner) if self.data_count > self.size:
return html
else:
return ""
调用:
page_current = i.get("page",1) page_size = 5 #每页显示几条数据
data_count = (db.select("messages",what="count(content) c")[0]["c"]) #总数据量 page = conf.Page(page_size,data_count,page_current)
page.set_url(web.ctx.fullpath)
page_html = page.get_html() data = db.select("messages",order="datetime desc",limit=page.size,offset=page.offset)
上传文件类:
class Upload(object):
u'''文件上传类,接受excel,csv文件''' def __init__(self,upfile):
import os
self.file = upfile
self.file_ext = upfile.filename.split(".")[-1]
self.file_name = upfile.filename.split(".")[-2]
#self.file_path = upfile.filename.replace('\\','/')
def save(self):
'''将文件存入服务器文件夹'''
import os,time
if not self.file_ext in ("xlsx","xls"):
return u"文件类型错误"
try:
os.mkdir(r"static/upload/file_dir/")
save_path = r"static/upload/file_dir/"
except:
save_path = r"static/upload/file_dir/" now = str(time.time()).split(".")[0] try:
with open(save_path+"%s"%(now+"."+self.file_ext),"wb") as f:
f.write(self.file.file.read())
self.filepath = save_path+"%s"%(now+"."+self.file_ext)
except:
pass def get_data(self):
'''返回数据'''
import os
# try:
# import openpyxl
# wb = openpyxl.load_workbook(self.filepath)
# ws = wb.get_sheet_by_name(wb.get_sheet_names()[0])
# data = [[j.value for j in i] for i in ws.rows[2:]]
# return data
# except:
# return "openpyxl is not exists!"
try:
import xlrd
wb = xlrd.open_workbook(self.filepath)
ws = wb.sheet_by_index(0)
data = [ws.row_values(i) for i in range(ws.nrows)][2:]
return data
except:
return "xlrd is not exists!"
finally:
os.remove(self.filepath)
class Upload(object):
u'''文件上传类,接受excel文件''' def __init__(self,upfile):
import os
self.file = upfile
self.file_ext = upfile.filename.split(".")[-1]
self.file_name = upfile.filename.split(".")[-2]
#self.file_path = upfile.filename.replace('\\','/')
def save(self):
'''将文件存入服务器文件夹'''
import os,time
if not self.file_ext in ("xlsx","xls"):
return u"文件类型错误"
try:
os.mkdir(r"static/upload/file_dir/")
save_path = r"static/upload/file_dir/"
except:
save_path = r"static/upload/file_dir/" now = str(time.time()).split(".")[0] try:
with open(save_path+"%s"%(now+"."+self.file_ext),"wb") as f:
f.write(self.file.file.read())
self.filepath = save_path+"%s"%(now+"."+self.file_ext)
except:
pass def get_data(self):
'''返回数据'''
import os
# try:
# import openpyxl
# wb = openpyxl.load_workbook(self.filepath)
# ws = wb.get_sheet_by_name(wb.get_sheet_names()[0])
# data = [[j.value for j in i] for i in ws.rows[2:]]
# return data
# except:
# return "openpyxl is not exists!"
try:
import xlrd
wb = xlrd.open_workbook(self.filepath)
ws = wb.sheet_by_index(0)
data = [ws.row_values(i) for i in range(ws.nrows)][2:]
return data
except:
return "xlrd is not exists!"
finally:
os.remove(self.filepath)