-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rendering DataFrames as HTML table, like Toree does sometimes
- Loading branch information
1 parent
73d5c21
commit f70e9cf
Showing
3 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
modules/almond-spark/src/main/scala/almond/spark/DataFrameRenderers.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package almond.spark | ||
|
||
import org.apache.spark.sql.{Dataset, Row} | ||
|
||
object DataFrameRenderer { | ||
|
||
// inspired by https://github.com/apache/incubator-toree/blob/5b19aac2e56a56d35c888acc4ed5e549b1f4ed7c/kernel/src/main/scala/org/apache/toree/utils/DataFrameConverter.scala#L47-L59 | ||
def render(df: Dataset[Row], limit: Int = 10): String = { | ||
import scalatags.Text.all._ | ||
val columnFields = df.schema.fieldNames.toSeq.map(th(_)) | ||
val columns = tr(columnFields) | ||
val rows = df | ||
.rdd | ||
.map { row => | ||
val fieldValues = row.toSeq.map(fieldToString).map(td(_)) | ||
tr(fieldValues) | ||
} | ||
.take(limit) | ||
table(columns, rows).render | ||
} | ||
|
||
// https://github.com/apache/incubator-toree/blob/5b19aac2e56a56d35c888acc4ed5e549b1f4ed7c/kernel/src/main/scala/org/apache/toree/utils/DataFrameConverter.scala#L84-L89 | ||
def fieldToString(any: Any): String = | ||
any match { | ||
case null => "null" | ||
case seq: Seq[_] => seq.mkString("[", ", ", "]") | ||
case _ => any.toString | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters