flask基于echarts数据可视化大屏
时间: 2023-06-28 12:11:59 浏览: 93
使用 Flask 和 Echarts 可以很方便地搭建一个数据可视化大屏。以下是一个简单的示例:
1. 安装 Flask 和 Echarts:
```
pip install flask
pip install pyecharts
```
2. 创建 Flask 应用和路由:
```python
from flask import Flask, render_template
from pyecharts import options as opts
from pyecharts.charts import Bar
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/data')
def data():
# 生成数据
x = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
y = [820, 932, 901, 934, 1290, 1330, 1320]
# 生成图表
bar = (
Bar()
.add_xaxis(x)
.add_yaxis('Sales', y)
.set_global_opts(title_opts=opts.TitleOpts(title='Weekly Sales'))
)
# 渲染图表
return bar.render_embed()
```
3. 创建 HTML 模板:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Flask Echarts Demo</title>
<script src="https://cdn.jsdelivr.net/npm/echarts@5.2.0/dist/echarts.min.js"></script>
</head>
<body>
<div id="chart" style="width: 800px; height: 600px;"></div>
<script>
var chart = echarts.init(document.getElementById('chart'));
chart.showLoading();
fetch('/data').then(function (response) {
response.text().then(function (text) {
chart.hideLoading();
chart.setOption(JSON.parse(text));
});
});
</script>
</body>
</html>
```
4. 启动 Flask 应用:
```python
if __name__ == '__main__':
app.run()
```
5. 在浏览器中访问 http://localhost:5000 ,即可看到数据可视化大屏。
注意:以上示例中的数据和图表仅作为演示用途,实际应用中需要根据具体需求进行修改。