当前位置:   article > 正文

Flask可视化_flake可视化

flake可视化

目录

Flask可视化

一、创建文件

二、写html文件

1.写html的基本代码

2.写盒子div

3.css文件

 4.连接数据库并处理数据

5.使用python.py文件,让app跑起来

6.使用echarts绘图

 7.使用ajax连接前后端并显示

8.echarts图表

         1.折线图

        2.柱形图

        3.饼图

        4.雷达图


Flask可视化

工具:

Python Flask——后台

html+css——前台页面

js(ajax)——前后台数据通信

mysql——存放数据

echart实现各种图形绘制

一、创建文件

可以参照下面的链接
创建flak

二、写html文件

1.写html的基本代码

2.写盒子div

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>淘宝可视化</title>
  6. <script src = '../static/js/echats.js'></script>
  7. <script src = '../static/js/jquery-3.6.0.js'></script>
  8. <link href="../static/css/tbcss.css" rel = 'stylesheet'>
  9. </head>
  10. <body>
  11. <div id ='title'>信息可视化</div>
  12. <div id = 'l_top'>123</div>
  13. <div id = 'l_bottom'>123</div>
  14. <div id = 'c_top'>123</div>
  15. <div id = 'c_bottom'>123</div>
  16. <div id = 'r_top'>123</div>
  17. <div id = 'r_bottom'>123</div>
  18. <script src = '../static/js/tbajax.js'></script>
  19. <script src = '../static/js/tbecharts.js'></script>
  20. </body>
  21. </html>

3.css文件

  1. *{
  2. margin: 0;
  3. padding: 0;
  4. }
  5. #title{
  6. position: absolute;
  7. height: 16%;
  8. width: 100%;
  9. color: white;
  10. font-size: 30px;
  11. background-color: black;
  12. display: flex;
  13. align-items: center;
  14. justify-content: center;
  15. }
  16. #l_top{
  17. position: absolute;
  18. width: 30%;
  19. height: 42%;
  20. top: 16%;
  21. background-color: cornflowerblue;
  22. }
  23. #l_bottom{
  24. position: absolute;
  25. width: 30%;
  26. height: 42%;
  27. top: 58%;
  28. background-color: bisque;
  29. }
  30. #c_top{
  31. position: absolute;
  32. width: 40%;
  33. height: 42%;
  34. top: 16%;
  35. left: 30%;
  36. background-color: chartreuse;
  37. }
  38. #c_bottom{
  39. position: absolute;
  40. width: 40%;
  41. height: 42%;
  42. top: 58%;
  43. left: 30%;
  44. background-color: yellow;
  45. }
  46. #r_top{
  47. position: absolute;
  48. width: 40%;
  49. height: 42%;
  50. top: 16%;
  51. left: 70%;
  52. background-color: beige;
  53. }
  54. #r_bottom{
  55. position: absolute;
  56. width: 40%;
  57. height: 42%;
  58. top: 58%;
  59. left: 70%;
  60. background-color: aqua;
  61. }

 4.连接数据库并处理数据

  1. import pymysql
  2. def connect():
  3. conn = pymysql.Connect(host='127.0.0.1',port=3306,user='root',password='tz15035895786',db='tb',charset='utf8')
  4. cursor = conn.cursor()
  5. return cursor,conn
  6. def closecn(cursor,conn):
  7. cursor.close
  8. conn.close
  9. def query(sql):
  10. cursor,conn = connect()
  11. cursor.execute(sql)
  12. res = cursor.fetchall()
  13. closecn(cursor,conn)
  14. return res
  15. def get_data1():
  16. sql ='select province,count(province) from tb group by province order by count(province) desc limit 10;'
  17. result = query(sql)
  18. city = []
  19. citycount = []
  20. for i in result:
  21. city.append(i[0])
  22. citycount.append(i[1])
  23. print(city)
  24. print(citycount)
  25. return city,citycount
  26. get_data1()

