windows python flask读取文件数据并返回表格

windows python flask读取文件数据并返回表格

tags: flask

在这里插入图片描述

在这里插入图片描述

data/puuv.txt

日期  	PV	UV
2020/7/18	15000	150
2020/7/19	15001	151
2020/7/20	15002	152
2020/7/21	15003	153
2020/7/22	15004	154
2020/7/23	15005	155
2020/7/24	15006	156
2020/7/25	15007	157
2020/7/26	15008	158
2020/7/27	15009	159
2020/7/28	15010	160
2020/7/29	15011	161
2020/7/30	15012	162
2020/7/31	15013	163
2020/8/1	15014	164
2020/8/2	15015	165
2020/8/3	15016	166
2020/8/4	15017	167
2020/8/5	15018	168
2020/8/6	15019	169
2020/8/7	15020	170

app.py

from flask import Flask, render_template, request

app = Flask(__name__)


@app.route("/pvuv")
def pvuv():
    # read file
    data = []
    with open("./data/pvuv.txt") as fin:
        is_first_line =True
        for line in fin:
            if is_first_line:
                is_first_line = False
                continue
            line = line[:-1] #\n
            pdate, pv, uv = line.split("\t")
            data.append((pdate, pv, uv))
    return render_template("pvuv.html", data=data)

templates/pvuv.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>网站的pvuv数据</title>
</head>
<body>
<h1>网站的PVUV的数据表格<h1>
    <div>
        <table border = "1px">
            <tr style="background: #ee0000">
                <th>日期</th>
                <th>PV</th>
                <th>UV</th>
            </tr>
            {%  for row in data %}
            <tr>
                <th>{{ row[0] }}</th>
                <th>{{ row[1] }}</th>
                <th>{{ row[2] }}</th>
            </tr>
            {% endfor %}
        </table>
    </div>>

</body>
</html>

访问界面:
在这里插入图片描述
更多阅读:

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ghostwritten

口渴,请赏一杯下午茶吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值