-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflaskPy.py
279 lines (220 loc) · 8.48 KB
/
flaskPy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from flask import Flask, render_template, request, session
import os
import json
import flask
import pymysql as mysql
import pandas as pd
import requests
from bs4 import BeautifulSoup
from pyecharts import options as opts
from pyecharts.charts import Bar, Line, Pie, Map
app = Flask(__name__)
app.config['SECRET_KEY'] = os.urandom(24)
db = mysql.connect(host='127.0.0.1', user='root', password='123456', db='abc', port=3306, charset='utf8')
cur = db.cursor()
@app.route('/') # 路由,让前端执行后端路径
def index():
return render_template("index.html")
@app.route('/login')
def login():
return render_template("login.html")
@app.route('/regin')
def regin():
return render_template("register.html")
@app.route('/logininfo', methods=['GET', 'POST'])
def logininfo():
name = request.values.get("usrname")
pwd = request.values.get("usrpwd")
session["usr"] = ''
session["msg"] = ''
cur.execute("select count(*) num from login where name='{}' and pwd='{}'".format(name, pwd))
result = cur.fetchall()
if result[0][0] > 0:
session["usr"] = name
return render_template('index.html')
else:
session['msg'] = '用户名或密码有误'
return render_template('login.html')
@app.route('/regininfo', methods=['GET', 'POST'])
def regininfo():
name = request.values.get("usrname")
pwd = request.values.get("usrpwd")
tel = request.values.get("usrtel")
mail = request.values.get("usrmail")
gender = request.values.get("usrgen")
cur.execute("insert into login values(null,'{}','{}','{}','{}','{}')".format(name, pwd, tel, mail, gender))
db.commit()
return render_template("login.html")
@app.route("/prov", methods=["GET"]) # 查询省
def prov():
cur.execute("select * from province") # id, name
data = cur.fetchall() # 列表套元组
arr = []
for v in data:
arr.append({
"id": v[0],
"pname": v[1]
})
# obj={}
re = flask.Response(json.dumps({"data": arr}))
return re
@app.route("/city", methods=["GET"]) # 查询市
def city():
id = request.values.get("pid")
cur.execute(
"select * from city where province_id={}".format(int(id))) # id,city_index没用 不要了,province_id外键,name, cityname
data = cur.fetchall()
arr = []
for v in data:
# obj["city_index"]=v[1]
arr.append({
"id": v[0],
"pid": v[2],
"cname": v[3],
"cityname": v[4],
})
re = flask.Response(json.dumps(arr))
return re
@app.route('/news')
def news():
return render_template("new.html")
@app.route('/yubao') # 跳转天气预报
def yubao():
return render_template("yubao.html")
@app.route('/aqi') # 跳转数据分析
def aqi():
return render_template("aqi.html")
@app.route("/wear", methods=["GET"]) # 查询天气,现爬,把整个标签传过去
def wear():
city = request.values.get("city")
req = requests.get("http://www.tianqihoubao.com/yubao/" + city + ".html")
txt = BeautifulSoup(req.text, "lxml")
table = txt.find("table")
table = str(table).replace("/legend", "http://www.tianqihoubao.com/legend")
re = flask.Response(json.dumps({"data": table}))
return re
@app.route("/kqzl", methods=["GET"]) # 查询空气质量
def kqzl():
city = request.values.get("city")
year = request.values.get("year")
file_name = "./" + city + "-" + year + ".csv"
if not os.path.exists(file_name): # 找不到对应数据文件,直接返回
json1 = {"msg": "无该城市数据"}
return flask.Response(json.dumps(json1))
columns = ["date", "kqzl", "aqi", "pm25", "pm10", "so2", "no2", "co", "o3"]
df = pd.read_csv(file_name, encoding="gbk", names=columns) # 读csv
arr = []
for idx, row in df.iterrows(): # 迭代函数
dic = {}
for col in columns:
dic[col] = row[col]
arr.append(dic)
json1 = {"data": arr, "msg": ""} # 看数组有没有传过来,搞个空字符串
re = flask.Response(json.dumps(json1)) # 拼好了传回去
return re
def df_month(x):
return x.month
@app.route("/xxt", methods=["GET"]) # 线性图页面
def xxt():
return render_template("xxt.html")
@app.route("/xxechart", methods=["GET"]) # 线性图表
def xxechart():
city = request.values.get("city")
year = request.values.get("year")
file_name = "./" + city + "-" + year + ".csv"
if not os.path.exists(file_name): # 找不到对应数据文件,直接返回
json1 = {"msg": "无该城市数据"}
return flask.Response(json.dumps(json1))
columns = ["date", "kqzl", "aqi", "pm25", "pm10", "so2", "no2", "co", "o3"]
df = pd.read_csv(file_name, encoding="gbk", names=columns) # 读csv
date = pd.to_datetime(df["date"], format="%Y-%m-%d")
df.index = date
m = df.groupby(df_month)
y = m["aqi"].mean().round(2)
y1 = m['pm10'].mean().round(2)
y2 = m['pm25'].mean().round(2)
x = [str(i) + "月" for i in range(1, 13)]
bar = (
Bar()
.add_xaxis(x)
.add_yaxis("pm10", list(y1), bar_width=25)
.add_yaxis("pm25", list(y2), bar_width=25)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="每月aqi、pm均值数据分析")
)
)
line = Line().add_xaxis(x).add_yaxis("平均aqi", list(y))
bar.overlap(line)
return bar.dump_options_with_quotes()
@app.route("/bzt", methods=["GET"])
def bzt():
return render_template("bzt.html")
@app.route("/bzchart", methods=["GET"])
def bzchart():
city = request.values.get("city")
year = request.values.get("year")
file_name = "./" + city + "-" + year + ".csv"
if not os.path.exists(file_name): # 找不到对应数据文件,直接返回
json1 = {"msg": "无该城市数据"}
return flask.Response(json.dumps(json1))
columns = ["date", "kqzl", "aqi", "pm25", "pm10", "so2", "no2", "co", "o3"]
df = pd.read_csv(file_name, encoding="gbk", names=columns) # 读csv
data = df["kqzl"].value_counts()
x = data.index
y = data.values
arr = [int(v) for v in y]
c = (
Pie()
.add("空气质量",
[list(z) for z in zip(list(x), arr)],
center=["50%", "60%"],
radius=["40%", "55%"]
)
.set_global_opts(
title_opts=opts.TitleOpts(title="全年空气质量总和数据分析", pos_left="center", pos_top="50px"),
legend_opts=opts.LegendOpts(pos_left="center"),
)
)
return c.dump_options_with_quotes()
@app.route("/geot", methods=["GET"])
def geot():
return render_template("geot.html")
@app.route("/geochart", methods=["GET"])
def geochart():
year = request.values.get("year")
columns = ["date", "kqzl", "aqi", "pm25", "pm10", "so2", "no2", "co", "o3"]
provinces = ["北京", "成都", "重庆", "上海", "哈尔滨", "天津"]
province_en = ["beijing", "chengdu", "chongqing", "shanghai", "haerbin", "tianjin"]
data = []
for prov in province_en:
file_name = "./" + prov + "-" + year + ".csv"
if not os.path.exists(file_name): # 找不到对应数据文件,直接返回
data.append(0)
else:
df = pd.read_csv(file_name, encoding="gbk", names=columns) # 读csv
data.append(int(df['kqzl'].value_counts()['优']))
c = (
Map()
.add("空气质量", [list(z) for z in zip(provinces, data)], "china")
.set_global_opts(
title_opts=opts.TitleOpts(title="空气质量均值"),
visualmap_opts=opts.VisualMapOpts(max_=200),
)
)
print(provinces, data)
return c.dump_options_with_quotes()
@app.route("/resuser", methods=["GET"])
def resuser():
user = request.values.get("user")
cur.execute("select count(*) num from login where user='{}'".format(user))
data = cur.fetchall()
josn = {}
if data[0][0] > 0:
josn = {"msg": "该用户名已被注册", "flg": False}
else:
josn = {"msg": "用户名可以使用", "flg": True}
re = flask.Response(json.dumps(josn))
return re
if __name__ == '__main__':
app.run()