Skip to content

Commit 24ffc49

Browse files
committed
Added ellipsis to truncated table cells in Markdown and HTML renderers.
1 parent f43c484 commit 24ffc49

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

core/src/main/resources/reference.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ rasterframes {
33
prefer-gdal = true
44
showable-tiles = true
55
showable-max-cells = 20
6+
max-truncate-row-element-length = 40
67
raster-source-cache-timeout = 120 seconds
78
}
89

core/src/main/scala/org/locationtech/rasterframes/util/package.scala

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
3838
import org.apache.spark.sql.catalyst.rules.Rule
3939
import org.apache.spark.sql.functions._
4040
import org.apache.spark.sql.rf._
41-
import org.apache.spark.sql.types.StringType
41+
import org.apache.spark.sql.types.{StringType, StructField}
4242
import org.apache.spark.sql._
4343
import org.slf4j.LoggerFactory
4444
import spire.syntax.cfor._
@@ -184,15 +184,25 @@ package object util {
184184
}
185185
}
186186

187+
private val truncateWidth = rfConfig.getInt("max-truncate-row-element-length")
188+
187189
implicit class DFWithPrettyPrint(val df: Dataset[_]) extends AnyVal {
190+
191+
def stringifyRowElements(cols: Seq[StructField], truncate: Boolean) = {
192+
cols
193+
.map(c => s"`${c.name}`")
194+
.map(c => df.col(c).cast(StringType))
195+
.map(c => if (truncate) {
196+
when(length(c) > lit(truncateWidth), concat(substring(c, 1, truncateWidth), lit("...")))
197+
.otherwise(c)
198+
} else c)
199+
}
200+
188201
def toMarkdown(numRows: Int = 5, truncate: Boolean = false): String = {
189202
import df.sqlContext.implicits._
190-
val cols = df.columns
191-
val header = cols.mkString("| ", " | ", " |") + "\n" + ("|---" * cols.length) + "|\n"
192-
val stringifiers = cols
193-
.map(c => s"`$c`")
194-
.map(c => df.col(c).cast(StringType))
195-
.map(c => if (truncate) substring(c, 1, 40) else c)
203+
val cols = df.schema.fields
204+
val header = cols.map(_.name).mkString("| ", " | ", " |") + "\n" + ("|---" * cols.length) + "|\n"
205+
val stringifiers = stringifyRowElements(cols, truncate)
196206
val cat = concat_ws(" | ", stringifiers: _*)
197207
val body = df
198208
.select(cat).limit(numRows)
@@ -206,20 +216,16 @@ package object util {
206216

207217
def toHTML(numRows: Int = 5, truncate: Boolean = false): String = {
208218
import df.sqlContext.implicits._
209-
val cols = df.columns
210-
val header = "<thead>\n" + cols.mkString("<tr><th>", "</th><th>", "</th></tr>\n") + "</thead>\n"
211-
val stringifiers = cols
212-
.map(c => s"`$c`")
213-
.map(c => df.col(c).cast(StringType))
214-
.map(c => if (truncate) substring(c, 1, 40) else c)
219+
val cols = df.schema.fields
220+
val header = "<thead>\n" + cols.map(_.name).mkString("<tr><th>", "</th><th>", "</th></tr>\n") + "</thead>\n"
221+
val stringifiers = stringifyRowElements(cols, truncate)
215222
val cat = concat_ws("</td><td>", stringifiers: _*)
216223
val body = df
217224
.select(cat).limit(numRows)
218225
.as[String]
219226
.collect()
220227
.mkString("<tr><td>", "</td></tr>\n<tr><td>", "</td></tr>\n")
221228

222-
223229
"<table>\n" + header + "<tbody>\n" + body + "</tbody>\n" + "</table>"
224230
}
225231
}

core/src/test/scala/org/locationtech/rasterframes/ExtensionMethodSpec.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import geotrellis.raster.{ByteCellType, GridBounds, TileLayout}
2626
import geotrellis.spark.tiling.{CRSWorldExtent, LayoutDefinition}
2727
import geotrellis.spark.{KeyBounds, SpatialKey, TileLayerMetadata}
2828
import org.apache.spark.sql.Encoders
29-
import org.locationtech.rasterframes.util.SubdivideSupport
29+
import org.locationtech.rasterframes.util._
3030

3131
import scala.xml.parsing.XhtmlParser
3232

@@ -113,13 +113,15 @@ class ExtensionMethodSpec extends TestEnvironment with TestData with SubdivideSu
113113
}
114114

115115
it("should render Markdown") {
116-
import org.locationtech.rasterframes.util._
117-
rf.toMarkdown().count(_ == '|') shouldBe >=(3 * 5)
116+
val md = rf.toMarkdown()
117+
md.count(_ == '|') shouldBe >=(3 * 5)
118+
md.count(_ == '\n') should be (6)
119+
120+
val md2 = rf.toMarkdown(truncate=true)
121+
md2 should include ("...")
118122
}
119123

120124
it("should render HTML") {
121-
import org.locationtech.rasterframes.util._
122-
123125
noException shouldBe thrownBy {
124126
XhtmlParser(scala.io.Source.fromString(rf.toHTML()))
125127
}

0 commit comments

Comments
 (0)