pyecharts源码解读(2)“全局变量”模块globals,修改页面标题

当前pyecharts版本为1.9.0

概述

globals模块位于pyecharts包顶级目录中,文件名为globals.py。用于定义pyecharts的“全局变量”。

globals模块按照Python常用的设计模式,把在全局范围内使用的变量定义在单一模块中的类中。

模块包含11个“私有”类、12个变量,除ToolTipFormatterType外,其余变量均为对应类的实例。

# 渲染方式
RenderType = _RenderType()
# 允许的生成的文件类型
FileType = _FileType()
# Symbol 样式类型
SymbolType = _SymbolType()
# 图表类型
ChartType = _ChartType
# Tooltip 格式器类型
TooltipFormatterType = _ToolTipFormatterType()
# 主题类型
ThemeType = _ThemeType()
# Geo 图形类型
GeoType = _GeoType()
# BMap 图形全局参数
BMapType = _BMapType
# Notebook 环境类型
NotebookType = _NotebookType()
# 远程资源 Host
OnlineHostType = _OnlineHost()
# Warning 控制
WarningType = _WarningControl()
# 全局环境唯一实例
CurrentConfig = _CurrentConfig()

在这里插入图片描述

全局环境实例CurrentConfig

在所有“全局”变量中,最重要的可能是CurrentConfig,它为pyecharts图表实例提供了部分全局默认值注意是部分,这意味着并不是说在导入图表类之前修改某些CurrentConfig属性就可以影响某些初始值!
先说结论:ONLINE_HOSTNOTEBOOK_TYPE支持直接修改初始值,而PAGE_TITLE则无效。

class _CurrentConfig:
    PAGE_TITLE = "Awesome-pyecharts"
    ONLINE_HOST = OnlineHostType.DEFAULT_HOST
    NOTEBOOK_TYPE = NotebookType.JUPYTER_NOTEBOOK
    GLOBAL_ENV = Environment(
        keep_trailing_newline=True,
        trim_blocks=True,
        lstrip_blocks=True,
        loader=FileSystemLoader(
            os.path.join(
                os.path.abspath(os.path.dirname(__file__)), "render", "templates"
            )
        ),
    )


CurrentConfig = _CurrentConfig()

案例:验证修改全局变量是否影响图表实例的初始值

代码

from pyecharts.globals import CurrentConfig, NotebookType
CurrentConfig.ONLINE_HOST = "http://127.0.0.1:8000/assets/"
CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB
CurrentConfig.PAGE_TITLE = "test"
from pyecharts.charts import Bar
bar = Bar()
print(CurrentConfig.ONLINE_HOST,CurrentConfig.NOTEBOOK_TYPE,CurrentConfig.PAGE_TITLE)
print(bar.js_host,CurrentConfig.NOTEBOOK_TYPE,bar.page_title)

输出

http://127.0.0.1:8000/assets/ jupyter_lab test
http://127.0.0.1:8000/assets/ jupyter_lab Awesome-pyecharts

原因分析

根据pyecharts\options\global_options.pypyecharts\charts\base.py源码可知,图表类在实例化时,默认配置是从InitOpts类实例中获取的,page_title配置项读取的是CurrentConfig第一次实例化时的CurrentConfig.PAGE_TITLE值,随后再修改CurrentConfig.PAGE_TITLE无济于事。
因此,修改页面标题的方法应当直接修改图表实例对象的page_title属性或者在图表类实例化时在init_opts参数中修改page_title属性。

代码
from pyecharts.charts import Bar
from pyecharts import options as opts

bar = Bar(init_opts=opts.InitOpts(page_title="a"))
print(bar.page_title)
bar.page_title="b"
print(bar.page_title)
输出
a
b
相关源码

pyecharts\options\global_options.py

class InitOpts(BasicOpts):
    def __init__(
        self,
        width: str = "900px",
        height: str = "500px",
        chart_id: Optional[str] = None,
        renderer: str = RenderType.CANVAS,
        page_title: str = CurrentConfig.PAGE_TITLE,
        theme: str = ThemeType.WHITE,
        bg_color: Union[str, dict] = None,
        js_host: str = "",
        animation_opts: Union[AnimationOpts, dict] = AnimationOpts(),
    ):
        self.opts: dict = {
            "width": width,
            "height": height,
            "chart_id": chart_id,
            "renderer": renderer,
            "page_title": page_title,
            "theme": theme,
            "bg_color": bg_color,
            "js_host": js_host,
            "animationOpts": animation_opts,
        }

pyechartscharts\base.py

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

        self.width = _opts.get("width", "900px")
        self.height = _opts.get("height", "500px")
        self.renderer = _opts.get("renderer", RenderType.CANVAS)
        self.page_title = _opts.get("page_title", CurrentConfig.PAGE_TITLE)
        self.theme = _opts.get("theme", ThemeType.WHITE)
        self.chart_id = _opts.get("chart_id") or uuid.uuid4().hexself.options: dict = {}
        self.js_host: str = _opts.get("js_host") or CurrentConfig.ONLINE_HOST
        self.js_functions: utils.OrderedSet = utils.OrderedSet()
        self.js_dependencies: utils.OrderedSet = utils.OrderedSet("echarts")
        self.options.update(backgroundColor=_opts.get("bg_color"))
        self.options.update(_opts.get("animationOpts", AnimationOpts()).opts)
        self._is_geo_chart: bool = False


  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值