pyecharts源码解读(9)渲染包render之engine模块:渲染引擎

本文详细介绍了 PyEcharts 的 render 包结构及其核心模块 engine,包括渲染设置、HTML 文件生成、渲染接口、嵌入 Notebook 的方法。engine 模块提供了 RenderEngine 类,用于图表的渲染输出,如生成 HTML 文件、嵌入 Notebook,并支持不同 Notebook 类型的输出。此外,还介绍了如何根据图表对象渲染 HTML 和在 Notebook 中展示图表。

当前pyecharts版本为1.9.0

概述

render包结构

render包位于pyecharts包顶级目录中,用于渲染图表。render包结构如下:

├─render # 渲染设置包
│  │  display.py # 定义HTML、JavaScript显示类,用于支持在notebook中嵌入输出结果
│  │  engine.py # 定义渲染相关类、函数
│  │  snapshot.py # 定义渲染图片函数
│  │  __init__.py # 暴露渲染图片函数
│  │
│  ├─templates # 定义渲染模板
│  │      components.html
│  │      macro
│  │      nb_components.html
│  │      nb_jupyter_globe.html
│  │      nb_jupyter_lab.html
│  │      nb_jupyter_lab_tab.html
│  │      nb_jupyter_notebook.html
│  │      nb_jupyter_notebook_tab.html
│  │      nb_nteract.html
│  │      simple_chart.html
│  │      simple_globe.html
│  │      simple_page.html
│  │      simple_tab.html

engine模块

engine模块,路径为pyecharts/render/engine.py,作用为提供渲染接口(HTML和notebook)。

engine模块的结构如下图所示。

  • write_utf8_html_file(file_name: str, html_content: str)函数:根据HTML字符串生成HTML文件。
  • RenderEngine(env: Optional[Environment] = None)类:根据jinja2的环境变量提供渲染抽象接口。
  • render(chart, path: str, template_name: str, env: Optional[Environment], **kwargs) -> str函数:封装RenderEngine(env: Optional[Environment] = None)render_chart_to_file方法输出HTML文件。返回值为输出的HTML文件的路径。
  • render_embed(chart, template_name: str, env: Optional[Environment], **kwargs) -> str函数:封装RenderEngine(env: Optional[Environment] = None)render_chart_to_template方法输出HTML字符串。返回值为字符串。
  • render_notebook(self, notebook_template, lab_template)函数:将图表嵌入到notebook中,针对不同notebook类型提供不同输出方式。notebook和jupyter lab封装RenderEngine(env: Optional[Environment] = None)render_chart_to_notebook方法实现。返回值为pyecharts.render.display.HTML类对象。
  • load_javascript(chart)函数:为jupyter lab提供JavaScript支持,根据chart对象的JavaScript依赖库生成JavaScript代码。返回值为pyecharts.render.display.Javascript类对象。

在这里插入图片描述

engine模块的应用

根据图表对象基类Base的源码可知,图表类的渲染方法底层底层由engine模块的相应类、函数实现。

pyecharts/charts/base.py模块源码

from ..render import engine

class Base(ChartMixin):
    """
    `Base` is the root class for all graphical class, it provides
    part of the initialization parameters and common methods
    """

    def __init__(self, init_opts: Union[InitOpts, dict] = InitOpts()):
        _opts = init_opts
        if isinstance(init_opts, InitOpts):
            _opts = init_opts.opts

    def render(
        self,
        path: str = "render.html",
        template_name: str = "simple_chart.html",
        env: Optional[Environment] = None,
        **kwargs,
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值