2929import bigframes
3030from bigframes ._config import display_options , options
3131from bigframes .display import plaintext
32+ import bigframes .formatting_helpers as formatter
3233
3334if typing .TYPE_CHECKING :
3435 import bigframes .dataframe
@@ -114,16 +115,16 @@ def create_html_representation(
114115 from bigframes .series import Series
115116
116117 if isinstance (obj , Series ):
118+ # Fallback to pandas string representation if the object is not a Series.
119+ # This protects against cases where obj might be something else unexpectedly,
120+ # or if the pandas Series implementation changes.
117121 pd_series = pandas_df .iloc [:, 0 ]
118122 try :
119123 html_string = pd_series ._repr_html_ ()
120124 except AttributeError :
121125 html_string = f"<pre>{ pd_series .to_string ()} </pre>"
122126
123- # Series doesn't typically show total rows/cols like DF in HTML repr here?
124- # But let's check what it was doing.
125- # Original code just returned _repr_html_ or wrapped to_string.
126- # It didn't append row/col count string for Series (wait, Series usually has length in repr).
127+ html_string += f"[{ total_rows } rows]"
127128 return html_string
128129 else :
129130 # It's a DataFrame
@@ -225,32 +226,21 @@ def get_anywidget_bundle(
225226 return widget_repr , widget_metadata
226227
227228
228- def repr_mimebundle (
229+ def _repr_mimebundle_deferred (
229230 obj : Union [bigframes .dataframe .DataFrame , bigframes .series .Series ],
230- include = None ,
231- exclude = None ,
232- ):
233- """
234- Custom display method for IPython/Jupyter environments.
235- """
236- # TODO(b/467647693): Anywidget integration has been tested in Jupyter, VS Code, and
237- # BQ Studio, but there is a known compatibility issue with Marimo that needs to be addressed.
231+ ) -> dict [str , str ]:
232+ return {
233+ "text/plain" : formatter .repr_query_job (obj ._compute_dry_run ()),
234+ "text/html" : formatter .repr_query_job_html (obj ._compute_dry_run ()),
235+ }
236+
237+
238+ def _repr_mimebundle_head (
239+ obj : Union [bigframes .dataframe .DataFrame , bigframes .series .Series ],
240+ ) -> dict [str , str ]:
238241 from bigframes .series import Series
239242
240243 opts = options .display
241- if opts .repr_mode == "anywidget" :
242- try :
243- return get_anywidget_bundle (obj , include = include , exclude = exclude )
244- except ImportError :
245- # Anywidget is an optional dependency, so warn rather than fail.
246- # TODO(shuowei): When Anywidget becomes the default for all repr modes,
247- # remove this warning.
248- warnings .warn (
249- "Anywidget mode is not available. "
250- "Please `pip install anywidget traitlets` or `pip install 'bigframes[anywidget]'` to use interactive tables. "
251- f"Falling back to static HTML. Error: { traceback .format_exc ()} "
252- )
253-
254244 blob_cols : list [str ]
255245 if isinstance (obj , Series ):
256246 pandas_df , row_count , query_job = obj ._block .retrieve_repr_request_results (
@@ -275,3 +265,34 @@ def repr_mimebundle(
275265 )
276266
277267 return {"text/html" : html_string , "text/plain" : text_representation }
268+
269+
270+ def repr_mimebundle (
271+ obj : Union [bigframes .dataframe .DataFrame , bigframes .series .Series ],
272+ include = None ,
273+ exclude = None ,
274+ ):
275+ """
276+ Custom display method for IPython/Jupyter environments.
277+ """
278+ # TODO(b/467647693): Anywidget integration has been tested in Jupyter, VS Code, and
279+ # BQ Studio, but there is a known compatibility issue with Marimo that needs to be addressed.
280+
281+ opts = options .display
282+ if opts .repr_mode == "deferred" :
283+ return _repr_mimebundle_deferred (obj )
284+
285+ if opts .repr_mode == "anywidget" :
286+ try :
287+ return get_anywidget_bundle (obj , include = include , exclude = exclude )
288+ except ImportError :
289+ # Anywidget is an optional dependency, so warn rather than fail.
290+ # TODO(shuowei): When Anywidget becomes the default for all repr modes,
291+ # remove this warning.
292+ warnings .warn (
293+ "Anywidget mode is not available. "
294+ "Please `pip install anywidget traitlets` or `pip install 'bigframes[anywidget]'` to use interactive tables. "
295+ f"Falling back to static HTML. Error: { traceback .format_exc ()} "
296+ )
297+
298+ return _repr_mimebundle_head (obj )
0 commit comments