You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VSCode的Jupyter中无需rich.print恢复彩色输出?

恢复Kedro 0.18.5升级后Jupyter Notebook的默认彩色输出

问题背景

升级Kedro从0.18.4到0.18.5后,VSCode中的Jupyter Notebook失去了默认输出的Rich彩色效果,仅显式调用pretty.Pretty()、pretty.pprint()或rich.print()才有彩色,普通输出仍为白色。已尝试安装rich[jupyter]并初始化Rich,但无效。

解决方案

1. 强制替换Jupyter默认文本格式化器

在Jupyter单元格中运行以下代码,让所有普通输出通过Rich的Pretty类渲染:

from rich.pretty import Pretty
from IPython.core.formatters import PlainTextFormatter

# 覆盖默认的对象文本渲染逻辑
PlainTextFormatter().type_printers[object] = lambda obj, pp, cycle: pp.text(Pretty(obj).text)

2. 手动注册Rich的Jupyter集成

如果%load_ext rich未生效,尝试手动安装Jupyter渲染支持:

from rich.jupyter import install_jupyter
install_jupyter()
# 同时重新初始化pretty和traceback
from rich import pretty, traceback
pretty.install(indent_guides=True, expand_all=True)
traceback.install(show_locals=True, indent_guides=True)

3. 回退Rich版本至Kedro 0.18.4兼容版本

Kedro 0.18.5可能升级了Rich依赖,导致行为变化。先确认Kedro 0.18.4对应的Rich兼容版本(可通过旧环境记录或PyPI Kedro 0.18.4依赖清单查询),比如假设兼容版本为12.6.0,执行:

pip install rich==12.6.0

重启Jupyter内核后验证效果。

4. 检查VSCode Jupyter设置

确保VSCode的Jupyter富输出功能开启:

  • 打开VSCode设置(快捷键Ctrl+,)
  • 搜索Jupyter: Enable Rich Output
  • 勾选该选项,重启VSCode后测试输出。

内容的提问来源于stack exchange,提问作者matt91t

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 22:57:16