-
Notifications
You must be signed in to change notification settings - Fork 63
feat: Display custom multi-index in anywidget mode #2250
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?
Conversation
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
441bb94 to
33fd6e3
Compare
bigframes/display/html.py
Outdated
| def _calculate_rowspans(dataframe: pd.DataFrame) -> list[list[int]]: | ||
| """Calculates the rowspan for each cell in a MultiIndex DataFrame. | ||
| Args: | ||
| dataframe (pd.DataFrame): | ||
| The DataFrame for which to calculate index rowspans. | ||
| Returns: | ||
| list[list[int]]: | ||
| A list of lists, where each inner list corresponds to an index level | ||
| and contains the rowspan for each row at that level. A value of 0 | ||
| indicates that the cell should not be rendered (it's covered by a | ||
| previous rowspan). | ||
| """ | ||
| if not isinstance(dataframe.index, pd.MultiIndex): | ||
| # If not a MultiIndex, no rowspans are needed for the index itself. | ||
| # Return a structure that indicates each index cell should be rendered once. | ||
| return [[1] * len(dataframe.index)] if dataframe.index.nlevels > 0 else [] | ||
|
|
||
| rowspans: list[list[int]] = [] | ||
| for level_idx in range(dataframe.index.nlevels): | ||
| current_level_spans: list[int] = [] | ||
| current_value = None | ||
| current_span = 0 | ||
|
|
||
| for i in range(len(dataframe.index)): | ||
| value = dataframe.index.get_level_values(level_idx)[i] | ||
|
|
||
| if value == current_value: | ||
| current_span += 1 | ||
| current_level_spans.append(0) # Mark as covered by previous rowspan | ||
| else: | ||
| # If new value, finalize previous span and start a new one | ||
| if current_span > 0: | ||
| # Update the rowspan for the start of the previous span | ||
| current_level_spans[i - current_span] = current_span | ||
| current_value = value | ||
| current_span = 1 | ||
| current_level_spans.append(0) # Placeholder, will be updated later | ||
|
|
||
| # Finalize the last span | ||
| if current_span > 0: | ||
| current_level_spans[len(dataframe.index) - current_span] = current_span | ||
|
|
||
| rowspans.append(current_level_spans) | ||
|
|
||
| return rowspans | ||
|
|
||
|
|
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.
While this makes for a somewhat cleaner view, I think we'd be better off not implementing rowpans. For example sorting by the various index columns is less intuitive if the index isn't rendered the same as other columns.
bigframes/display/html.py
Outdated
| table_html.append(' <tr style="text-align: left;">') | ||
|
|
||
| # Add index headers | ||
| for name in dataframe.index.names: |
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.
Let's try to actually refer to the original BigFrames DataFrame when deciding which index columns to render or not. In particular, we should not render the index if the BigFrame DataFrame has a NULL index.
This reverts commit 0353dc5.
…572fcdbbb01d7cda1929cb
…mode.ipynb" This reverts commit 5919552aa4158bdf4c3de12b12270f75314b8418.
This PR introduces several improvements related to the
anywidgetdisplay mode. For displaying multi-index DataFrames in anywidget mode, it will be rendered with a nested "semi-exploding" view, similar to BigQuery web UI.Added Index and Multi-Index DataFrame Example to Notebook:
notebooks/dataframes/anywidget_mode.ipynbnotebook now includes a new section demonstrating how to display a DataFrame with a MultiIndex using real data from the PyPI public dataset. This enhances the notebook's examples for complex data structures.Added Index and Multi-Index System Tests:
See an example here: screen/6nyCvGjzpRwM2nW
Fixes #<459515995> 🦕