1818
1919import html
2020import traceback
21+ import typing
2122from typing import Any , Union
2223import warnings
2324
2425import pandas as pd
2526import pandas .api .types
2627
2728import bigframes
28- from bigframes ._config import options
29+ from bigframes ._config import display_options , options
2930import bigframes .dataframe
30- from bigframes .display import plaintext
31- import bigframes .formatting_helpers
3231import bigframes .series
3332
3433
@@ -100,6 +99,53 @@ def render_html(
10099 return "\n " .join (table_html )
101100
102101
102+ def create_text_representation (
103+ obj : Union [bigframes .dataframe .DataFrame , bigframes .series .Series ],
104+ pandas_df : pd .DataFrame ,
105+ total_rows : typing .Optional [int ],
106+ ) -> str :
107+ """Create a text representation of the DataFrame or Series."""
108+ opts = bigframes .options .display
109+ with display_options .pandas_repr (opts ):
110+ if isinstance (obj , bigframes .series .Series ):
111+ pd_series = pandas_df .iloc [:, 0 ]
112+ if len (obj ._block .index_columns ) == 0 :
113+ repr_string = pd_series .to_string (
114+ length = False , index = False , name = True , dtype = True
115+ )
116+ else :
117+ repr_string = pd_series .to_string (length = False , name = True , dtype = True )
118+ else :
119+ import pandas .io .formats
120+
121+ to_string_kwargs = (
122+ pandas .io .formats .format .get_dataframe_repr_params () # type: ignore
123+ )
124+ if not obj ._has_index :
125+ to_string_kwargs .update ({"index" : False })
126+ to_string_kwargs .update ({"show_dimensions" : False })
127+ repr_string = pandas_df .to_string (** to_string_kwargs )
128+
129+ lines = repr_string .split ("\n " )
130+ is_truncated = total_rows is not None and total_rows > len (pandas_df )
131+
132+ if is_truncated :
133+ lines .append ("..." )
134+ lines .append ("" ) # Add empty line for spacing only if truncated
135+ if isinstance (obj , bigframes .series .Series ):
136+ lines .append (f"[{ total_rows } rows]" )
137+ else :
138+ column_count = len (obj .columns )
139+ lines .append (f"[{ total_rows or '?' } rows x { column_count } columns]" )
140+ elif isinstance (obj , bigframes .dataframe .DataFrame ):
141+ # For non-truncated DataFrames, we still need to add dimensions if show_dimensions was False
142+ column_count = len (obj .columns )
143+ lines .append ("" )
144+ lines .append (f"[{ total_rows or '?' } rows x { column_count } columns]" )
145+
146+ return "\n " .join (lines )
147+
148+
103149def create_html_representation (
104150 obj : Union [bigframes .dataframe .DataFrame , bigframes .series .Series ],
105151 pandas_df : pd .DataFrame ,
@@ -108,9 +154,17 @@ def create_html_representation(
108154 blob_cols : list [str ],
109155) -> str :
110156 """Create an HTML representation of the DataFrame or Series."""
111- return obj ._create_html_representation (
112- pandas_df , total_rows , total_columns , blob_cols
113- )
157+ if isinstance (obj , bigframes .series .Series ):
158+ pd_series = pandas_df .iloc [:, 0 ]
159+ try :
160+ html_string = pd_series ._repr_html_ ()
161+ except AttributeError :
162+ html_string = f"<pre>{ pd_series .to_string ()} </pre>"
163+ else :
164+ html_string = obj ._create_html_representation (
165+ pandas_df , total_rows , total_columns , blob_cols
166+ )
167+ return html_string
114168
115169
116170def get_anywidget_bundle (
@@ -152,19 +206,19 @@ def get_anywidget_bundle(
152206 total_columns ,
153207 blob_cols if "blob_cols" in locals () else [],
154208 )
155- widget_repr ["text/plain" ] = plaintext .create_text_representation (
156- obj , cached_pd , total_rows
157- )
209+ widget_repr ["text/plain" ] = create_text_representation (obj , cached_pd , total_rows )
158210
159211 return widget_repr , widget_metadata
160212
161213
162- def repr_mimebundle_head (
214+ def repr_mimebundle (
163215 obj : Union [bigframes .dataframe .DataFrame , bigframes .series .Series ],
164216 include = None ,
165217 exclude = None ,
166218):
167- """Mimebundle display for the start of the data."""
219+ """
220+ Custom display method for IPython/Jupyter environments.
221+ """
168222 opts = bigframes .options .display
169223 if opts .repr_mode == "anywidget" :
170224 try :
@@ -195,37 +249,6 @@ def repr_mimebundle_head(
195249 obj , pandas_df , row_count , column_count , blob_cols
196250 )
197251
198- text_representation = plaintext .create_text_representation (
199- obj , pandas_df , row_count
200- )
252+ text_representation = create_text_representation (obj , pandas_df , row_count )
201253
202254 return {"text/html" : html_string , "text/plain" : text_representation }
203-
204-
205- def repr_mimebundle_deferred (
206- obj : Union [bigframes .dataframe .DataFrame , bigframes .series .Series ],
207- include = None ,
208- exclude = None ,
209- ):
210- """Mimebundle display for deferred execution mode."""
211- # We don't need the mimetype for the deferred case, but we need to match
212- # the signature of the other repr_mimebundle_* methods.
213- # TODO(swast): Add an HTML representation for deferred queries that simply
214- # prints the SQL.
215- query_job = obj ._compute_dry_run ()
216- text_representation = bigframes .formatting_helpers .repr_query_job (query_job )
217- return {"text/plain" : text_representation }
218-
219-
220- def repr_mimebundle (
221- obj : Union [bigframes .dataframe .DataFrame , bigframes .series .Series ],
222- include = None ,
223- exclude = None ,
224- ):
225- """
226- Custom display method for IPython/Jupyter environments.
227- """
228- if options .display .repr_mode == "deferred" :
229- return repr_mimebundle_deferred (obj , include = include , exclude = exclude )
230- else :
231- return repr_mimebundle_head (obj , include = include , exclude = exclude )
0 commit comments