当前位置:   article > 正文

flask 数据可视化图形_flask可视化

flask可视化

     

目录

1、如何开始一个flask系统

2、登录注册

3、牛奶面包折线图

4、echarts可视化图像

1. 创建一个【page.html】(柱形图)部分

2. 创建一个【login.html】登录部分

3. 创建一个【page2.html】牛奶面包的折线图部分

前言:    

    本文的学习需要读者有一定的Flask的知识基础,初学者请先自学完成Flask简易项目搭建。Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求进行预处理,然后触发Flask框架,开发人员基于Flask框架提供的功能对请求进行相应的处理,并返回给用户,如果要返回给用户复杂的内容时,需要借助jinja2模板来实现对模板的处理,即:将模板和数据进行渲染,将渲染后的字符串返回给用户浏览器。

1、如何开始一个flask系统

在命令中pip安装一个flask,就可以直接开始一个flask系统了。

pip install flask

在 运行py文件会遇到很多报错,部分原因是没有选择对环境。

因为我弄的是牛奶和面包的销量数据我直接导入了文件

 创建一个app.py文件,一个简单的flask应用。

  1. from flask import Flask, request, redirect, render_template, session, jsonify
  2. import pandas as pd
  3. df = pd.read_excel(r'g:\数据可视化及开发python\15-flask开发基础\flask\demo1\折线图作业数据-商品销量(1).xlsx')
  4. df['时间'] = df['日期'].agg(lambda x:str(x) [5:10])
  5. dict_to_json = {
  6. 'time':df['时间'].tolist(),
  7. 'milk':df['牛奶'].tolist(),
  8. 'bread':df['面包'].tolist(),
  9. }
  10. app = Flask(__name__)

除了一个基础的app之外,还需要静态文件(static):一些不会变更的css,js,img文件,还有html文件(templates)

 

所以一个基础的flask应用如下:

2、登录注册

登录账号密码

对用户信息加密

  1. app.secret_key = 'QWERTYUIOP' # 对用户信息加密
  2. @app.route('/login', methods=['GET', "POST"])
  3. def login():
  4. if request.method == 'GET':
  5. return render_template('login.html')
  6. user = request.form.get('user')
  7. pwd = request.form.get('pwd')
  8. if user == 'admin' and pwd == '123':
  9. session['user_info'] = user
  10. return redirect('/page')
  11. elif user == 'admin' and pwd =='123':
  12. session['user_info'] = user
  13. return redirect('/page2')
  14. else:
  15. return render_template('login.html', msg='用户名或密码输入错误')