5.使用python.py文件,让app跑起来

  1. from flask import Flask
  2. from flask import render_template
  3. from flask import jsonify
  4. import util
  5. app = Flask(__name__)
  6. @app.route('/')
  7. def index(): # put application's code here
  8. return render_template('index.html')
  9. @app.route('/data1')
  10. def get_data():
  11. city,citycount = util.get_data1()
  12. return jsonify({'city':city,'citycount':citycount})
  13. @app.route('/data2')
  14. def get_data2():
  15. gendercount = util.get_data2()
  16. return jsonify({'gendercount':gendercount})
  17. @app.route('/data3')
  18. def get_data3():
  19. item_id,item_idcount = util.get_data3()
  20. return jsonify({item_id:'item_id',item_idcount:'item_idcount'})
  21. if __name__ == '__main__':
  22. app.run()

6.使用echarts绘图

  1. var a = echarts.init(document.getElementById('l_top'),'dark')
  2. var b = {
  3. title:{
  4. text:'中国城市各分布数量',
  5. left:'center'
  6. },
  7. xAxis:{
  8. type:'category',
  9. data:[]
  10. } ,
  11. yAxis: {
  12. type: 'value'
  13. },
  14. series:[
  15. {
  16. type:'line',
  17. data: []
  18. }
  19. ]
  20. }
  21. a.setOption(b)

 7.使用ajax连接前后端并显示

  1. function get_data(){
  2. $.ajax({
  3. 'url':'/data1',
  4. 'success':function (data1){
  5. b.xAxis.data = data1.city
  6. b.series[0].data = data1.citycount
  7. a.setOption(b)
  8. }
  9. })
  10. }

8.echarts图表

         1.折线图

                

  1. option = {
  2. xAxis: {
  3. type: 'category',
  4. data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  5. },
  6. yAxis: {
  7. type: 'value'
  8. },
  9. series: [
  10. {
  11. data: [150, 230, 224, 218, 135, 147, 260],
  12. type: 'line'
  13. }
  14. ]
  15. };

        2.柱形图

  1. option = {
  2. xAxis: {
  3. type: 'category',
  4. data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  5. },
  6. yAxis: {
  7. type: 'value'
  8. },
  9. series: [
  10. {
  11. data: [120, 200, 150, 80, 70, 110, 130],
  12. type: 'bar',
  13. showBackground: true,
  14. backgroundStyle: {
  15. color: 'rgba(180, 180, 180, 0.2)'
  16. }
  17. }
  18. ]
  19. };

        3.饼图

  1. option = {
  2. title: {
  3. text: 'Referer of a Website',
  4. subtext: 'Fake Data',
  5. left: 'center'
  6. },
  7. tooltip: {
  8. trigger: 'item'
  9. },
  10. legend: {
  11. orient: 'vertical',
  12. left: 'left'
  13. },
  14. series: [
  15. {
  16. name: 'Access From',
  17. type: 'pie',
  18. radius: '50%',
  19. data: [
  20. { value: 1048, name: 'Search Engine' },
  21. { value: 735, name: 'Direct' },
  22. { value: 580, name: 'Email' },
  23. { value: 484, name: 'Union Ads' },
  24. { value: 300, name: 'Video Ads' }
  25. ],
  26. emphasis: {
  27. itemStyle: {
  28. shadowBlur: 10,
  29. shadowOffsetX: 0,
  30. shadowColor: 'rgba(0, 0, 0, 0.5)'
  31. }
  32. }
  33. }
  34. ]
  35. };

        4.雷达图

 

  1. option = {
  2. title: {
  3. text: 'Basic Radar Chart'
  4. },
  5. legend: {
  6. data: ['Allocated Budget', 'Actual Spending']
  7. },
  8. radar: {
  9. // shape: 'circle',
  10. indicator: [
  11. { name: 'Sales', max: 6500 },
  12. { name: 'Administration', max: 16000 },
  13. { name: 'Information Technology', max: 30000 },
  14. { name: 'Customer Support', max: 38000 },
  15. { name: 'Development', max: 52000 },
  16. { name: 'Marketing', max: 25000 }
  17. ]
  18. },
  19. series: [
  20. {
  21. name: 'Budget vs spending',
  22. type: 'radar',
  23. data: [
  24. {
  25. value: [4200, 3000, 20000, 35000, 50000, 18000],
  26. name: 'Allocated Budget'
  27. },
  28. {
  29. value: [5000, 14000, 28000, 26000, 42000, 21000],
  30. name: 'Actual Spending'
  31. }
  32. ]
  33. }
  34. ]
  35. };

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号