Python tornado用40行代碼搭建數(shù)據(jù)庫交互網(wǎng)頁實現(xiàn)快速全棧開發(fā)方式
Python tornado用40行代碼搭建數(shù)據(jù)庫交互網(wǎng)頁實現(xiàn)快速全棧開發(fā)
作為數(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
或者看截圖
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
屬性進(jìn)行了標(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): # 進(jìn)入網(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需要進(jìn)入cmd輸入ipconfig
查看,Mac需要進(jìn)入Terminal輸入ifconfig
查看(Mac的真是難找??)
知識點
前后端 | 知識點 |
---|---|
前端 | html |
后端 | python pymysql模塊 python tornado模塊 |
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python使用sqlalchemy實現(xiàn)連接數(shù)據(jù)庫的幫助類
這篇文章主要為大家詳細(xì)介紹了Python如何使用sqlalchemy實現(xiàn)連接數(shù)據(jù)庫的幫助類,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,需要的可以參考下2024-02-02Python使用scrapy采集時偽裝成HTTP/1.1的方法
這篇文章主要介紹了Python使用scrapy采集時偽裝成HTTP/1.1的方法,實例分析了scrapy采集的使用技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04python中Tkinter復(fù)選框Checkbutton是否被選中判斷
這篇文章主要介紹了python中Tkinter復(fù)選框Checkbutton是否被選中判斷方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01Python文件簡單操作及openpyxl操作excel文件詳解
這篇文章主要為大家詳細(xì)介紹了python對文件的簡單使用及openpyxl操作excel文件的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-10-10python詳解如何通過sshtunnel pymssql實現(xiàn)遠(yuǎn)程連接數(shù)據(jù)庫
為了安全起見,很多公司服務(wù)器數(shù)據(jù)庫的訪問多半是要做限制的,由專門的DBA管理,而且都是做的集群,數(shù)據(jù)庫只能內(nèi)網(wǎng)訪問,所以就有一個直接的問題是,往往多數(shù)時候,在別的機器上(比如自己本地),是不能訪問數(shù)據(jù)庫的,給日常開發(fā)調(diào)試造成了很大不便2021-10-10