0


python 如何使用 pandas 在 flask web 网页中分页显示 csv 文件数据

一、实战场景

python 如何使用 pandas 在 flask web 网页中分页显示 csv 文件数据

二、知识点

python 基础语法

python 文件读写

python 分页

pandas 数据处理

flask web 框架

jinja 模版

三、菜鸟实战

初始化 Flask 框架,设置路由

  1. '''
  2. Description: 分页读取并显示 csv 文件数据
  3. '''
  4. from math import ceil
  5. import csv
  6. from flask import Flask, render_template, request, redirect
  7. from spiders.file_manager import FileManager
  8. # 初始化框架
  9. web = Flask(__name__)
  10. @web.route('/')
  11. def index():
  12. # 首页
  13. return redirect('/table/1')
  14. @web.route('/table/<int:page_num>')
  15. def table(page_num):
  16. # 分页读取并显示 csv 文件数据
  17. file_manager = FileManager()
  18. file = file_manager.get_data_file_path("tao365_detail.csv")
  19. # 若不指定limits默认为 5
  20. limits = request.args.get('limits', 5, type=int)
  21. def show_csv(reader, page=page_num, limits=limits):
  22. # 内部函数,根据limits和所在页数生成数据
  23. df = []
  24. for row in reader:
  25. if page_num * limits >= (reader.line_num - 1) > (page_num - 1) * limits:
  26. df.append(row)
  27. return df
  28. with open(file, 'r+', encoding='utf-8') as f:
  29. # 计算页面数
  30. row_length = len(f.readlines()) - 1
  31. pages = int(ceil(row_length / limits))
  32. with open(file, 'r+', encoding='utf-8') as f:
  33. # 计算数据
  34. reader = csv.reader(f)
  35. next(reader)
  36. df = show_csv(reader, page_num, limits)
  37. # 加载模版和模版数据
  38. return render_template('table.html', df=df, pages=pages, page_num=page_num, limits=limits)
  39. @web.route('/table_detail')
  40. def table_detail():
  41. # 二手房详情
  42. row = request.args.get('row').split(',')
  43. return render_template('table_detail.html', row=row)
  44. # 启动 flask web 框架
  45. web.run(debug=True)

jinja 模版 渲染列表数据

  1. <section id="team" class="header">
  2. <div class="container">
  3. <div class="section-title">
  4. <h2> pandas 在网页中分页显示 csv 文件</h2>
  5. <p>使用 Python、pandas、bootstrap、flask 框架等技术实现</p>
  6. </div>
  7. <!-- ======= 预览板块 ======= -->
  8. <section class="counts section-bg">
  9. <div class="container">
  10. <div class="row">
  11. <div class="col">
  12. <!-- ======= 使用表格循环展示数据 ======= -->
  13. <table class="table table-striped table-hover" style="">
  14. <tbody>
  15. <tr>
  16. <th>标题</th>
  17. <th>价格</th>
  18. <th>每平方价格</th>
  19. <!-- <th>小区</th> -->
  20. <!-- <th>地址</th> -->
  21. <th>房屋户型</th>
  22. <th>建筑面积</th>
  23. <th>所在楼层</th>
  24. <th>房屋朝向</th>
  25. <th>建筑年代</th>
  26. <th>详情</th>
  27. </tr>
  28. {% for row in df %}
  29. <!-- <li>{{row}}</li> -->
  30. <tr class="alt">
  31. {% for subrow in row %} {% if loop.index != 5 and loop.index != 4 %}
  32. <td>{{subrow}}</td>
  33. {% endif %} {% endfor %}
  34. <td><a class="link-table" data-row="{{row}}" href="/table_detail">点击查看</a> </td>
  35. </tr>
  36. {% endfor %}
  37. </tbody>
  38. </table>
  39. </div>
  40. </div>
  41. <div id="demo" style="display: flex;justify-content: center;"></div>
  42. </div>
  43. </section>
  44. <!-- End Counts Section -->
  45. </div>
  46. </section>

分页请求数据

  1. $(document).ready(function() {
  2. $('.link-table').each(function() {
  3. var row = $(this).attr('data-row')
  4. var row1 = eval('(' + row + ')').join(',')
  5. $(this).attr('href', '/table_detail?row=' + row1)
  6. })
  7. layui.use(['laypage', 'layer'], function() {
  8. var laypage = layui.laypage,
  9. layer = layui.layer;
  10. laypage.render({
  11. elem: 'demo',
  12. count: "{{pages}}",
  13. curr: "{{ page_num }}",
  14. theme: '#587187',
  15. // limit: pageSize //一页数据
  16. jump: function(obj, first) {
  17. console.log(obj.curr, first)
  18. if (!first) {
  19. window.location.href = "/table/" + obj.curr; //跳转链接
  20. }
  21. }
  22. });
  23. });
  24. })

显示详情页数据示例

  1. <section id="team" class="header">
  2. <div class="container">
  3. <div class="section-title">
  4. <h2>pandas 在网页中分页显示 csv 文件 - 详情页数据示例</h2>
  5. <p>使用 Python、pandas、bootstrap、flask 框架等技术实现</p>
  6. </div>
  7. <!-- ======= 预览板块 ======= -->
  8. <section class="counts section-bg">
  9. <div class="container">
  10. <div class="detail__mainCotetnL fl">
  11. <table class="table table-striped table-hover" style="">
  12. <tbody>
  13. <tr>
  14. <td>标题</td>
  15. <td colspan="3">{{row[0]}}</td>
  16. </tr>
  17. <tr>
  18. <td>价格</td>
  19. <td>{{row[1]}}</td>
  20. <td>每平方价格</td>
  21. <td>{{row[2]}}</td>
  22. </tr>
  23. <tr>
  24. <td>房屋户型</td>
  25. <td>{{row[5]}}</td>
  26. <td>建筑面积</td>
  27. <td>{{row[6]}}</td>
  28. </tr>
  29. <tr>
  30. <td>所在楼层</td>
  31. <td>{{row[7]}}</td>
  32. <td>房屋朝向</td>
  33. <td>{{row[8]}}</td>
  34. </tr>
  35. <tr>
  36. <td>建筑年代</td>
  37. <td>{{row[9]}}</td>
  38. <td></td>
  39. <td></td>
  40. </tr>
  41. </tbody>
  42. </table>
  43. </div>
  44. </div>
  45. </section>
  46. <!-- End Counts Section -->
  47. </div>
  48. </section>

运行结果

运行截图

浏览器中打开 http://127.0.0.1:5000

列表页数据示例

详情页数据示例

资源链接

https://download.csdn.net/download/qq_39816613/87374650

菜鸟实战,持续学习!

标签: python flask pandas

本文转载自: https://blog.csdn.net/qq_39816613/article/details/128592794
版权归原作者 菜鸟实战 所有, 如有侵权,请联系我们删除。

“python 如何使用 pandas 在 flask web 网页中分页显示 csv 文件数据”的评论:

还没有评论