当前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,

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

被折叠的 条评论
为什么被折叠?