3、牛奶面包折线图

  1. @app.route('/page')
  2. def index():
  3. data = {
  4. 'x':['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
  5. 'y':[260, 150, 230, 224, 218, 135, 147]
  6. }
  7. return render_template('page.html',**data)
  8. # 定义一个路由存储json数据
  9. @app.route('/json')
  10. def json():
  11. return jsonify(dict_to_json) # jsonify中传入一个字典
  12. # 定义一个路由展示图行
  13. @app.route('/page2')
  14. def page2():
  15. return render_template('page2.html')
  16. if __name__ == "__main__":
  17. # app.run('0.0.0.0',port=80)
  18. app.run()
  19. print(dict_to_json)

        app.py合并后的代码

  1. from flask import Flask, request, redirect, render_template, session, jsonify
  2. import pandas as pd
  3. df = pd.read_excel(r'g:\数据可视化及开发python\15-flask开发基础\flask\demo1\折线图作业数据-商品销量(1).xlsx')
  4. df['时间'] = df['日期'].agg(lambda x:str(x) [5:10])
  5. dict_to_json = {
  6. 'time':df['时间'].tolist(),
  7. 'milk':df['牛奶'].tolist(),
  8. 'bread':df['面包'].tolist(),
  9. }
  10. app = Flask(__name__)
  11. app.secret_key = 'QWERTYUIOP' # 对用户信息加密
  12. @app.route('/login', methods=['GET', "POST"])
  13. def login():
  14. if request.method == 'GET':
  15. return render_template('login.html')
  16. user = request.form.get('user')
  17. pwd = request.form.get('pwd')
  18. if user == 'admin' and pwd == '123':
  19. session['user_info'] = user
  20. return redirect('/page')
  21. elif user == 'admin' and pwd =='123':
  22. session['user_info'] = user
  23. return redirect('/page2')
  24. else:
  25. return render_template('login.html', msg='用户名或密码输入错误')
  26. @app.route('/page')
  27. def index():
  28. data = {
  29. 'x':['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
  30. 'y':[260, 150, 230, 224, 218, 135, 147]
  31. }
  32. return render_template('page.html',**data)
  33. # 定义一个路由存储json数据
  34. @app.route('/json')
  35. def json():
  36. return jsonify(dict_to_json) # jsonify中传入一个字典
  37. # 定义一个路由展示图行
  38. @app.route('/page2')
  39. def page2():
  40. return render_template('page2.html')
  41. if __name__ == "__main__":
  42. # app.run('0.0.0.0',port=80)
  43. app.run()
  44. print(dict_to_json)

4、echarts可视化图像

可视化有很多种方法,但是最省力的之一,一定是直接使用echarts:echarts的代码可以直接复制过来用,经过简单的修改就可以实现非常好看的效果。

可视化图像

 可以参考网页中建立一个基础的可视化的页面。

1. 创建一个【page.html】(柱形图)部分

渲染效果:

        给模板中添加数据使其变得更见美观,做到渲染效果。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>bar</title>
  6. <!-- <script type="text/javascript" src="https://assets.pyecharts.org/assets/echarts.min.js"></script> -->
  7. <script type="text/javascript" src="../static/js/echarts.min.js"></script>
  8. </head>
  9. <body>
  10. <h2>{{x}}</h2>
  11. <h2>{{y}}</h2>
  12. <div id="chart" style="width:900; height:500px;"></div>
  13. <script type='text/javascript'>
  14. // var chartDom = document.getElementById('chart');
  15. var myChart = echarts.init(document.getElementById('chart'));
  16. var option;
  17. option = {
  18. xAxis: {
  19. type: 'category',
  20. data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  21. },
  22. yAxis: {
  23. type: 'value'
  24. },
  25. series: [
  26. {
  27. data: [260, 150, 230, 224, 218, 135, 147],
  28. type: 'bar'
  29. },
  30. ]
  31. };
  32. myChart.setOption(option);
  33. console.log(x);
  34. </script>
  35. </body>
  36. </html>

2. 创建一个【login.html】登录部分

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Login</title>
  6. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
  7. integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
  8. <style>
  9. .loginForm {
  10. /*边框高度*/
  11. height: 300px;
  12. /*边框宽度*/
  13. width: 500px;
  14. /*边框颜色*/
  15. border: #4d4d4d solid 1px;
  16. /*边框圆角*/
  17. border-radius: 4px;
  18. /*阴影 水平方向,竖直方向,模糊距离*/
  19. box-shadow: 5px 5px 5px #4d4d4d;
  20. /*上边界距离*/
  21. margin-top: 150px;
  22. /*左边界距离:自动*/
  23. margin-left: auto;
  24. /*右边界距离:自动*/
  25. margin-right: auto;
  26. /*用户名、密码间距*/
  27. padding: 20px 40px;
  28. }
  29. /*将用户登录置于中间*/
  30. .loginForm h2 {
  31. text-align: center;
  32. }
  33. .button {
  34. text-align: center;
  35. vertical-align: middle;
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <div class="loginForm">
  41. <h2>平台登录界面</h2>
  42. <form method="post">
  43. <div class="form-group">
  44. <label>用户名</label>
  45. <input type="text" class="form-control" name="user" placeholder="请输入用户名">
  46. </div>
  47. <div class="form-group">
  48. <label>密码</label>
  49. <input type="password" class="form-control" name="pwd" placeholder="请输入密码">
  50. </div>
  51. <div class="button">
  52. <input type="submit" class="btn btn-primary" value="登 录"/>
  53. </div>
  54. </form>
  55. </div>
  56. </body>
  57. </html>

3. 创建一个【page2.html】牛奶面包的折线图部分

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Awesome-pyecharts</title>
  6. <script type="text/javascript" src="https://assets.pyecharts.org/assets/v5/echarts.min.js"></script>
  7. </head>
  8. <body >
  9. <div id="4908dbf047b244aea5a10ff9a82d2dd2" class="chart-container" style="width:900px; height:500px; "></div>
  10. <script>
  11. var chart_4908dbf047b244aea5a10ff9a82d2dd2 = echarts.init(
  12. document.getElementById('4908dbf047b244aea5a10ff9a82d2dd2'), 'white', {renderer: 'canvas'});
  13. var option_4908dbf047b244aea5a10ff9a82d2dd2 = {
  14. "backgroundColor": "white",
  15. "animation": true,
  16. "animationThreshold": 2000,
  17. "animationDuration": 1000,
  18. "animationEasing": "cubicOut",
  19. "animationDelay": 0,
  20. "animationDurationUpdate": 300,
  21. "animationEasingUpdate": "cubicOut",
  22. "animationDelayUpdate": 0,
  23. "aria": {
  24. "enabled": false
  25. },
  26. "color": [
  27. "#5470c6",
  28. "#91cc75",
  29. "#fac858",
  30. "#ee6666",
  31. "#73c0de",
  32. "#3ba272",
  33. "#fc8452",
  34. "#9a60b4",
  35. "#ea7ccc"
  36. ],
  37. "series": [
  38. {
  39. "type": "line",
  40. "name": "\u9762\u5305",
  41. "connectNulls": false,
  42. "xAxisIndex": 0,
  43. "symbolSize": 4,
  44. "showSymbol": true,
  45. "smooth": false,
  46. "clip": true,
  47. "step": false,
  48. "data": [
  49. [
  50. "05-01",
  51. 80
  52. ],
  53. [
  54. "05-02",
  55. 77
  56. ],
  57. [
  58. "05-03",
  59. 51
  60. ],
  61. [
  62. "05-04",
  63. 57
  64. ],
  65. [
  66. "05-05",
  67. 65
  68. ],
  69. [
  70. "05-06",
  71. 78
  72. ],
  73. [
  74. "05-07",
  75. 58
  76. ],
  77. [
  78. "05-08",
  79. 66
  80. ],
  81. [
  82. "05-09",
  83. 51
  84. ],
  85. [
  86. "05-10",
  87. 73
  88. ],
  89. [
  90. "05-11",
  91. 79
  92. ],
  93. [
  94. "05-12",
  95. 52
  96. ],
  97. [
  98. "05-13",
  99. 91
  100. ],
  101. [
  102. "05-14",
  103. 56
  104. ],
  105. [
  106. "05-15",
  107. 80
  108. ],
  109. [
  110. "05-16",
  111. 45
  112. ],
  113. [
  114. "05-17",
  115. 58
  116. ],
  117. [
  118. "05-18",
  119. 81
  120. ],
  121. [
  122. "05-19",
  123. 57
  124. ],
  125. [
  126. "05-20",
  127. 82
  128. ],
  129. [
  130. "05-21",
  131. 64
  132. ],
  133. [
  134. "05-22",
  135. 85
  136. ],
  137. [
  138. "05-23",
  139. 68
  140. ],
  141. [
  142. "05-24",
  143. 88
  144. ],
  145. [
  146. "05-25",
  147. 63
  148. ],
  149. [
  150. "05-26",
  151. 48
  152. ],
  153. [
  154. "05-27",
  155. 67
  156. ],
  157. [
  158. "05-28",
  159. 53
  160. ],
  161. [
  162. "05-29",
  163. 56
  164. ],
  165. [
  166. "05-30",
  167. 75
  168. ],
  169. [
  170. "05-31",
  171. 85
  172. ]
  173. ],
  174. "hoverAnimation": true,
  175. "label": {
  176. "show": true,
  177. "margin": 8
  178. },
  179. "logBase": 10,
  180. "seriesLayoutBy": "column",
  181. "lineStyle": {
  182. "show": true,
  183. "width": 1,
  184. "opacity": 1,
  185. "curveness": 0,
  186. "type": "solid"
  187. },
  188. "areaStyle": {
  189. "opacity": 0
  190. },
  191. "markPoint": {
  192. "label": {
  193. "show": true,
  194. "position": "inside",
  195. "color": "#fff",
  196. "margin": 8
  197. },
  198. "data": [
  199. {
  200. "type": "min"
  201. }
  202. ]
  203. },
  204. "zlevel": 0,
  205. "z": 0
  206. },
  207. {
  208. "type": "line",
  209. "name": "\u725b\u5976",
  210. "connectNulls": false,
  211. "xAxisIndex": 0,
  212. "symbol": "traingle",
  213. "symbolSize": 6,
  214. "showSymbol": true,
  215. "smooth": false,
  216. "clip": true,
  217. "step": false,
  218. "data": [
  219. [
  220. "05-01",
  221. 53
  222. ],
  223. [
  224. "05-02",
  225. 60
  226. ],
  227. [
  228. "05-03",
  229. 84
  230. ],
  231. [
  232. "05-04",
  233. 72
  234. ],
  235. [
  236. "05-05",
  237. 81
  238. ],
  239. [
  240. "05-06",
  241. 66
  242. ],
  243. [
  244. "05-07",
  245. 78
  246. ],
  247. [
  248. "05-08",
  249. 64
  250. ],
  251. [
  252. "05-09",
  253. 96
  254. ],
  255. [
  256. "05-10",
  257. 82
  258. ],
  259. [
  260. "05-11",
  261. 49
  262. ],
  263. [
  264. "05-12",
  265. 42
  266. ],
  267. [
  268. "05-13",
  269. 50
  270. ],
  271. [
  272. "05-14",
  273. 72
  274. ],
  275. [
  276. "05-15",
  277. 65
  278. ],
  279. [
  280. "05-16",
  281. 81
  282. ],
  283. [
  284. "05-17",
  285. 77
  286. ],
  287. [
  288. "05-18",
  289. 66
  290. ],
  291. [
  292. "05-19",
  293. 85
  294. ],
  295. [
  296. "05-20",
  297. 79
  298. ],
  299. [
  300. "05-21",
  301. 76
  302. ],
  303. [
  304. "05-22",
  305. 70
  306. ],
  307. [
  308. "05-23",
  309. 79
  310. ],
  311. [
  312. "05-24",
  313. 81
  314. ],
  315. [
  316. "05-25",
  317. 59
  318. ],
  319. [
  320. "05-26",
  321. 49
  322. ],
  323. [
  324. "05-27",
  325. 66
  326. ],
  327. [
  328. "05-28",
  329. 85
  330. ],
  331. [
  332. "05-29",
  333. 72
  334. ],
  335. [
  336. "05-30",
  337. 59
  338. ],
  339. [
  340. "05-31",
  341. 65
  342. ]
  343. ],
  344. "hoverAnimation": true,
  345. "label": {
  346. "show": true,
  347. "margin": 8
  348. },
  349. "logBase": 10,
  350. "seriesLayoutBy": "column",
  351. "lineStyle": {
  352. "show": true,
  353. "width": 1,
  354. "opacity": 1,
  355. "curveness": 0,
  356. "type": "solid",
  357. "color": "#44cef6"
  358. },
  359. "areaStyle": {
  360. "opacity": 0
  361. },
  362. "markPoint": {
  363. "label": {
  364. "show": true,
  365. "position": "inside",
  366. "color": "#fff",
  367. "margin": 8
  368. },
  369. "data": [
  370. {
  371. "type": "max"
  372. }
  373. ]
  374. },
  375. "itemStyle": {
  376. "color": "#e4c6d0",
  377. "borderColor": "#a4e2c6",
  378. "borderWidth": 1
  379. },
  380. "zlevel": 0,
  381. "z": 0
  382. }
  383. ],
  384. "legend": [
  385. {
  386. "data": [
  387. "\u9762\u5305",
  388. "\u725b\u5976"
  389. ],
  390. "selected": {}
  391. }
  392. ],
  393. "tooltip": {
  394. "show": true,
  395. "trigger": "item",
  396. "triggerOn": "mousemove|click",
  397. "axisPointer": {
  398. "type": "line"
  399. },
  400. "showContent": true,
  401. "alwaysShowContent": false,
  402. "showDelay": 0,
  403. "hideDelay": 100,
  404. "enterable": false,
  405. "confine": false,
  406. "appendToBody": false,
  407. "transitionDuration": 0.4,
  408. "textStyle": {
  409. "fontSize": 14
  410. },
  411. "borderWidth": 0,
  412. "padding": 5,
  413. "order": "seriesAsc"
  414. },
  415. "xAxis": [
  416. {
  417. "show": true,
  418. "scale": false,
  419. "nameLocation": "end",
  420. "nameGap": 15,
  421. "gridIndex": 0,
  422. "inverse": false,
  423. "offset": 0,
  424. "splitNumber": 5,
  425. "minInterval": 0,
  426. "splitLine": {
  427. "show": true,
  428. "lineStyle": {
  429. "show": true,
  430. "width": 1,
  431. "opacity": 1,
  432. "curveness": 0,
  433. "type": "solid"
  434. }
  435. },
  436. "data": [
  437. "05-01",
  438. "05-02",
  439. "05-03",
  440. "05-04",
  441. "05-05",
  442. "05-06",
  443. "05-07",
  444. "05-08",
  445. "05-09",
  446. "05-10",
  447. "05-11",
  448. "05-12",
  449. "05-13",
  450. "05-14",
  451. "05-15",
  452. "05-16",
  453. "05-17",
  454. "05-18",
  455. "05-19",
  456. "05-20",
  457. "05-21",
  458. "05-22",
  459. "05-23",
  460. "05-24",
  461. "05-25",
  462. "05-26",
  463. "05-27",
  464. "05-28",
  465. "05-29",
  466. "05-30",
  467. "05-31"
  468. ]
  469. }
  470. ],
  471. "yAxis": [
  472. {
  473. "show": true,
  474. "scale": false,
  475. "nameLocation": "end",
  476. "nameGap": 15,
  477. "gridIndex": 0,
  478. "inverse": false,
  479. "offset": 0,
  480. "splitNumber": 5,
  481. "minInterval": 0,
  482. "splitLine": {
  483. "show": true,
  484. "lineStyle": {
  485. "show": true,
  486. "width": 1,
  487. "opacity": 1,
  488. "curveness": 0,
  489. "type": "solid"
  490. }
  491. }
  492. }
  493. ]
  494. };
  495. chart_4908dbf047b244aea5a10ff9a82d2dd2.setOption(option_4908dbf047b244aea5a10ff9a82d2dd2);
  496. </script>
  497. </body>
  498. </html>

最后运行app.py结果:

复制该网站进入浏览器中运行,在http://127.0.0.1:5000/后面添加l(login)、(page)、(page2)就能分别出现柱形图、登录页面、折线图。

 

小结

        本小节,介绍了 Flask Web 框架的主要特性、官方网站、源码资源、体系结构、安装部署和常用插件,并通过一个示例程序给演示了 Flask 的基本应用。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/142087
推荐阅读
相关标签
  

闽ICP备14008679号