Rakuish — ADR-0076-charts-and-mime

ADR-0076:图表生态接入与 Jupyter 显示协议(V-370)

  • 状态:已采纳(0.161.0)

  • 日期:2026-12-01

  • 相关:ADR-0074(图像回流 / base64 + 魔数)、ADR-0048(对象显示协议 P1)、ADR-0073(借生态三层 / L3 桥)、ADR-0075(Arrow 专线与 py 命令)

  • 测试:t/101-charts.t(25 条)、t/100-py-command.t(41 条)、t/99-py-tables-images.t(35 条)

1. 背景

用户的原话是:「终端看图这个我觉得不算刚需,关键是能不能接上 matplot 这类图表生态」。

这句话把问题从「终端怎么画图」改成了「图表对象怎么离开 Python、到能显示它的地方去」。 0.159.0 已经有 image-out 与 Image.to-mime(ADR-0074),matplotlib 的 Figure 可以走 savefig 变成 PNG 的 base64 —— 但那只覆盖 matplotlib 一家, 而且把"图"理解成了"位图"。plotly 这类库的产物是 mime 包(HTML / SVG / JSON), 硬压成 PNG 会丢交互与矢量精度。

2. 决定一:不实现终端图像渲染,一律走 mime 包

终端里的"图"最终只能靠 half-block 或 ANSI 艺术近似,信息量远低于原图,还要引入 终端能力探测、像素比、缩放等一整套问题。而 Rakuish 已经有一条完整的 mime 通路:

Python 对象 --(_repr_*)--> MimeBundle --to-mime--> Display 的 extra-mime
        --> Bundle.to-mime --> kernel/notebook 的 data 字典 --> Jupyter 前端渲染

所以本次只做「把图变成 mime 包」,不新增任何展示分支(Display.rakumod 的 bundle 仍然只认 ADR-0048 的 P1 对象协议)。终端里 py --mime 显示的是 text/plain 摘要(如 <Figure>(mime: text/html, image/png)),图本身在 notebook / html 里可见。这与 ADR-0074 的 Image 完全同构。

3. 决定二:通用 Jupyter 协议优先,而不是逐库适配

Python 生态里"能把对象变成 mime 包"的钩子不止一个:

钩子谁在用形态
_repr_mimebundle_(include, exclude)规范入口dict{mime: data}
_repr_png_ / _repr_svg_ / _repr_html_ / _repr_json_ / _repr_latex_matplotlib(png)、plotly(html)、sympy(latex)单档
savefig(path)matplotlib Figure文件

实测依据(这条决定了不能只认 mimebundle):plotly 7.2.0 的 _repr_mimebundle_() 返回空 dict,但 _repr_html_() 返回 7,425 字符的 HTML 片段。只认 mimebundle 会漏掉整个 plotly;只认 savefig 会漏掉 plotly 与 sympy。所以 worker 的 _repr_bundle 按上表顺序合并:

b = obj._repr_mimebundle_()            # 第一档
if not b and has _repr_png_/…: 逐个取单档
if 仍为空 and has savefig:     savefig 到临时 png,读 base64

合并而不是"第一个非空就停",是因为 matplotlib 可能同时提供 png 与 svg, 多给一档没有代价(上限见 §5)。

4. 决定三:MPLBACKEND 用 setdefault 设成 Agg

本机 matplotlib 的默认后端是 tkagg —— 在 worker 里 plt.subplots() 会尝试开窗口, 无显示环境时直接抛 ImportError: Cannot load backend 'TkAgg',有显示环境时又会弹窗。 worker 启动处:

os.environ.setdefault("MPLBACKEND", "Agg")

用 setdefault 而不是直接赋值,是为了尊重用户显式设置的 MPLBACKEND (例如他们想要 pdf 或 cairo)。

5. 决定四:单档上限 1 MiB 字符,超了记账不截断

mime 包要经过 JSON 文本进 Raku,再进 notebook 的 data 字典。一张大图 base64 后几 MB 是常态。MAX_MIME_CHARS = 1_000_000,超出的档整档丢弃, 名字记进 MimeBundle.dropped,py --mime 的结果里能看到 —— 不静默截断 (截断的 base64 是坏数据,比没有更糟)。

6. 决定五:失败要给"下一步"

py --mime <expr> 拿不到任何档时,错误里带 _plot_hint():如果命名空间里 存在 plt,提示「你刚画过图:用 py --image plt.gcf()」。这不是猜测: plt.gcf() 是当前 Figure 的入口,而 py ax.plot(...) 的返回值是 Axes (不可 JSON 化、也没有 _repr_*),这正是新手最容易卡住的地方。

