您好,登錄后才能下訂單哦!
這篇文章主要介紹了python flask數據可視化怎么實現的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇python flask數據可視化怎么實現文章都會有所收獲,下面我們一起來看看吧。
# -*- coding: utf-8 -*- # 作者: lang1.xia # 創建時間: 2022-08-25 03:38:26 # 修改時間: 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 外部樣式調用 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 # 外部數據庫方法調用 from data_storage import one_data # 自定義本地html模板 app = Flask(__name__, template_folder="templates") def bar_base() -> Table: # 查詢數據庫信息形成大列表 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五分鐘負載","內存使用率", "innodb行鎖", "連接數","磁盤IO", "磁盤"] rows = data_rows # 添加標題、表數據、表樣式 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="數據庫巡檢", title_style={"style": "font-size: 28px; font-weight:bold;text-align: center"})) ) return c @app.route("/") def index(): # 調用函數、返回到前端 c = bar_base() return Markup(c.render_embed()) if __name__ == "__main__": app.run(host="0.0.0.0")
# -*- coding: utf-8 -*- # 作者: lang1.xia # 創建時間: 2022-08-22 07:06:53 # 修改時間: 2022-08-22 07:06:53 # 未使用 import pymysql # 基礎連接信息 def conndb(): conn = pymysql.connect(host="IP地址", user="賬號", passwd="密碼", database="數據庫名", port="數據庫端口") cur = conn.cursor() return conn, cur # 關閉連接 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
瀏覽器訪問http://127.0.0.1:5000
關于“python flask數據可視化怎么實現”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“python flask數據可視化怎么實現”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。