Skip to content

Commit aab8c67

Browse files
committed
Added IPython section to users' manual.
Improved flexibility of table & tile rendering to IPython display.
1 parent 37f6dc6 commit aab8c67

File tree

2 files changed

+18
-52
lines changed

2 files changed

+18
-52
lines changed

pyrasterframes/src/main/python/docs/ipython.pymd

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ df = spark.read.raster(uri) \
2626
.drop('proj_raster')
2727
```
2828

29-
Print the schema to confirm it's "shape":
29+
Print the schema to confirm its "shape":
3030

3131
```python schema
3232
df.printSchema()
@@ -41,21 +41,9 @@ tile = df.select(df.tile).first()['tile']
4141
tile
4242
```
4343

44-
If you access the tile's `cells` you get the underlying numpy ndarray (more specifically in this case, `numpy.ma.MaskedArray`).
45-
46-
```python cells
47-
tile.cells
48-
```
49-
50-
If you just want the string representation of the Tile, use `str`:
51-
52-
```python tile_as_string
53-
str(tile)
54-
```
55-
5644
## pyspark.sql.DataFrame Display
5745

58-
There is also a capability for HTML rendering of the spark DataFrame. Rendering work is done on the JVM and the HTML string representation is provided for IPython to display.
46+
There is also a capability for HTML rendering of the spark DataFrame.
5947

6048
```python spark_dataframe
6149
df.select('tile', 'extent')

pyrasterframes/src/main/python/pyrasterframes/rf_ipython.py

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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 `&lt; img`
210-
rendering = df.to_html(escape=False, # means our `< img` does not get changed to `&lt; 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 `&lt; 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

225205
def 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

Comments
 (0)