7. 没采用的方案

方案为什么不做
终端图像渲染(half-block / kitty 协议)用户明确说不是刚需;信息量低、终端能力分支多
只为 matplotlib 写 savefig 适配plotly / sympy / altair 全部漏掉,而通用钩子已经够用
只认 _repr_mimebundle_plotly 7 实测返回空 dict(§3)
在 worker 里把 HTML 压成 PNG丢交互与矢量精度,还要引入浏览器依赖
把 MimeBundle 塞进 Display 的新分支违反 ADR-0048:显示层只认 to-mime,不认类型白名单

8. 不变量

  1. MimeBundle.to-mime 返回的字典只含 mime => 字符串;没有对象、没有内存地址。

  2. 终端可见摘要(Str)不含 base64、不含 ANSI(t/101 第 25 条)。

  3. py --image 的四种来源与 ADR-0074 完全一致,不因本次改动放宽。

  4. py --mime 拿不到档时 ok=False,且错误含可执行的下一步提示。

  5. MPLBACKEND 只在未设置时生效。

  6. 单档超过 1 MiB 时丢弃并记入 dropped,不截断。

  7. 进程内常驻:matplotlib 的 Figure 在 worker 里跨命令存活(py plt.gcf() 直接可用)。

9. 下一步

  • 若 notebook 侧要支持交互式 plotly(application/vnd.plotly.v1+json), 只需在 worker 的钩子表里加一档,Raku 侧无需改动(mime 通路已经通用)。

  • py --mime 目前只作用于表达式;语句形式的图(不支持尾表达式)仍需 py --image 或先赋值再 --mime 变量名。

Rakuish v0.161.4

Raku 原生的命令与脚本环境(API v3):Raku 表达式 + `!` argv、会话级 Sys 文件 / 进程 API、DSL 短命令、可扩展 Command 注册表、无 magic 的 REPL、全权限本地执行与统一审计。v2 的 bash 子集 / %magic / 独立 shell 引擎已于 0.34.0 整体删除,只剩一套语义。

Authors

  • zef:skyter10086

License

MIT

Dependencies

JSON::FastTerminal::LineEditorTerminal::APIArrow::IPC:ver<0.1.0+>

Test Dependencies

Provides

  • Rakuish::Audit
  • Rakuish::CLI
  • Rakuish::Command
  • Rakuish::DSL
  • Rakuish::Data
  • Rakuish::Data::Arrow
  • Rakuish::Data::CsvOptions
  • Rakuish::Data::Cursor
  • Rakuish::Data::Duck
  • Rakuish::Data::Engine
  • Rakuish::Data::Engine::DuckDB
  • Rakuish::Data::Engine::SQLite
  • Rakuish::Data::Pager
  • Rakuish::Data::Stream
  • Rakuish::Data::Table
  • Rakuish::Display
  • Rakuish::Eval
  • Rakuish::Eval::Protocol
  • Rakuish::Eval::Proxy
  • Rakuish::Eval::Server
  • Rakuish::Incomplete
  • Rakuish::Kernel
  • Rakuish::Kernel::Factory
  • Rakuish::Kernel::Notebook
  • Rakuish::Py
  • Rakuish::REPL::Chrome
  • Rakuish::REPL::Complete
  • Rakuish::REPL::Highlight
  • Rakuish::REPL::Hints
  • Rakuish::REPL::Introspect
  • Rakuish::REPL::Keymap
  • Rakuish::REPL::Methods
  • Rakuish::REPL::Plugins
  • Rakuish::REPL::Session
  • Rakuish::REPL::Session::Cells
  • Rakuish::REPL::Session::Commands
  • Rakuish::REPL::Session::Completion
  • Rakuish::REPL::Session::Config
  • Rakuish::REPL::Session::Diagnostics
  • Rakuish::REPL::Session::Exec
  • Rakuish::REPL::Session::Persistence
  • Rakuish::REPL::Session::Replay
  • Rakuish::REPL::Shell
  • Rakuish::REPL::SqlComplete
  • Rakuish::Result
  • Rakuish::Result::Renderer
  • Rakuish::Snapshot
  • Rakuish::Store
  • Rakuish::Sys
  • Rakuish::Sys::Console
  • Rakuish::Sys::FS
  • Rakuish::Sys::JobTable
  • Rakuish::Sys::PosixSpawn
  • Rakuish::Sys::Proc
  • Rakuish::Sys::Term
  • Rakuish::Sys::WinJob

Documentation

The Camelia image is copyright 2009 by Larry Wall. "Raku" is a trademark of the Yet Another Society. All rights reserved.

Built with Podlite — the markup and publishing tools behind this site.