-
Notifications
You must be signed in to change notification settings - Fork 63
feat: display series in anywidget mode #2346
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
Changes from 10 commits
218b7c2
9e3163f
3227b23
c1a8f83
c39293a
9dff0f4
f67e30f
81d1dbe
057f54d
f70d5c1
58e357a
fd04e6a
4825aeb
8845464
d36fc0f
593f9ae
400ea07
945616c
a474606
bd56992
971ee33
1a73628
1b7952b
15b2ac6
f8914c8
9fea10e
a20a5ee
9e92c2a
c0f4b4e
38899b7
64230d1
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 | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |||||||||||||||||||||||||||
| import itertools | ||||||||||||||||||||||||||||
| import numbers | ||||||||||||||||||||||||||||
| import textwrap | ||||||||||||||||||||||||||||
| import traceback | ||||||||||||||||||||||||||||
| import typing | ||||||||||||||||||||||||||||
| from typing import ( | ||||||||||||||||||||||||||||
| Any, | ||||||||||||||||||||||||||||
|
|
@@ -48,6 +49,7 @@ | |||||||||||||||||||||||||||
| import pyarrow as pa | ||||||||||||||||||||||||||||
| import typing_extensions | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| import bigframes._config.display_options as display_options | ||||||||||||||||||||||||||||
| import bigframes.core | ||||||||||||||||||||||||||||
| from bigframes.core import agg_expressions, groupby, log_adapter | ||||||||||||||||||||||||||||
| import bigframes.core.block_transforms as block_ops | ||||||||||||||||||||||||||||
|
|
@@ -568,6 +570,105 @@ def reset_index( | |||||||||||||||||||||||||||
| block = block.assign_label(self._value_column, name) | ||||||||||||||||||||||||||||
| return bigframes.dataframe.DataFrame(block) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def _get_anywidget_bundle( | ||||||||||||||||||||||||||||
| self, include=None, exclude=None | ||||||||||||||||||||||||||||
| ) -> tuple[dict[str, Any], dict[str, Any]]: | ||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||
| Helper method to create and return the anywidget mimebundle for Series. | ||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||
| from bigframes import display | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Convert Series to DataFrame for TableWidget | ||||||||||||||||||||||||||||
| series_df = self.to_frame() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Create and display the widget | ||||||||||||||||||||||||||||
| widget = display.TableWidget(series_df) | ||||||||||||||||||||||||||||
| widget_repr_result = widget._repr_mimebundle_(include=include, exclude=exclude) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Handle both tuple (data, metadata) and dict returns | ||||||||||||||||||||||||||||
| if isinstance(widget_repr_result, tuple): | ||||||||||||||||||||||||||||
| widget_repr, widget_metadata = widget_repr_result | ||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||
| widget_repr = widget_repr_result | ||||||||||||||||||||||||||||
| widget_metadata = {} | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| widget_repr = dict(widget_repr) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Add text representation | ||||||||||||||||||||||||||||
| widget_repr["text/plain"] = self._create_text_representation( | ||||||||||||||||||||||||||||
| widget._cached_data, widget.row_count | ||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
| # At this point, we have already executed the query as part of the | |
| # widget construction. Let's use the information available to render | |
| # the HTML and plain text versions. | |
| widget_repr["text/html"] = self._create_html_representation( | |
| widget._cached_data, | |
| widget.row_count, | |
| len(self.columns), | |
| blob_cols, | |
| ) | |
| widget_repr["text/plain"] = self._create_text_representation( | |
| widget._cached_data, widget.row_count | |
| ) |
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.
Ideally we'd refactor so that the code is shared. One way to do this would be to call the respective Series._create_html_representation or DataFrame._create_html_representation from the helper function.
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.
A lot of this code is nearly identical to the logic in https://github.com/googleapis/python-bigquery-dataframes/blob/main/bigframes/dataframe.py with the following differences:
self.to_frame()before rendering the TableWidget.text/htmlandtext/plain.Could you please refactor
dataframe.pyand this file to use shared helper functions to reduce duplication? https://github.com/googleapis/python-bigquery-dataframes/blob/main/bigframes/display/html.py seems like it'd be and appropriate place for such helpers.