Python大屏看板最全教程之数据库连接
閱讀本文大約需要3分鐘
主要內(nèi)容:數(shù)據(jù)分析。
適用人群:Python初學(xué)者,數(shù)據(jù)分析師,或有志從事數(shù)據(jù)分析工作的人員。
準(zhǔn)備軟件:Anaconda(Spyder:代碼編譯)、Navicat Premium 12(數(shù)據(jù)庫)。
此圖來自于網(wǎng)絡(luò),侵刪
從事IT項目管理這么多年,基本上已經(jīng)遺棄編程技能,但從2019年開始接觸Python,深深地迷上了這門語言,像硬件集成、數(shù)據(jù)分析,我都會用python來寫。曉風(fēng)想通過本文,讓初學(xué)者們學(xué)會以下內(nèi)容:
1、Pyecharts圖表;
2、連接數(shù)據(jù)庫;
3、大屏看板-監(jiān)控中心。
今天,我們講2、連接數(shù)據(jù)庫
接上文,還是以柱狀圖為例。
1、首先,我們打開Navicat,“文件 - 新建連接 - MYSQL”,我這邊起名新連接為“datacenter”,點擊右鍵,新建數(shù)據(jù)庫“warehouse_input”(名字可以自己起)
2、建完數(shù)據(jù)庫,我們來建自己的數(shù)據(jù)庫表,保存
3、點擊該表,維護一些測試數(shù)據(jù),完成數(shù)據(jù)庫內(nèi)容的創(chuàng)建
4、回到Anaconda - Spyder,打開之前編寫的圖表文件或新建文件,我們先把柱狀圖的代碼修改為函數(shù)形式
from pyecharts import options as opts from pyecharts.charts import Bar,Page import pymysqldef bar(cos): #柱狀圖 c = ( Bar(init_opts=opts.InitOpts(bg_color="white")).add_xaxis(costomer).add_yaxis("訂單數(shù)量", quantity).set_series_opts(label_opts=opts.LabelOpts(is_show=False)).set_global_opts( title_opts=opts.TitleOpts("客戶訂單數(shù)量"), datazoom_opts=opts.DataZoomOpts(is_show=True,range_start=0,range_end=100), toolbox_opts=opts.ToolboxOpts(orient="vertical",pos_left="90%",feature=opts.ToolBoxFeatureOpts(data_zoom=opts.ToolBoxFeatureDataZoomOpts(is_show=False)))))return c5、添加import pymysql(連接數(shù)據(jù)庫需要),from operator import itemgetter(將數(shù)據(jù)參數(shù)化時需要),這里我想呈現(xiàn)各客戶的訂單數(shù)量,那么SQL語句和連接數(shù)據(jù)庫的代碼如下
import pymysql from operator import itemgetterdb = pymysql.connect(host="localhost", user="root", password="123456", database="warehouse_input") sql = "select customer_name,sum(cloth_quantity) AS nums from po GROUP BY customer_name" try:cursor = db.cursor()cursor.execute(sql)cos = cursor.fetchall() except Exception as e:db.rollback()print('事物處理失敗',e) else: db.commit()print('事物處理成功',cos) cursor.close() db.close()6、接下來我們結(jié)合柱狀圖函數(shù)和數(shù)據(jù)庫連接的內(nèi)容,加入page函數(shù)
from pyecharts import options as opts from pyecharts.charts import Bar,Page import pymysql from operator import itemgetterdef bar(cos): #柱狀圖 costomer = list(map(itemgetter(0), cos))quantity = list(map(itemgetter(1), cos))c = ( Bar(init_opts=opts.InitOpts(bg_color="white")).add_xaxis(costomer).add_yaxis("訂單數(shù)量", quantity).set_series_opts(label_opts=opts.LabelOpts(is_show=False)).set_global_opts( title_opts=opts.TitleOpts("客戶訂單數(shù)量"),datazoom_opts=opts.DataZoomOpts(is_show=True,range_start=0,range_end=100),toolbox_opts=opts.ToolboxOpts(orient="vertical",pos_left="90%",feature=opts.ToolBoxFeatureOpts(data_zoom=opts.ToolBoxFeatureDataZoomOpts(is_show=False)))))return cdb = pymysql.connect(host="localhost", user="root", password="123456", database="warehouse_input") sql = "select customer_name,sum(cloth_quantity) AS nums from po GROUP BY customer_name" try:cursor = db.cursor()cursor.execute(sql)cos = cursor.fetchall() except Exception as e:db.rollback()print('事物處理失敗',e) else: db.commit()print('事物處理成功',cos) cursor.close() db.close() page = Page() page.add(bar(cos)) page.render("bar.ht7、最后,我們運行下,看下效果
?
學(xué)習(xí)到了這里,我們就可以學(xué)會了圖表的動態(tài)數(shù)據(jù)呈現(xiàn)。好了,大家趕緊動起來,有空就把所有圖表都操作一遍。今天的內(nèi)容就到這里,下篇文章將會教大家怎么將多個圖表布局在一個大屏上,敬請期待。愿我們一起成長!
如果覺得有用的話,請幫忙點贊、關(guān)注、收藏哦,感謝您的支持!
總結(jié)
以上是生活随笔為你收集整理的Python大屏看板最全教程之数据库连接的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C/C++程序员简历
- 下一篇: Google Sketchup 三维建模