复制
收藏
提问
全网
#!/usr/bin/env python # -*- encode: utf-8 -*- from flask import Flask, request, send_from_directory from jinja2 import Environment, FileSystemLoader app = Flask(__name__) env = Environment(loader=FileSystemLoader('./templates')) env.filters['len'] = len def tac(): tacs = [] with open('/Users/wuailun/Downloads/imei_mg/full_info.log', 'r', encoding='utf-8') as f: for line in f: line = line.strip() line = eval(line) tac = line.get('TAC') model = line.get('MODEL').replace(' ', '-') brand = line.get('BRAND').replace(' ', '-') t = line.get('Device type') tacs.append([tac, model, brand, t, f'images/{brand.lower()}-{model.lower()}.jpg']) return tacs @app.route('/') def index(): data = tac() page = int(request.args.get('page', 1)) current_page = page template = env.get_template('t.html') start_index = (page - 1) * 10 end_index = start_index + 10 paginated_data = data[start_index:end_index] return template.render(data=paginated_data, current_page=current_page) @app.route('/images/<path:image_name>') def get_image(image_name): image_directory = '/Users/wuailun/Downloads/imei_mg/pic/' # 图片所在目录的相对路径 return send_from_directory(image_directory, image_name) if __name__ == '__main__': app.run()
1个月前
#!/usr/bin/env python
# -*- encode: utf-8 -*-
from flask import Flask, request, send_from_directory
from jinja2 import Environment, FileSystemLoader
app = Flask(__name__)
env = Environment(loader=FileSystemLoader('./templates'))
env.filters['len'] = len
def tac():
tacs = []
with open('/Users/wuailun/Downloads/imei_mg/full_info.log', 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
line = eval(line)
tac = line.get('TAC')
model = line.get('MODEL').replace(' ', '-')
brand = line.get('BRAND').replace(' ', '-')
t = line.get('Device type')
tacs.append([tac, model, brand, t, f'images/{brand.lower()}-{model.lower()}.jpg'])
return tacs
@app.route('/')
def index():
data = tac()
page = int(request.args.get('page', 1))
current_page = page
template = env.get_template('t.html')
start_index = (page - 1) * 10
end_index = start_index + 10
paginated_data = data[start_index:end_index]
return template.render(data=paginated_data, current_page=current_page)
@app.route('/images/<path:image_name>')
def get_image(image_name):
image_directory = '/Users/wuailun/Downloads/imei_mg/pic/' # 图片所在目录的相对路径
return send_from_directory(image_directory, image_name)
if __name__ == '__main__':
app.run()
解释
- Flask应用初始化:创建Flask应用实例,并配置Jinja2模板引擎。
- 数据处理函数:
tac()
函数读取日志文件并解析数据,生成设备信息列表。 - 主页路由:
index()
函数处理主页请求,分页显示设备信息。 - **图片路由
您已终止本次搜索,可编辑问题后重新提问。
更专业一些