作為數(shù)據(jù)分析師,我們大部分時間做的事情都是搭建線下Excel報表,這既有優(yōu)點也有缺點
優(yōu)點是:
- 開發(fā)效率
快速建模,最快十分鐘就可以建模 - 數(shù)據(jù)傳播
便于傳播,發(fā)文件就是發(fā)模型 - 交互友好
對使用者門檻低,便于修改
缺點也有:
- 版本控制
文件副本太多,極難做版本控制.經(jīng)常有人找我修改模型卻發(fā)現(xiàn)我已經(jīng)更新了,只是沒有給他最新版本 - 平臺限制
無法跨平臺,Mac不能用,WPS不能用,Excel2010及以下版本不能用 - 靜態(tài)數(shù)據(jù)
更新數(shù)據(jù)有門檻,必須Windows系統(tǒng),需要Excel2013及以上版本,還需要數(shù)據(jù)庫賬號密碼,還需要IE9及以上瀏覽器,如果是早期Excel2016版本,還需要修改ReturnSingleDatabase
語句 - 數(shù)據(jù)量小
基本上一個Excel模型,在目前主流Windows電腦上存放1萬數(shù)據(jù)就開始卡,10萬數(shù)據(jù)要刷新很久,100萬基本上非i7不能刷新和使用了
基于以上這些問題,我開始轉(zhuǎn)向前端網(wǎng)頁開發(fā),在研究了Flask
,Django
和Tornado
之后,選擇了簡單快速的Tornado
,就是下面這個
在這里插入圖片描述
快速開發(fā)Python Web
1. 文件結(jié)構(gòu)
先準(zhǔn)備一個文件夾,例如取名python_web
,文件結(jié)構(gòu)如下
python_web
|— templates
| |— index.html
|— main.py
或者看截圖
python_web
2. index.html
代碼
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8”>
</head>
<body>
<div>
<form method='post' target=‘/‘>
<input type='date' title='開始日期' name=‘begindate’>
<input type='date' title='結(jié)束日期' name='enddate’>
<input type='submit' title='點擊運行' value=‘運行’>
</form>
</div>
<div>
<table>
<thead>
<tr>
{% for c in col %}
<th>{{c}}</th>
{% end %}
</tr>
</thead>
<tbody>
{% for i in data %}
<tr>
{% for x in i %}
<td>{{x}}</td>
{% end %}
</tr>
{% end %}
</tbody>
</table>
</div>
</body>
</html>
35行代碼,很簡單的一個網(wǎng)頁
- 3-5行,為了讓網(wǎng)頁支持中文
- 7-13行,條件設(shè)置部分
這里面有兩個要傳入py的變量,用name
屬性進行了標(biāo)記,分別是變量begindate
,變量enddate
- 14-33行,表格數(shù)據(jù)部分
這里是根據(jù)上面的條件,傳入py數(shù)據(jù)的兩個變量,一個是表格標(biāo)題變量col
,一個是表格數(shù)據(jù)變量data
3. main.py
代碼
main.py
# -*- coding: utf-8 -*-
import os.path
import pymysql
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define,options
define('port',default=1111,help='run on the given port',type=int) # 這里主要是定義端口
col = ['標(biāo)題1','標(biāo)題2','標(biāo)題3','標(biāo)題4'] # 傳入網(wǎng)頁的標(biāo)題列表
config = {'host':'123.123.123.123','user':'dan','passwd':'dan','port':3306,'db':'dan'} # 數(shù)據(jù)庫配置信息
class IndexHandler(tornado.web.RequestHandler):
def get(self): # 進入網(wǎng)頁時觸發(fā)
self.render('index.html',col=col,data=(('',))) # 渲染網(wǎng)頁,傳入?yún)?shù)
def post(self): # 點擊按鈕時觸發(fā)
begindate = self.get_argument('begindate') # 接收網(wǎng)頁傳來的參數(shù)1
enddate = self.get_argument('enddate') # 接收網(wǎng)頁傳來的參數(shù)2
# 下面是數(shù)據(jù)庫配置,連接,運行,關(guān)閉
db = pymysql.connect(host=config['host'],user=config['user'],passwd=config['passwd'],port=config['port'],db=config['db’])
cs = db.cursor()
cs.execute("select * from dan where everyday between '%s' and '%s'" % (begindate,enddate))
data = cs.fetchall()
cs.close()
db.close()
self.render('index.html',col=col,data=data) # 渲染網(wǎng)頁,傳入?yún)?shù)
if __name__==‘__main__’:
tornado.options.parse_command_line()
app = tornado.web.Application(
handlers=[(r'/',IndexHandler)], # 將地址綁定到類
template_path=os.path.join(os.path.dirname(__file__),'templates')) # 指明網(wǎng)頁文件夾
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
40行代碼,主要分兩部分
- IndexHandler
點擊網(wǎng)頁觸發(fā)get
或post
方法時調(diào)用該部分 - tornado
服務(wù)器配置,開啟服務(wù)器
4. 參數(shù)傳遞流程圖
流程圖
網(wǎng)頁跑起來是這樣的
在這里插入圖片描述
選擇日期,點擊運行,就會出數(shù)據(jù).這操作比Excel切片器就簡單多了,而且背后有海量的數(shù)據(jù)庫數(shù)據(jù)支撐,可以說是海量數(shù)據(jù)了
至于ip,Windows需要進入cmd輸入ipconfig
查看,Mac需要進入Terminal輸入ifconfig
查看<small>(Mac的真是難找??)</small>
知識點
前后端 | 知識點 |
---|---|
前端 | html |
后端 | python pymysql模塊 python tornado模塊 |