|
19 | 19 | import datetime |
20 | 20 | import inspect |
21 | 21 | import itertools |
22 | | -import json |
23 | 22 | import re |
24 | 23 | import sys |
25 | 24 | import textwrap |
|
54 | 53 | import pyarrow |
55 | 54 | import tabulate |
56 | 55 |
|
57 | | -import bigframes._config.display_options as display_options |
58 | 56 | import bigframes.constants |
59 | 57 | import bigframes.core |
60 | 58 | from bigframes.core import agg_expressions, log_adapter |
@@ -790,6 +788,9 @@ def __repr__(self) -> str: |
790 | 788 | if opts.repr_mode == "deferred": |
791 | 789 | return formatter.repr_query_job(self._compute_dry_run()) |
792 | 790 |
|
| 791 | + # TODO(swast): pass max_columns and get the true column count back. Maybe |
| 792 | + # get 1 more column than we have requested so that pandas can add the |
| 793 | + # ... for us? |
793 | 794 | max_results = opts.max_rows |
794 | 795 | pandas_df, row_count, query_job = self._block.retrieve_repr_request_results( |
795 | 796 | max_results |
@@ -826,96 +827,6 @@ def _repr_mimebundle_(self, include=None, exclude=None): |
826 | 827 |
|
827 | 828 | return html.repr_mimebundle(self, include=include, exclude=exclude) |
828 | 829 |
|
829 | | - def _create_text_representation( |
830 | | - self, |
831 | | - pandas_df: pandas.DataFrame, |
832 | | - total_rows: typing.Optional[int], |
833 | | - ) -> str: |
834 | | - """Create a text representation of the DataFrame.""" |
835 | | - opts = bigframes.options.display |
836 | | - with display_options.pandas_repr(opts): |
837 | | - import pandas.io.formats |
838 | | - |
839 | | - to_string_kwargs = ( |
840 | | - pandas.io.formats.format.get_dataframe_repr_params() # type: ignore |
841 | | - ) |
842 | | - if not self._has_index: |
843 | | - to_string_kwargs.update({"index": False}) |
844 | | - to_string_kwargs.update({"show_dimensions": False}) |
845 | | - repr_string = pandas_df.to_string(**to_string_kwargs) |
846 | | - |
847 | | - lines = repr_string.split("\n") |
848 | | - is_truncated = total_rows is not None and total_rows > len(pandas_df) |
849 | | - |
850 | | - if is_truncated: |
851 | | - lines.append("...") |
852 | | - lines.append("") # Add empty line for spacing only if truncated |
853 | | - column_count = len(self.columns) |
854 | | - lines.append(f"[{total_rows or '?'} rows x {column_count} columns]") |
855 | | - else: |
856 | | - # For non-truncated DataFrames, we still need to add dimensions if show_dimensions was False |
857 | | - column_count = len(self.columns) |
858 | | - lines.append("") |
859 | | - lines.append(f"[{total_rows or '?'} rows x {column_count} columns]") |
860 | | - |
861 | | - return "\n".join(lines) |
862 | | - |
863 | | - def _create_html_representation( |
864 | | - self, |
865 | | - pandas_df: pandas.DataFrame, |
866 | | - row_count: int, |
867 | | - column_count: int, |
868 | | - blob_cols: list[str], |
869 | | - ) -> str: |
870 | | - """Create an HTML representation of the DataFrame.""" |
871 | | - opts = bigframes.options.display |
872 | | - with display_options.pandas_repr(opts): |
873 | | - # TODO(shuowei, b/464053870): Escaping HTML would be useful, but |
874 | | - # `escape=False` is needed to show images. We may need to implement |
875 | | - # a full-fledged repr module to better support types not in pandas. |
876 | | - if bigframes.options.display.blob_display and blob_cols: |
877 | | - |
878 | | - def obj_ref_rt_to_html(obj_ref_rt) -> str: |
879 | | - obj_ref_rt_json = json.loads(obj_ref_rt) |
880 | | - obj_ref_details = obj_ref_rt_json["objectref"]["details"] |
881 | | - if "gcs_metadata" in obj_ref_details: |
882 | | - gcs_metadata = obj_ref_details["gcs_metadata"] |
883 | | - content_type = typing.cast( |
884 | | - str, gcs_metadata.get("content_type", "") |
885 | | - ) |
886 | | - if content_type.startswith("image"): |
887 | | - size_str = "" |
888 | | - if bigframes.options.display.blob_display_width: |
889 | | - size_str = f' width="{bigframes.options.display.blob_display_width}"' |
890 | | - if bigframes.options.display.blob_display_height: |
891 | | - size_str = ( |
892 | | - size_str |
893 | | - + f' height="{bigframes.options.display.blob_display_height}"' |
894 | | - ) |
895 | | - url = obj_ref_rt_json["access_urls"]["read_url"] |
896 | | - return f'<img src="{url}"{size_str}>' |
897 | | - |
898 | | - return f'uri: {obj_ref_rt_json["objectref"]["uri"]}, authorizer: {obj_ref_rt_json["objectref"]["authorizer"]}' |
899 | | - |
900 | | - formatters = {blob_col: obj_ref_rt_to_html for blob_col in blob_cols} |
901 | | - |
902 | | - # set max_colwidth so not to truncate the image url |
903 | | - with pandas.option_context("display.max_colwidth", None): |
904 | | - html_string = pandas_df.to_html( |
905 | | - escape=False, |
906 | | - notebook=True, |
907 | | - max_rows=pandas.get_option("display.max_rows"), |
908 | | - max_cols=pandas.get_option("display.max_columns"), |
909 | | - show_dimensions=pandas.get_option("display.show_dimensions"), |
910 | | - formatters=formatters, # type: ignore |
911 | | - ) |
912 | | - else: |
913 | | - # _repr_html_ stub is missing so mypy thinks it's a Series. Ignore mypy. |
914 | | - html_string = pandas_df._repr_html_() # type:ignore |
915 | | - |
916 | | - html_string += f"[{row_count} rows x {column_count} columns in total]" |
917 | | - return html_string |
918 | | - |
919 | 830 | def __delitem__(self, key: str): |
920 | 831 | df = self.drop(columns=[key]) |
921 | 832 | self._set_block(df._get_block()) |
|
0 commit comments