2626import pandas .api .types
2727
2828import bigframes
29- from bigframes ._config import display_options , options
30- import bigframes .dataframe
31- import bigframes .series
29+ from bigframes ._config import options
30+ from bigframes .display import plaintext
31+
32+ if typing .TYPE_CHECKING :
33+ import bigframes .dataframe
34+ import bigframes .series
3235
3336
3437def _is_dtype_numeric (dtype : Any ) -> bool :
@@ -99,53 +102,6 @@ def render_html(
99102 return "\n " .join (table_html )
100103
101104
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-
149105def create_html_representation (
150106 obj : Union [bigframes .dataframe .DataFrame , bigframes .series .Series ],
151107 pandas_df : pd .DataFrame ,
@@ -154,13 +110,22 @@ def create_html_representation(
154110 blob_cols : list [str ],
155111) -> str :
156112 """Create an HTML representation of the DataFrame or Series."""
157- if isinstance (obj , bigframes .series .Series ):
113+ # Note: We need to import Series here to avoid circular imports, but only if we use isinstance.
114+ # To check if it is a Series without importing, we can check if it has the _repr_html_ method
115+ # or rely on duck typing. However, the original code used isinstance.
116+ # Let's import inside the function if needed, or rely on attribute checks.
117+ # But wait, type checking imports are not available at runtime.
118+ # We can check __class__.__name__ or similar, or just import locally.
119+ from bigframes .series import Series
120+
121+ if isinstance (obj , Series ):
158122 pd_series = pandas_df .iloc [:, 0 ]
159123 try :
160124 html_string = pd_series ._repr_html_ ()
161125 except AttributeError :
162126 html_string = f"<pre>{ pd_series .to_string ()} </pre>"
163127 else :
128+ # It's a DataFrame
164129 html_string = obj ._create_html_representation (
165130 pandas_df , total_rows , total_columns , blob_cols
166131 )
@@ -177,8 +142,9 @@ def get_anywidget_bundle(
177142 This function encapsulates the logic for anywidget display.
178143 """
179144 from bigframes import display
145+ from bigframes .series import Series
180146
181- if isinstance (obj , bigframes . series . Series ):
147+ if isinstance (obj , Series ):
182148 df = obj .to_frame ()
183149 else :
184150 df , blob_cols = obj ._get_display_df_and_blob_cols ()
@@ -206,7 +172,9 @@ def get_anywidget_bundle(
206172 total_columns ,
207173 blob_cols if "blob_cols" in locals () else [],
208174 )
209- widget_repr ["text/plain" ] = create_text_representation (obj , cached_pd , total_rows )
175+ widget_repr ["text/plain" ] = plaintext .create_text_representation (
176+ obj , cached_pd , total_rows
177+ )
210178
211179 return widget_repr , widget_metadata
212180
@@ -219,6 +187,8 @@ def repr_mimebundle(
219187 """
220188 Custom display method for IPython/Jupyter environments.
221189 """
190+ from bigframes .series import Series
191+
222192 opts = bigframes .options .display
223193 if opts .repr_mode == "anywidget" :
224194 try :
@@ -231,7 +201,7 @@ def repr_mimebundle(
231201 )
232202
233203 blob_cols : list [str ]
234- if isinstance (obj , bigframes . series . Series ):
204+ if isinstance (obj , Series ):
235205 pandas_df , row_count , query_job = obj ._block .retrieve_repr_request_results (
236206 opts .max_rows
237207 )
@@ -249,6 +219,8 @@ def repr_mimebundle(
249219 obj , pandas_df , row_count , column_count , blob_cols
250220 )
251221
252- text_representation = create_text_representation (obj , pandas_df , row_count )
222+ text_representation = plaintext .create_text_representation (
223+ obj , pandas_df , row_count
224+ )
253225
254226 return {"text/html" : html_string , "text/plain" : text_representation }
0 commit comments