根据上一篇继续
http://blog.csdn.net/xx123er/article/details/77945591
3.flask脚本的编写
from
flask
import
Flask, jsonify, render_template, request,redirect,url_for
# import forms
import
json
import
data_view
app = Flask(__name__)
# app.jinja_env.add_extension("chartkick.ext.charts")
@
app.route
(
'/'
,
methods
=[
'GET'
,
'POST'
])
@
app.route
(
'/index'
,
methods
=[
'GET'
,
'POST'
])
def
index():
if
request.method ==
'GET'
:
view=data_view.viewdata()
view.getmethod()
time1=
"2017-9-5"
time2=
"2017-9-8"
if
request.method ==
'POST'
:
time1 = request.form.get(
'start_time'
,
180
)
time2 = request.form.get(
'end_time'
,
180
)
col_name = request.form.get(
'col_name'
)
view = data_view.viewdata()
view.postmethod(time1,time2,col_name)
return
render_template(
'highcharts_line_labels.html'
,
tit
=view.title,
xax
=view.xAxis,
yax
=view.yAxis,
seri
=view.series,
start_time
=time1,
end_time
=time2)
if
__name__ ==
"__main__"
:
app.run(
debug
=
True
,
host
=
'0.0.0.0'
,
port
=
5000
)
4..数据组装模块data_view.py
import
database_csv
import
sys
class
viewdata():
def
__init__
(
self
):
self
.title={}
self
.title[
"style"
]={}
self
.title[
"style"
][
"color"
]=
"#ff0000"
self
.xAxis={}
self
.yAxis={}
self
.yAxis[
"title"
] = {}
self
.series=[]
def
getmethod(
self
):
self
.title[
"text"
]=
'rsp'
self
.xAxis[
"categories"
]=[
'Jan'
,
'Feb'
,
'Mar'
,
'Apr'
,
'May'
,
'Jun'
,
'Jul'
,
'Aug'
,
'Sep'
,
'Oct'
,
'Nov'
,
'Dec'
]
self
.yAxis[
"title"
][
"text"
]=
'num'
y1 = {}
y1[
"name"
] =
'Tokyo'
y1[
"data"
] = [
3.9
,
4.2
,
5.7
,
8.5
,
11.9
,
15.2
,
17.0
,
16.6
,
14.2
,
10.3
,
6.6
,
4.8
]
self
.series.append(y1)
def
postmethod(
self
,start,end,
colnu
):
print
colnu
file,columns,y_txt=
self
.co2columns(
str
(
colnu
))
database = database_csv.csv_data(file)
database.select_bytime(
str
(start),
str
(end))
data = database.select_col(columns)
# print data
self
.title[
"text"
] =
str
(
colnu
)
self
.xAxis[
"categories"
] = [
str
(i)
for
i
in
data[
"Time"
]]
self
.yAxis[
"title"
][
"text"
] = y_txt
for
e
in
columns:
y2 = {}
y2[
"name"
] = e.split(
"_"
)[-
1
]
y2[
"data"
] = [val
for
val
in
data[e]]
self
.series.append(y2)
def
co2columns(
self
,co):
if
co==
"toatal_cost"
:
file=
"stress.csv"
columns = [
"toa_min"
,
"toa_max"
,
"toa_aver"
,
"toa_80"
,
"toa_90"
,
"toa_95"
]
ytxt=
"~ second"
return
file,columns,ytxt