(3-5)绘制散点图和折线图:Flask+pygal+SQLite实现数据分析

3.5  Flask+pygal+SQLite实现数据分析

在本节的内容中,将使用Flask+pygal+SQLite3实现数据分析功能。将需要分析的数据保存在SQLite3数据库中,然后在Flask Web网页中使用库pygal绘制出对应的统计图。

3.5.1  创建数据库

首先使用PyCharm创建一个Flask Web项目,然后通过文件models.py设计SQLite数据库的结构,主要实现代码如下所示。

from dbconnect import db

# 许可证申请数量
class Appinfo(db.Model):
    __tablename__ = 'appinfo'
    # 注意这句,网上有些实例上并没有
    # 必须设置主键
    id = db.Column(db.Integer, primary_key=True)
    year = db.Column(db.String(20))
    month = db.Column(db.String(20))
    cnt = db.Column(db.String(20))

    def __init__(self, year, month, cnt):
        self.year = year
        self.month = month
        self.cnt = cnt

    def __str__(self):
        return self.year + ":" + self.month + ":" + self.cnt

    def __repr__(self):
        return self.year + ":" + self.month + ":" + self.cnt

    def save(self):
        db.session.add(self)
        db.session.commit()

在数据库表appinfo中添加数据,如图3-39所示。

图3-39  数据库dzj.db中的数据

3.5.2  绘制统计图

编写Flask Web启动文件pygal_test.py,首先建立URL路径导航指向模板文件index.htm,然后提取数据库中的数据,并使用pygal绘制出统计图表。文件pygal_test.py的主要实现代码如下所示。

app = Flask(__name__)
dbpath = app.root_path
# 注意斜线的方向
app.config['SQLALCHEMY_DATABASE_URI'] = r'sqlite:///' + dbpath + '/dzj.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
print(app.config['SQLALCHEMY_DATABASE_URI'])

db.init_app(app)

@app.route('/')
def APPLYTBLINFO():
    db.create_all()
    # 在第一次调用时执行就可以
    appinfos = Appinfo.query.all()
    # 选择年份
    list_year = []
    # 选择月份
    list_month = []
    # 月份对应的数字
    map_cnt = {}
    for info in appinfos:
        if info.year not in list_year:
            list_year.append(info.year)
            map_cnt[info.year] = [int(info.cnt)]
        else:
            map_cnt[info.year].append(int(info.cnt))
        if info.month not in list_month:
            list_month.append(info.month)
    line_chart = pygal.Line()
    line_chart.title = '信息'
    line_chart.x_labels = map(str, list_month)
    for year in list_year:
        line_chart.add(str(year) + "年", map_cnt[year])
    return render_template('index.html', chart=line_chart)


if __name__ == '__main__':
    app.run(debug=True)

模板文件index.htm的具体实现代码如下所示。

<body style="width: 1000px;margin: auto">
<div  id="container">
    <div id="header" style="background: burlywood;height: 50px;">
        <h2 style="font-size: 30px;  position: absolute; margin-top: 10px;margin-left: 300px;
        text-align:center;">数据走势图分析</h2>
    </div>
    <div id="leftbar" style="width: 200px;height: 600px;background: cadetblue;float: left">
        <h2 style="margin-left: 20px">数据图总览</h2><br/>
        <table>
            <tr>
                <td>
                   <a name="appinfo"  style="margin-left: 20px;">数量分析图</a><br>
                </td>
            </tr>
        </table>
    </div>
    <div id="chart" style="width: 800px;float: left">
       <embed type="image/svg+xml" src= {{ chart.render_data_uri()|safe }} />
    </div>
</div>
</body>

执行Flask Web项目,在浏览器中输入http://127.0.0.1:5000/后会显示绘制的统计图,执行效果如图3-40所示。

图3-40  绘制的统计图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

码农三叔

感谢鼓励

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

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

打赏作者

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

抵扣说明:

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

余额充值