@@ -143,25 +143,11 @@ def pandas_df_to_html(df: DataFrame) -> Optional[str]:
143143 # honor the existing options on display
144144 if not pd .get_option ("display.notebook_repr_html" ):
145145 return None
146- return pandas_df_to_mime (df , 'text/html' )
147-
148-
149- def pandas_df_to_markdown (df : DataFrame ) -> Optional [str ]:
150- """Provide HTML formatting for pandas.DataFrame with rf_types.Tile in the columns. """
151- return pandas_df_to_mime (df , 'text/markdown' )
152-
153-
154- def pandas_df_to_mime (df : DataFrame , mimetype : str ) -> str :
155- """Provide HTML formatting for pandas.DataFrame with rf_types.Tile in the columns. """
156- import pandas as pd
157146
158147 default_max_colwidth = pd .get_option ('display.max_colwidth' ) # we'll try to politely put it back
159148
160149 if len (df ) == 0 :
161- if "html" in mimetype :
162- return df ._repr_html_ ()
163- else :
164- return ""
150+ return df ._repr_html_ ()
165151
166152 tile_cols = []
167153 geom_cols = []
@@ -204,22 +190,16 @@ def _safe_bytearray_to_html(b):
204190
205191 # This is needed to avoid our tile being rendered as `<img src="only up to fifty char...`
206192 pd .set_option ('display.max_colwidth' , None )
207-
208- if 'html' in mimetype :
209- # `escape` means our `< img` does not get changed to `< img`
210- rendering = df .to_html (escape = False , # means our `< img` does not get changed to `< img`
211- formatters = formatter , # apply custom format to columns
212- render_links = True , # common in raster frames
213- notebook = True ,
214- max_rows = pd .get_option ("display.max_rows" ), # retain existing options
215- max_cols = pd .get_option ("display.max_columns" ),
216- show_dimensions = pd .get_option ("display.show_dimensions" ),
217- )
218- else :
219- rendering = df .to_markdown ()
220-
193+ return_html = df .to_html (escape = False , # means our `< img` does not get changed to `< img`
194+ formatters = formatter , # apply custom format to columns
195+ render_links = True , # common in raster frames
196+ notebook = True ,
197+ max_rows = pd .get_option ("display.max_rows" ), # retain existing options
198+ max_cols = pd .get_option ("display.max_columns" ),
199+ show_dimensions = pd .get_option ("display.show_dimensions" ),
200+ )
221201 pd .set_option ('display.max_colwidth' , default_max_colwidth )
222- return rendering
202+ return return_html
223203
224204
225205def spark_df_to_markdown (df : DataFrame , num_rows : int = 5 , truncate : bool = False ) -> str :
@@ -270,7 +250,7 @@ def _folium_map_formatter(map) -> str:
270250 # Markdown. These will likely only effect docs build.
271251 markdown_formatter = formatters ['text/markdown' ]
272252 # Pandas doesn't have a markdown
273- markdown_formatter .for_type (pandas .DataFrame , pandas_df_to_markdown )
253+ markdown_formatter .for_type (pandas .DataFrame , pandas_df_to_html )
274254 markdown_formatter .for_type (pyspark .sql .DataFrame , spark_df_to_markdown )
275255 # Running loose here by embedding tile as `img` tag.
276256 markdown_formatter .for_type (Tile , tile_to_html )
@@ -286,21 +266,19 @@ def _folium_map_formatter(map) -> str:
286266 Tile .show = plot_tile
287267
288268 # noinspection PyTypeChecker
289- def _display (df : pyspark .sql .DataFrame , num_rows : int = 5 , truncate : bool = False ) -> ():
269+ def _display (df : pyspark .sql .DataFrame , num_rows : int = 5 , truncate : bool = False ,
270+ mimetype : str = 'text/html' ) -> ():
290271 """
291272 Invoke IPython `display` with specific controls.
292273 :param num_rows: number of rows to render
293274 :param truncate: If `True`, shorten width of columns to no more than 40 characters
294275 :return: None
295276 """
296277
297- # It's infuriating that hacks like this seem to be the only way to
298- # determine your execution context.
299- env = str (type (get_ipython ()))
300- if "Terminal" in env :
301- display_markdown (spark_df_to_markdown (df , num_rows , truncate ), raw = True )
302- else :
278+ if "html" in mimetype :
303279 display_html (spark_df_to_html (df , num_rows , truncate ), raw = True )
280+ else :
281+ display_markdown (spark_df_to_markdown (df , num_rows , truncate ), raw = True )
304282
305283
306284 # Add enhanced display function
0 commit comments