這篇文章主要介紹了python flask數(shù)據(jù)可視化怎么實(shí)現(xiàn)的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇python flask數(shù)據(jù)可視化怎么實(shí)現(xiàn)文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。
創(chuàng)新互聯(lián)擁有十余年成都網(wǎng)站建設(shè)工作經(jīng)驗(yàn),為各大企業(yè)提供成都做網(wǎng)站、成都網(wǎng)站制作服務(wù),對(duì)于網(wǎng)頁(yè)設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、成都APP應(yīng)用開(kāi)發(fā)、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、程序開(kāi)發(fā)、網(wǎng)站優(yōu)化(SEO優(yōu)化)、微網(wǎng)站、申請(qǐng)域名等,憑借多年來(lái)在互聯(lián)網(wǎng)的打拼,我們?cè)诨ヂ?lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了很多網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營(yíng)銷經(jīng)驗(yàn),集策劃、開(kāi)發(fā)、設(shè)計(jì)、營(yíng)銷、管理等網(wǎng)站化運(yùn)作于一體,具備承接各種規(guī)模類型的網(wǎng)站建設(shè)項(xiàng)目的能力。
# -*- coding: utf-8 -*- # 作者: lang1.xia # 創(chuàng)建時(shí)間: 2022-08-25 03:38:26 # 修改時(shí)間: 2022-08-25 03:38:26 from flask import Flask from jinja2 import Environment, FileSystemLoader from markupsafe import Markup from pyecharts.globals import CurrentConfig # echarts 外部樣式調(diào)用 CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader("./templates")) CurrentConfig.ONLINE_HOST = 'http://127.0.0.1/assets/' from pyecharts.options import ComponentTitleOpts from pyecharts.components import Table # 外部數(shù)據(jù)庫(kù)方法調(diào)用 from data_storage import one_data # 自定義本地html模板 app = Flask(__name__, template_folder="templates") def bar_base() -> Table: # 查詢數(shù)據(jù)庫(kù)信息形成大列表 sql = "select * from ains_MySQL_base" res = one_data(sql) data_rows = [] if res["code"] == 200 and res["data"]: for i in res["data"]: disk_io_info = ''.join(list(i[8])) disk_io_tmp = "" for key_d in eval(disk_io_info): key = list(key_d.keys()) val = list(key_d.values()) new_str = key[0] + ": " + str(round(val[0],4)) + " \n" disk_io_tmp += new_str new_i = list(i[3:7]) + list(i[9:-1]) new_i.append(disk_io_tmp) disk_info = ''.join(list(i[7])) disk_tmp = "" for key_d in eval(disk_info): key = list(key_d.keys()) val = list(key_d.values()) new_str = key[0] + ": " + str(round(val[0],4)) + " \n" disk_tmp += new_str new_i.append(disk_tmp) data_rows.append(new_i) print(data_rows) # 定義表頭 headers = [ "IP地址", "CPU使用率", "CPU五分鐘負(fù)載","內(nèi)存使用率", "innodb行鎖", "連接數(shù)","磁盤IO", "磁盤"] rows = data_rows # 添加標(biāo)題、表數(shù)據(jù)、表樣式 c = ( Table() .add(headers, rows, attributes={"style": "margin:0% auto;font-size: 28px;text-align: left,width:100px", "class": "fl-table"}) .set_global_opts(title_opts=ComponentTitleOpts(title="數(shù)據(jù)庫(kù)巡檢", title_style={"style": "font-size: 28px; font-weight:bold;text-align: center"})) ) return c @app.route("/") def index(): # 調(diào)用函數(shù)、返回到前端 c = bar_base() return Markup(c.render_embed()) if __name__ == "__main__": app.run(host="0.0.0.0")
# -*- coding: utf-8 -*- # 作者: lang1.xia # 創(chuàng)建時(shí)間: 2022-08-22 07:06:53 # 修改時(shí)間: 2022-08-22 07:06:53 # 未使用 import pymysql # 基礎(chǔ)連接信息 def conndb(): conn = pymysql.connect(host="IP地址", user="賬號(hào)", passwd="密碼", database="數(shù)據(jù)庫(kù)名", port="數(shù)據(jù)庫(kù)端口") cur = conn.cursor() return conn, cur # 關(guān)閉連接 def closedb(conn, cur): cur.close() conn.close() # executemany方法封裝 def batch_data(sql): conn, cur = conndb() try: cur.executemany(sql) result = cur.fetchall() except Exception as e : return {"code": 400, "message": e} else: conn.commit() closedb(conn=conn, cur=cur) return {"code": 200, "message": "true", "data": result} # execute方法封裝 def one_data(sql): conn, cur = conndb() try: cur.execute(sql) result = cur.fetchall() except Exception as e : return {"code": 400, "message": e} else: conn.commit() closedb(conn=conn, cur=cur) return {"code": 200, "message": "true", "data": result}
python server.py
瀏覽器訪問(wèn)http://127.0.0.1:5000
關(guān)于“python flask數(shù)據(jù)可視化怎么實(shí)現(xiàn)”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“python flask數(shù)據(jù)可視化怎么實(shí)現(xiàn)”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。