数据可视化 - Flask构建Web界面(一)
1. 库的准备
from flask import Flask, render_template # flask网页需要的库
import sqlite3 # 下面其中一个网页实例要用到的Sqlite数据库函数,与构建Flask网页没有太大关系
2. 代码实现
1. 主函数
app = Flask(__name__)
# 这一步即可完成基本网页的框架
@app.route('/') # 定义路由
def index():
return render_template("index.html") # 引入模板,index.html文件放入网页的框架代码
# 下面是完善网页的内容
# 引入数据库制作的表格
@app.route('/movie')
def movie():
# SQLite数据库的引用
movies = []
con = sqlite3.connect("movie.db")
cur = con.cursor()
sql = '''select * from movie250'''
data = cur.execute(sql)
for item in data:
movies.append(item)
cur.close()
con.close()
# render_template函数同时导入网页模板和参数(等号前后名字可以不一致,不过最好一致)
return render_template("movie.html", movies=movies)
# 引入EChart图表
@app.route('/score')
def score():
score = [] # 评分种类
number = [] # 每个评分的统计数量
con = sqlite3.connect("movie.db")
cur = con.cursor()
sql = '''select score,count(score) from movie250 group by score'''
data = cur.execute(sql)
for item in data:
score.append(str(item[0]))
number.append(item[1])
cur.close()
con.close()
return render_template("score.html", score=score, number=number)
# 以下两个具体内容略
@app.route('/word')
def word():
return render_template("word.html")
@app.route('/team')
def team():
return render_template("team.html")
# 网页的运行
if __name__ == '__main__':
app.run()
2. index.html
这个总的代码不需要自己打,可以去 https://sc.chinaz.com/ 网站上下载想要的模板,再对其中的HTML5代码进行需要的改动,一般下载的代码格式都是很不错的,分块都很明显,该自己需要的部分即可。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>豆瓣电影TOP250数据可视化</title>
<meta content="" name="descriptison">
<meta content="" name="keywords">
<!-- Favicons -->
<link href="../static/assets/img/favicon.png" rel="icon">
<link href="../static/assets/img/apple-touch-icon.png" rel="apple-touch-icon">
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Raleway:300,300i,400,400i,600,600i,700,700i,900" rel="stylesheet">
<!-- Vendor CSS Files -->
<link href="../static/assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="../static/assets/vendor/icofont/icofont.min.css" rel="stylesheet">
<link href="../static/assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
<link href="../static/assets/vendor/animate.css/animate.min.css" rel="stylesheet">
<link href="../static/assets/vendor/venobox/venobox.css" rel="stylesheet">
<link href="../static/assets/vendor/aos/aos.css" rel="stylesheet">
<!-- Template Main CSS File -->
<link href="../static/assets/css/style.css" rel="stylesheet">
<!-- =======================================================
* Template Name: Mamba - v2.0.1
* Template URL: https://bootstrapmade.com/mamba-one-page-bootstrap-template-free/
* Author: BootstrapMade.com
* License: https://bootstrapmade.com/license/
======================================================== -->
</head>
<body>
<!-- ======= Header ======= -->
<header id="header">
<div class="container">
<div class="logo float-left">
<h1 class="text-light"><a href="temp.html"><span>豆瓣电影Top250</span></a></h1>
<!-- Uncomment below if you prefer to use an image logo -->
<!-- <a href="temp.html"><img src="../static/assets/img/logo.png" alt="" class="img-fluid"></a>-->
</div>
<nav class="nav-menu float-right d-none d-lg-block">
<ul>
<li class="active"><a href="/">首页</a></li>
<li><a href="/movie">电影</a></li>
<li><a href="/score">评分</a></li>
<li><a href="/word">词云</a></li>
<li><a href="/team">团队</a></li>
</ul>
</nav><!-- .nav-menu -->
</div>
</header><!-- End Header -->
<!-- ======= Hero Section ======= -->
<section id="team" class="team">
<div class="container">
<div class="section-title">
<h2>豆瓣电影TOP250-数据分析</h2>
<p>应用python爬虫,flask框架,ECharts,WordCloud技术应用实现</p>
</div>
<!-- ======= Counts Section ======= -->
<section class="counts section-bg">
<div class="container">
<div class="row">
<div class="col-lg-3 col-md-6 text-center" data-aos="fade-up">
<a href="/movie">
<div class="count-box">
<i class="icofont-movie" style="color: #20b38e;"></i>
<span data-toggle="counter-up">250</span>
<p>经典电影</p>
</div>
</a>
</div>
<div class="col-lg-3 col-md-6 text-center" data-aos="fade-up" data-aos-delay="200">
<a href="/score">
<div class="count-box">
<i class="icofont-document-folder" style="color: #c042ff;"></i>
<span data-toggle="counter-up">1</span>
<p>评分报告</p>
</div>
</a>
</div>
<div class="col-lg-3 col-md-6 text-center" data-aos="fade-up" data-aos-delay="400">
<a href="/word">
<div class="count-box">
<i class="icofont-brand-wordpress" style="color: #46d1ff;"></i>
<span data-toggle="counter-up">5693</span>
<p>词汇统计</p>
</div>
</a>
</div>
<div class="col-lg-3 col-md-6 text-center" data-aos="fade-up" data-aos-delay="600">
<a href="/team">
<div class="count-box">
<i class="icofont-users-alt-5" style="color: #ffb459;"></i>
<span data-toggle="counter-up">5</span>
<p>团队成员</p>
</div>
</a>
</div>
</div>
</div>
</section><!-- End Counts Section -->
</div>
</section><!-- End Hero -->
<!-- ======= Footer ======= -->
<footer id="footer">
<div class="container">
<div class="copyright">
© The first program <strong><span>Douban Movie</span></strong>. All Rights Reserved
</div>
<div class="credits">
<!-- All the links in the footer should remain intact. -->
<!-- You can delete the links only if you purchased the pro version. -->
<!-- Licensing information: https://bootstrapmade.com/license/ -->
<!-- Purchase the pro version with working PHP/AJAX contact form: https://bootstrapmade.com/mamba-one-page-bootstrap-template-free/ -->
Designed by <a href="https://bootstrapmade.com/">Leric</a>
</div>
</div>
</footer><!-- End Footer -->
<!-- Vendor JS Files -->
<script src="../static/assets/vendor/jquery/jquery.min.js"></script>
<script src="../static/assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="../static/assets/vendor/jquery.easing/jquery.easing.min.js"></script>
<script src="../static/assets/vendor/php-email-form/validate.js"></script>
<script src="../static/assets/vendor/jquery-sticky/jquery.sticky.js"></script>
<script src="../static/assets/vendor/venobox/venobox.min.js"></script>
<script src="../static/assets/vendor/waypoints/jquery.waypoints.min.js"></script>
<script src="../static/assets/vendor/counterup/counterup.min.js"></script>
<script src="../static/assets/vendor/isotope-layout/isotope.pkgd.min.js"></script>
<script src="../static/assets/vendor/aos/aos.js"></script>
<!-- Template Main JS File -->
<script src="../static/assets/js/main.js"></script>
</body>
</html>
3. movie.html(数据库的引用)
总的框架和上面一样,复制粘贴即可,为了和上面做一个比较,我将所有代码放到这里。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>豆瓣电影TOP250数据可视化-电影</title>
<meta content="" name="descriptison">
<meta content="" name="keywords">
<!-- Favicons -->
<link href="../static/assets/img/favicon.png" rel="icon">
<link href="../static/assets/img/apple-touch-icon.png" rel="apple-touch-icon">
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Raleway:300,300i,400,400i,600,600i,700,700i,900" rel="stylesheet">
<!-- Vendor CSS Files -->
<link href="../static/assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="../static/assets/vendor/icofont/icofont.min.css" rel="stylesheet">
<link href="../static/assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
<link href="../static/assets/vendor/animate.css/animate.min.css" rel="stylesheet">
<link href="../static/assets/vendor/venobox/venobox.css" rel="stylesheet">
<link href="../static/assets/vendor/aos/aos.css" rel="stylesheet">
<!-- Template Main CSS File -->
<link href="../static/assets/css/style.css" rel="stylesheet">
<!-- =======================================================
* Template Name: Mamba - v2.0.1
* Template URL: https://bootstrapmade.com/mamba-one-page-bootstrap-template-free/
* Author: BootstrapMade.com
* License: https://bootstrapmade.com/license/
======================================================== -->
</head>
<body>
<!-- ======= Header ======= -->
<header id="header">
<div class="container">
<div class="logo float-left">
<h1 class="text-light"><a href="temp.html"><span>豆瓣电影Top250-电影</span></a></h1>
<!-- Uncomment below if you prefer to use an image logo -->
<!-- <a href="temp.html"><img src="../static/assets/img/logo.png" alt="" class="img-fluid"></a>-->
</div>
<nav class="nav-menu float-right d-none d-lg-block">
<ul>
<li class="active"><a href="/">首页</a></li>
<li><a href="/movie">电影</a></li>
<li><a href="/score">评分</a></li>
<li><a href="/word">词云</a></li>
<li><a href="/team">团队</a></li>
</ul>
</nav><!-- .nav-menu -->
</div>
</header><!-- End Header -->
<!-- ======= Hero Section ======= -->
<section id="team" class="team">
<div class="container">
<div class="section-title">
<h2>豆瓣电影TOP250电影</h2>
</div>
<!-- ======= Counts Section ======= -->
<section class="counts section-bg">
<div class="container">
<table class="table table-striped">
<tr>
<td>排名</td>
<td>电影中文名称</td>
<td>电影外国名称</td>
<td>评分</td>
<td>评价人数</td>
<td>概述</td>
<td>其他信息</td>
</tr>
{% for movie in movies %}<!--jinja-->
<tr>
<td>{{ movie[0] }}</td>
<td>
<a href="{{ movie[1] }}">
{{ movie[3] }}
</a>
</td>
<td>{{ movie[4] }}</td>
<td>{{ movie[5] }}</td>
<td>{{ movie[6] }}</td>
<td>{{ movie[7] }}</td>
<td>{{ movie[8] }}</td>
</tr>
{% endfor %}
</table>
</div>
</section><!-- End Counts Section -->
</div>
</section><!-- End Hero -->
<!-- ======= Footer ======= -->
<footer id="footer">
<div class="container">
<div class="copyright">
© The first program <strong><span>Douban Movie</span></strong>. All Rights Reserved
</div>
<div class="credits">
<!-- All the links in the footer should remain intact. -->
<!-- You can delete the links only if you purchased the pro version. -->
<!-- Licensing information: https://bootstrapmade.com/license/ -->
<!-- Purchase the pro version with working PHP/AJAX contact form: https://bootstrapmade.com/mamba-one-page-bootstrap-template-free/ -->
Designed by <a href="https://bootstrapmade.com/">Leric</a>
</div>
</div>
</footer><!-- End Footer -->
<!-- Vendor JS Files -->
<script src="../static/assets/vendor/jquery/jquery.min.js"></script>
<script src="../static/assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="../static/assets/vendor/jquery.easing/jquery.easing.min.js"></script>
<script src="../static/assets/vendor/php-email-form/validate.js"></script>
<script src="../static/assets/vendor/jquery-sticky/jquery.sticky.js"></script>
<script src="../static/assets/vendor/venobox/venobox.min.js"></script>
<script src="../static/assets/vendor/waypoints/jquery.waypoints.min.js"></script>
<script src="../static/assets/vendor/counterup/counterup.min.js"></script>
<script src="../static/assets/vendor/isotope-layout/isotope.pkgd.min.js"></script>
<script src="../static/assets/vendor/aos/aos.js"></script>
<!-- Template Main JS File -->
<script src="../static/assets/js/main.js"></script>
</body>
</html>
4. score.html (EChart图表)
这里只显示部分代码,位置是 Hero Section,直接替换即可。
<!-- ======= Hero Section ======= -->
<section id="team" class="team">
<div class="container">
<div class="section-title">
<h2>豆瓣电影TOP250-评分分布</h2>
</div>
<!-- ======= 图标 ======= -->
<section class="counts section-bg">
<div class="container">
<div id="main" style="width: 100%;height:300px;"></div>
<script type="text/javascript">
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
// 指定图表的配置项和数据
var option = {
color:['#3398DB'],
tooltip:{
trigger:'axis',
axisPointer:{
type:'shadow'
}
},
xAxis: {
type: 'category',
data: {{ score|tojson }}
},
yAxis: {
type: 'value'
},
series: [{
data: {{ number }},
type: 'bar',
showBackground: true,
backgroundStyle: {
color: 'rgba(180, 180, 180, 0.2)'
},
barWidth:45
}]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
</script>
</div>
</section><!-- End Counts Section -->
</div>
</section><!-- End Hero -->
3. 注意事项
1、在一开始运行flask框架的时候,他的debug model是关闭的,也就是说代码改变后,刷新网页不能随之改变,这种情况的处理如下。
点击右上角图中所示的倒三角,点击编辑配置。
在图中FLASK_DEBUG处打钩即可。
2、在更改模板代码的时候,可以在模板呈现的网页上,通过开发者工具查看不同位置的代码,在通过 Ctrl + F 在代码中查询修改相关代码,开发者工具参考 Python爬虫入门记(2)- 网络代理(伪装) 中的使用。