pyecharts结合flask框架的使用
作者:程序媛小庄 时间:2022-12-01 18:37:25
介绍
本文主要介绍如何在Flask框架中使用pyecharts
,关于Flask框架使用这里不做具体说明~
Flask模板渲染
首先需要创建一个flask项目,flask项目对目录结构要求不高,但是如果是前后端混合项目的话,模板文件必须存放在templates
文件夹下,否则视图函数返回模板文件时会提示找不到对应的文件。下面是flask项目的简单目录结构:
.
├── server.py
└── templates
下述代码是server.py
中的示例的代码:
from flask import Flask
from jinja2 import Markup, Environment, FileSystemLoader
from pyecharts import options as opts
from pyecharts.charts import Bar
app = Flask(__name__, static_folder="templates")
def bar_base() -> Bar:
c = (
Bar()
.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
.add_yaxis("商家B", [15, 25, 16, 55, 48, 8])
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
)
return c
@app.route("/")
def index():
c = bar_base()
return Markup(c.render_embed())
if __name__ == "__main__":
app.run()
运行上述代码,使用浏览器打开http://127.0.0.1:5000 即可访问服务,具体效果如下图所示:
Flask前后端分离
创建flask项目和上述文件目录保持一致,前后端分离的情况下,就需要后端将pyecharts生成的图表返回给前端,方法就是可以将图表生成到一个html文件中,然后返回给前端即可。
需要新建 HTML 文件保存位于项目根目录的 templates 文件夹,这里以如下 index.html 为例. 主要用到了 jquery
和 pyecharts
管理的 echarts.min.js
依赖。
下述是index.html
中的代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Awesome-pyecharts</title>
<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.min.js"></script>
<script type="text/javascript" src="https://assets.pyecharts.org/assets/echarts.min.js"></script>
</head>
<body>
<div id="bar" style="width:1000px; height:600px;"></div>
<script>
$(
function () {
var chart = echarts.init(document.getElementById('bar'), 'white', {renderer: 'canvas'});
$.ajax({
type: "GET",
url: "http://127.0.0.1:5000/barChart",
dataType: 'json',
success: function (result) {
chart.setOption(result);
}
});
}
)
</script>
</body>
</html>
然后就需要编写后端的代码了,包括flask服务以及pyecharts
生成图表,目录结构和模板渲染一致,这里需要注意一个问题,目前由于 json 数据类型的问题,无法将 pyecharts 中的 JSCode 类型的数据转换成 json 数据格式返回到前端页面中使用。因此在使用前后端分离的情况下尽量避免使用 JSCode 进行画图。
下面是server.py
中的后端代码:
from random import randrange
from flask import Flask, render_template
from pyecharts import options as opts
from pyecharts.charts import Bar
app = Flask(__name__, static_folder="templates")
def bar_base() -> Bar:
c = (
Bar()
.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
.add_yaxis("商家A", [randrange(0, 100) for _ in range(6)])
.add_yaxis("商家B", [randrange(0, 100) for _ in range(6)])
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-基本示例", subtitle="我是副标题"))
)
return c
@app.route("/")
def index():
return render_template("index.html")
@app.route("/barChart")
def get_bar_chart():
c = bar_base()
return c.dump_options_with_quotes()
if __name__ == "__main__":
app.run()
同样运行该项目,使用浏览器打开 http://127.0.0.1:5000 即可访问服务。
来源:https://juejin.cn/post/7114474794183032868
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL的数据库数据备份和恢复详解
node命令行服务器(http-server)和跨域的实现
![](https://img.aspxhome.com/file/2023/1/112811_0s.jpg)
Pandas —— resample()重采样和asfreq()频度转换方式
python实现电脑自动关机
触手生春【4.13】CSS中的伪元素选择符
![](https://img.aspxhome.com/file/UploadPic/200811/11/o2008113223147-93s.jpg)
Windows下MySQL安装教程图文详解
![](https://img.aspxhome.com/file/2023/0/78220_0s.jpg)
Python应用库大全总结
Python识别html主要文本框过程解析
![](https://img.aspxhome.com/file/2023/1/95771_0s.jpg)
python数据分析之聚类分析(cluster analysis)
![](https://img.aspxhome.com/file/2023/1/134941_0s.png)
python 日志模块logging的使用场景及示例
![](https://img.aspxhome.com/file/2023/9/121609_0s.png)
bootstrap-table组合表头的实现方法
![](https://img.aspxhome.com/file/2023/8/127178_0s.png)
pytorch 运行一段时间后出现GPU OOM的问题
python使用RNN实现文本分类
![](https://img.aspxhome.com/file/2023/1/96461_0s.jpg)
vue 实现特定条件下绑定事件
python字典操作实例详解
Python实现脚本锁功能(同时只能执行一个脚本)
![](https://img.aspxhome.com/file/2023/0/127700_0s.png)
Python中使用subprocess库创建附加进程
![](https://img.aspxhome.com/file/2023/8/85138_0s.png)
PHP信号处理机制的操作代码讲解
安装MySQL错误归档处理
Python+ Flask实现Mock Server详情
![](https://img.aspxhome.com/file/2023/0/67920_0s.png)