三、菜鸟实战

马上安排!

1、创建 python 文件

"""
Author: 菜鸟实战
实战场景:  如何绘制饼状图分析商品库存
"""

# 导入系统包
import platform
from flask import Flask, render_template
from pyecharts import options as opts
from pyecharts.charts import *
from pyecharts.faker import Faker

print("Hello,菜鸟实战")
print("实战场景:  如何绘制饼状图分析商品分类 \n")

web = Flask(__name__)

# 数据构建
x_data = ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
y_data = [1186, 1246, 1346, 1086, 1430, 1064]

data = [[x_data[i], y_data[i]] for i in range(len(x_data))]


def pie_charts() -> Pie():
    # 实例化对象
    pie = Pie()
    # 采用Flask库直接获取颜色
    pie.add("", data, color=Faker.rand_color())
    # 全局置标题、标签
    pie.set_global_opts(
        title_opts=opts.TitleOpts(title="如何绘制饼状图分析商品分类", subtitle="菜鸟实战,坚持学习!"),
        legend_opts=opts.LegendOpts(type_="scroll", orient="vertical", pos_top="20%", pos_left="0%")
    )
    return pie


# 获取对象
p = pie_charts()
# 绘制图形,生成HTML文件的
p.render('./templates/pie_charts.html')


# 添加路由显示图表
@web.route('/')
def index():
    return render_template('pie_charts.html')


if __name__ == "__main__":
    # 运行项目
    web.run(debug=False)

print("Python 版本", platform.python_version())

2、运行结果

Hello,菜鸟实战

实战场景:  如何绘制饼状图分析商品分类
* Serving Flask app ‘py032’ (lazy loading)
  * Environment: production
    WARNING: This is a development server. Do not use it in a production deployment.
    Use a production WSGI server instead.
  * Debug mode: off
  * Running on http://127.0.0.1:5000 (Press CTRL+C to quit)