base.html–(父模板)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="/static/css/bootstrap.css" >
<!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
<link rel="stylesheet" href="/static/css/bootstrap-theme.css" >
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="/static/js/bootstrap.js" ></script>
<style>
#top{
margin-left:20px ;
}
</style>
{% block style %}
{% endblock %}
</head>
<body>
<h3 align="center">好家园房产中介网后台管理</h3>
<div id="top">
<span>
<a href="/">查看住房信息</a>
</span>
<span>|
<a href="/addHouse">发布住房信息</a>
</span>
<span>|
<a href="/show">汇总住房信息</a>
</span>
</div>
{% block centent %}
{% endblock %}
</body>
</html>
addHouse.html–(添加house的子模板)
{% extends "base.html" %}
{% block centent %}
<form method="post" class="form-horizontal" >
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">房型</label>
<div class="col-sm-10">
<select type="text" class="form-control" name="type" placeholder="房型">
{% for rowType in rowTypes %}
<option value="{{ rowType["HouseTypeID"]}}">{{ rowType["HouseTypeName"] }}</option>
{% endfor %}
</select>
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">面积(平方米)</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="area" placeholder="面积">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">价格(万元)</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="price" placeholder="价格">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">地址</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="address" placeholder="地址">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">描述</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="desc" placeholder="描述">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">提交</button>
</div>
</div>
</form>
{% endblock %}
index.html–(显示house的子摸版)
{% extends "base.html" %}
{% block centent %}
<table class="table table-bordered" >
<tr>
<th>编号</th>
<th>房型</th>
<th>面积</th>
<th>价格</th>
<th>地点</th>
<th>描述</th>
</tr>
{% for row in rows %}
<tr>
<td>{{ row["HouseID"] }}</td>
<td>{{ row["HouseTypeName"] }}</td>
<td>{{ row["Area"] }}</td>
<td>{{ row["Price"] }}</td>
<td>{{ row["Adress"] }}</td>
<td>{{ row["HouseDesc"] }}</td>
</tr>
{% endfor %}
</table>
{% endblock %}
show.html–(汇总的house界面)
{% extends "base.html" %}
{% block centent %}
<script src="/static/js/echarts.min.js"></script>
<div id="main" style="width: 1000px;height:500px;" ></div>
<script type="text/javascript" >
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
// 指定图表的配置项和数据
var option = {
title: {
text: '汇总房产信息显示'
},
tooltip: {},
legend: {
data: ['评论数']
},
xAxis: {
data: {{ x |safe }} {# 阻止他转义字符 #}
},
yAxis: {},
series: [
{
type: 'bar',
data: {{ y }}
}
]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
</script>
{% endblock %}
Mysql.py–(连接数据库)
import pymysql
class mysql:
def __init__(self):
self.conn=pymysql.connect(host="127.0.0.1",user="root",passwd="123456",db="test")
self.cursor=self.conn.cursor(pymysql.cursors.DictCursor)
def update(self,sql,args=None):
# sql="delete from goods where goodsid=%s"
result=self.cursor.execute(sql,args)
if result>0:
self.conn.commit() #提交
return True
else:
return False
def close(self):
self.conn.close()
app.py–(项目启动的视图函数界面)
from flask import Flask,render_template,request,redirect
from Mysql import mysql
import pandas as pd
app = Flask(__name__)
@app.route('/')
def index():
db = mysql()
sql="select * from houseTypes a,houses b where a.HouseTypeID=b.HouseTypeID order by houseid"
db.cursor.execute(sql)
rows = db.cursor.fetchall()
return render_template("index.html", rows=rows)
@app.route('/addHouse',methods=["post","get"])
def addHouse():
if request.method == "GET":
db = mysql()
sql = "select * from housetypes"
db.cursor.execute(sql)
rowTypes = db.cursor.fetchall()
return render_template("addHouse.html", rowTypes=rowTypes)
else:
typeid = request.form.get("type")
area = request.form.get("area")
price = request.form.get("price")
address = request.form.get("address")
desc = request.form.get("desc")
sql = "insert into houses values(null,%s,%s,%s,%s,%s)"
mysql().update(sql, [typeid, area, price, address, desc])
return redirect("/")
@app.route('/show')
def show():
db=mysql()
sql = "select * from houseTypes a,houses b where a.HouseTypeID=b.HouseTypeID "
db.cursor.execute(sql)
rows = db.cursor.fetchall()
df=pd.DataFrame(rows)
newDf=df.groupby("HouseTypeName").count()["HouseID"].sort_values(ascending=False).head()
return render_template("show.html",x=list(newDf.index),y=list(newDf.values))
if __name__ == '__main__':
app.run()