-
Notifications
You must be signed in to change notification settings - Fork 417
feat: implement _repr_mimebundle_ for DataFrame #6385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Issue #2598 可行性分析 | ||
|
|
||
| ## 1. issue summary | ||
| 为 `daft.DataFrame` 增加 `_repr_mimebundle_`,让不支持 `_repr_html_` 的 Jupyter 前端(例如 Zed)也能正确展示 DataFrame。 | ||
|
|
||
| ## 2. root cause | ||
| 当前 `DataFrame` 只提供 `__repr__` 和 `_repr_html_`,部分前端优先走 `_repr_mimebundle_` 协议,缺失该协议会导致展示降级或不可用。 | ||
|
|
||
| ## 3. expected modification modules | ||
| - `daft/dataframe/dataframe.py` | ||
| - `tests/dataframe/test_repr.py` | ||
|
|
||
| ## 4. implementation plan | ||
| 1. 在 `DataFrame` 上新增 `_repr_mimebundle_(include=None, exclude=None)`。 | ||
| 2. 默认返回 `text/plain` 与 `text/html` 两种 mime。 | ||
| 3. 支持 `include`/`exclude` 过滤,兼容 IPython display 协议。 | ||
| 4. 新增测试覆盖默认返回与 include/exclude 过滤行为。 | ||
|
|
||
| ## 复杂度评估 | ||
| - 预计修改文件数:2(<20) | ||
| - API 设计变更:无(仅新增兼容显示协议方法) | ||
| - 架构调整:无 | ||
| - 多模块重构:无 | ||
|
|
||
| 结论:可直接实现,风险低。 | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -275,6 +275,24 @@ def test_repr_with_html_string(): | |
| ) | ||
|
|
||
|
|
||
| def test_repr_mimebundle_contains_plain_and_html(make_df): | ||
| df = make_df({"A": [1, 2, 3], "B": ["x", "y", "z"]}) | ||
|
|
||
| bundle = df._repr_mimebundle_() | ||
|
|
||
| assert set(bundle.keys()) == {"text/plain", "text/html"} | ||
| assert bundle["text/plain"] == df.__repr__() | ||
| assert bundle["text/html"] == df._repr_html_() | ||
|
|
||
|
|
||
| def test_repr_mimebundle_include_exclude(make_df): | ||
| df = make_df({"A": [1]}) | ||
|
|
||
| assert set(df._repr_mimebundle_(include={"text/plain"}).keys()) == {"text/plain"} | ||
| assert set(df._repr_mimebundle_(exclude={"text/html"}).keys()) == {"text/plain"} | ||
| assert df._repr_mimebundle_(include={"text/plain"}, exclude={"text/plain"}) == {} | ||
|
||
|
|
||
|
|
||
| class MyObj: | ||
| def __repr__(self) -> str: | ||
| return "myobj-custom-repr" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eager evaluation of both representations regardless of
includefilterBoth
__repr__()and_repr_html_()are computed up-front before anyinclude/excludefiltering is applied. This means that even when a caller specifiesinclude={"text/plain"}, the potentially expensive_repr_html_()call (which may generate an interactive HTML widget) is still executed unconditionally.Consider building the bundle lazily so that only the representations that will survive the filter are generated:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix