Pyecharts生成图片--无需配置环境变量

举报
yd_210205184 发表于 2026/11/06 15:16:49 2026/11/06
【摘要】 Pyecharts是采用Python调用echarts生成图片,echarts为前端可视化内容,所以生成图片的文件格式为html,现有两种方式生成图片:采用selenium生成图片采用phantomjs生成图片采用pyppeteer生成图片这里我采用的phantomjs方式,相对来说该工具较为熟悉.1.首先需要安装Pyecharts生成图片的依赖:        pip install sn...

Pyecharts是采用Python调用echarts生成图片,echarts为前端可视化内容,所以生成图片的文件格式为html,现有两种方式生成图片:

  • 采用selenium生成图片
  • 采用phantomjs生成图片
  • 采用pyppeteer生成图片


这里我采用的phantomjs方式,相对来说该工具较为熟悉.

1.首先需要安装Pyecharts生成图片的依赖:
        pip install snapshot-phantomjs

2.代码使用方式:
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.render import make_snapshot

from snapshot_phantomjs import snapshot

def bar_chart() -> Bar:
    c = (
        Bar()
        .add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"])
        .add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
        .add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49])
        .reversal_axis()
        .set_series_opts(label_opts=opts.LabelOpts(position="right"))
        .set_global_opts(title_opts=opts.TitleOpts(title="Bar-测试渲染图片"))
    )
    return c

make_snapshot(snapshot, bar_chart().render(), "bar0.png")
3.由于作者比较懒,不想设置太多无用的环境变量,同时该方式同时需要将phantomjs执行文件设置环境变量,就比较麻烦.这里我的思路是既然需要设置环境变量那么代码中肯定有调用的地方以及位置,直接找到源码中调用的位置,重写调用方式,以减少设置环境变量的步骤.3.1找源码调用phantomjs工具调用位置

from snapshot_phantomjs import snapshot
from pyecharts.render import make_snapshot
make_snapshot(snapshot)
Pyecharts make_snapshot()调用了snapshot_phantomjs的make_snapshot方法

def make_snapshot(
    html_path: str, file_type: str, delay: int = 2, pixel_ratio: int = 2, **_
):
    chk_phantomjs()
    logger.info("Generating file ...")
    proc_params = [
        PHANTOMJS_EXEC,
        os.path.join(get_resource_dir("phantomjs"), "snapshot.js"),
        to_file_uri(html_path),
        file_type,
        str(int(delay * 1000)),
        str(pixel_ratio),
    ]
    proc = subprocess.Popen(
        proc_params,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        # shell=True will make Windows happy.
        shell=get_shell_flag(),
    )
    content = io.TextIOWrapper(proc.stdout, encoding="utf-8").read()
    return content
查看snapshot_phantomjs.snapshot中make_snapshot()的源码:
PHANTOMJS_EXEC = "phantomjs"


def make_snapshot(
    html_path: str, file_type: str, delay: int = 2, pixel_ratio: int = 2, **_
):
    chk_phantomjs()
    logger.info("Generating file ...")
    proc_params = [
        PHANTOMJS_EXEC,
        os.path.join(get_resource_dir("phantomjs"), "snapshot.js"),
        to_file_uri(html_path),
        file_type,
        str(int(delay * 1000)),
        str(pixel_ratio),
    ]
    proc = subprocess.Popen(
        proc_params,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        # shell=True will make Windows happy.
        shell=get_shell_flag(),
    )
    content = io.TextIOWrapper(proc.stdout, encoding="utf-8").read()
    return content从这里调用了PHANTOMJS_EXEC也就是phantomjs工具,这就是需要更改的代码了,这里我们更改为我们存放的phantomjs文件位置就好了,开始重写该函数
IMAGES_BASE_DIR = os.path.dirname(os.path.abspath(__file__))

PJS_BASE_DIR = os.path.join(IMAGES_BASE_DIR, "tools")

EXE_PHANTOMJS_EXEC = "phantomjs.exe"
LINUX_64_PHANTOMJS_EXEC = "phantomjs"

if platform.system() == "Windows":
    PHANTOMJS_EXEC = os.path.join(PJS_BASE_DIR, "phantomjs.exe")
else:
    PHANTOMJS_EXEC = os.path.join(PJS_BASE_DIR, "phantomjs")
os.chmod(PHANTOMJS_EXEC, 0o755)


def make_snapshot(
        html_path: str, file_type: str, delay: int = 2, pixel_ratio: int = 2, **_
):
    chk_phantomjs()
    logger.info("Generating file ...")
    proc_params = [
        PHANTOMJS_EXEC,
        os.path.join(PJS_BASE_DIR, "snapshot.js"),
        to_file_uri(html_path),
        file_type,
        str(int(delay * 1000)),
        str(pixel_ratio),
    ]
    proc = subprocess.Popen(
        proc_params,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        # shell=True will make Windows happy.
        shell=get_shell_flag(),
    )
    content = io.TextIOWrapper(proc.stdout, encoding="utf-8").read()
    return content重写完成,之后更改调用改为重写的图片生成代码调用:
make_snapshot(
    my_snapshot, c.render(os.path.join(my_snapshot.IMAGES_BASE_DIR, "images_tmp", "render.html")),
    os.path.join(images_tmp_path, "1.png"),

【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0)

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。