Skip to content

Commit 7f68283

Browse files
authored
Merge pull request #385 from s22s/fix/rf_ipy_render_383
In rf_ipython, honor users pandas max_colwidth on Geometry objects
2 parents d865a6b + 74be32d commit 7f68283

File tree

3 files changed

+177
-284
lines changed

3 files changed

+177
-284
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#
2020

2121
import pyrasterframes.rf_types
22+
from shapely.geometry.base import BaseGeometry
23+
2224
import numpy as np
2325

2426

@@ -120,13 +122,18 @@ def pandas_df_to_html(df):
120122
if not pd.get_option("display.notebook_repr_html"):
121123
return None
122124

125+
default_max_colwidth = pd.get_option('display.max_colwidth') # we'll try to politely put it back
126+
123127
if len(df) == 0:
124128
return df._repr_html_()
125129

126130
tile_cols = []
131+
geom_cols = []
127132
for c in df.columns:
128133
if isinstance(df.iloc[0][c], pyrasterframes.rf_types.Tile): # if the first is a Tile try formatting
129134
tile_cols.append(c)
135+
elif isinstance(df.iloc[0][c], BaseGeometry): # if the first is a Geometry try formatting
136+
geom_cols.append(c)
130137

131138
def _safe_tile_to_html(t):
132139
if isinstance(t, pyrasterframes.rf_types.Tile):
@@ -135,11 +142,21 @@ def _safe_tile_to_html(t):
135142
# handles case where objects in a column are not all Tile type
136143
return t.__repr__()
137144

145+
def _safe_geom_to_html(g):
146+
if isinstance(g, BaseGeometry):
147+
wkt = g.wkt
148+
if len(wkt) > default_max_colwidth:
149+
return wkt[:default_max_colwidth-3] + '...'
150+
else:
151+
wkt
152+
else:
153+
return g.__repr__()
154+
138155
# dict keyed by column with custom rendering function
139156
formatter = {c: _safe_tile_to_html for c in tile_cols}
157+
formatter.update({c: _safe_geom_to_html for c in geom_cols})
140158

141159
# This is needed to avoid our tile being rendered as `<img src="only up to fifty char...`
142-
default_max_colwidth = pd.get_option('display.max_colwidth') # we'll try to politely put it back
143160
pd.set_option('display.max_colwidth', -1)
144161
return_html = df.to_html(escape=False, # means our `< img` does not get changed to `&lt; img`
145162
formatters=formatter, # apply custom format to columns

rf-notebook/src/main/notebooks/Getting Started.ipynb

Lines changed: 108 additions & 67 deletions
Large diffs are not rendered by default.

rf-notebook/src/main/notebooks/pretty_rendering_in_rf.ipynb

Lines changed: 51 additions & 216 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)