Skip to content

Add static rendering for GitHub #480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.jetbrains.kotlinx.dataframe.impl.renderType
import org.jetbrains.kotlinx.dataframe.impl.scale
import org.jetbrains.kotlinx.dataframe.impl.truncate
import org.jetbrains.kotlinx.dataframe.jupyter.CellRenderer
import org.jetbrains.kotlinx.dataframe.jupyter.DefaultCellRenderer
import org.jetbrains.kotlinx.dataframe.jupyter.RenderedContent
import org.jetbrains.kotlinx.dataframe.name
import org.jetbrains.kotlinx.dataframe.nrow
Expand Down Expand Up @@ -174,6 +175,70 @@ internal fun AnyFrame.toHtmlData(
return DataFrameHtmlData("", body, script)
}

internal fun AnyFrame.toStaticHtml(
configuration: DisplayConfiguration = DisplayConfiguration.DEFAULT,
cellRenderer: CellRenderer,
): DataFrameHtmlData {
val df = this
val id = "static_df_${nextTableId()}"
val columnsToRender = columns()

fun StringBuilder.emitTag(tag: String, attributes: String = "", tagContents: StringBuilder.() -> Unit) {
append("<")
append(tag)
if (attributes.isNotEmpty()) {
append(" ")
append(attributes)
}
append(">")

tagContents()

append("</")
append(tag)
append(">")
}

fun StringBuilder.emitHeader() = emitTag("thead") {
emitTag("tr") {
columnsToRender.forEach { col ->
emitTag("th") {
append(col.name())
}
}
}
}

fun StringBuilder.emitCell(cellValue: Any?) = emitTag("td") {
append(cellRenderer.content(cellValue, configuration).truncatedContent)
}

fun StringBuilder.emitRow(row: AnyRow) = emitTag("tr") {
columnsToRender.forEach { col ->
emitCell(row[col.path()])
}
}

fun StringBuilder.emitBody() = emitTag("tbody") {
val rowsCountToRender = minOf(rowsCount(), configuration.rowsLimit ?: Int.MAX_VALUE)
for (rowIndex in 0..<rowsCountToRender) {
emitRow(df[rowIndex])
}
}

fun StringBuilder.emitTable() = emitTag("table", """class="dataframe" id="$id"""") {
emitHeader()
emitBody()
}

return DataFrameHtmlData(
body = buildString { emitTable() },
script = """
document.getElementById("$id").style.display = "none";
""".trimIndent()
)
}

internal fun DataFrameHtmlData.print() = println(this)

@Deprecated(
Expand All @@ -188,7 +253,7 @@ public fun <T> DataFrame<T>.html(): String = toStandaloneHTML().toString()
*/
public fun <T> DataFrame<T>.toStandaloneHTML(
configuration: DisplayConfiguration = DisplayConfiguration.DEFAULT,
cellRenderer: CellRenderer = org.jetbrains.kotlinx.dataframe.jupyter.DefaultCellRenderer,
cellRenderer: CellRenderer = DefaultCellRenderer,
getFooter: (DataFrame<T>) -> String? = { "DataFrame [${it.size}]" },
): DataFrameHtmlData = toHTML(configuration, cellRenderer, getFooter).withTableDefinitions()

Expand All @@ -197,7 +262,7 @@ public fun <T> DataFrame<T>.toStandaloneHTML(
*/
public fun <T> DataFrame<T>.toHTML(
configuration: DisplayConfiguration = DisplayConfiguration.DEFAULT,
cellRenderer: CellRenderer = org.jetbrains.kotlinx.dataframe.jupyter.DefaultCellRenderer,
cellRenderer: CellRenderer = DefaultCellRenderer,
getFooter: (DataFrame<T>) -> String? = { "DataFrame [${it.size}]" },
): DataFrameHtmlData {
val limit = configuration.rowsLimit ?: Int.MAX_VALUE
Expand All @@ -218,6 +283,8 @@ public fun <T> DataFrame<T>.toHTML(

var tableHtml = toHtmlData(configuration, cellRenderer)

tableHtml += toStaticHtml(configuration, DefaultCellRenderer)

if (bodyFooter != null) {
tableHtml += DataFrameHtmlData("", bodyFooter, "")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,24 @@ class RenderingTests {
df.toHTML().script shouldContain rendered
}
}

@Test
fun `static rendering should be present`() {
val df = dataFrameOf("a", "b")(listOf(1, 1), listOf(2, 4))
val actualHtml = df.toHTML()

actualHtml.body shouldContain """
<thead>
<tr>
<th>a</th><th>b</th>
</tr>
</thead>
<tbody>
<tr>
<td>[1, 1]</td><td>[2, 4]</td>
</tr>
</tbody>
</table>
""".trimIndent().replace("\n", "")
}
}
71 changes: 69 additions & 2 deletions core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/io/html.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.jetbrains.kotlinx.dataframe.impl.renderType
import org.jetbrains.kotlinx.dataframe.impl.scale
import org.jetbrains.kotlinx.dataframe.impl.truncate
import org.jetbrains.kotlinx.dataframe.jupyter.CellRenderer
import org.jetbrains.kotlinx.dataframe.jupyter.DefaultCellRenderer
import org.jetbrains.kotlinx.dataframe.jupyter.RenderedContent
import org.jetbrains.kotlinx.dataframe.name
import org.jetbrains.kotlinx.dataframe.nrow
Expand Down Expand Up @@ -174,6 +175,70 @@ internal fun AnyFrame.toHtmlData(
return DataFrameHtmlData("", body, script)
}

internal fun AnyFrame.toStaticHtml(
configuration: DisplayConfiguration = DisplayConfiguration.DEFAULT,
cellRenderer: CellRenderer,
): DataFrameHtmlData {
val df = this
val id = "static_df_${nextTableId()}"
val columnsToRender = columns()

fun StringBuilder.emitTag(tag: String, attributes: String = "", tagContents: StringBuilder.() -> Unit) {
append("<")
append(tag)
if (attributes.isNotEmpty()) {
append(" ")
append(attributes)
}
append(">")

tagContents()

append("</")
append(tag)
append(">")
}

fun StringBuilder.emitHeader() = emitTag("thead") {
emitTag("tr") {
columnsToRender.forEach { col ->
emitTag("th") {
append(col.name())
}
}
}
}

fun StringBuilder.emitCell(cellValue: Any?) = emitTag("td") {
append(cellRenderer.content(cellValue, configuration).truncatedContent)
}

fun StringBuilder.emitRow(row: AnyRow) = emitTag("tr") {
columnsToRender.forEach { col ->
emitCell(row[col.path()])
}
}

fun StringBuilder.emitBody() = emitTag("tbody") {
val rowsCountToRender = minOf(rowsCount(), configuration.rowsLimit ?: Int.MAX_VALUE)
for (rowIndex in 0..<rowsCountToRender) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow first time I see ..< in action, cool

emitRow(df[rowIndex])
}
}

fun StringBuilder.emitTable() = emitTag("table", """class="dataframe" id="$id"""") {
emitHeader()
emitBody()
}

return DataFrameHtmlData(
body = buildString { emitTable() },
script = """
document.getElementById("$id").style.display = "none";
""".trimIndent()
)
}

internal fun DataFrameHtmlData.print() = println(this)

@Deprecated(
Expand All @@ -188,7 +253,7 @@ public fun <T> DataFrame<T>.html(): String = toStandaloneHTML().toString()
*/
public fun <T> DataFrame<T>.toStandaloneHTML(
configuration: DisplayConfiguration = DisplayConfiguration.DEFAULT,
cellRenderer: CellRenderer = org.jetbrains.kotlinx.dataframe.jupyter.DefaultCellRenderer,
cellRenderer: CellRenderer = DefaultCellRenderer,
getFooter: (DataFrame<T>) -> String? = { "DataFrame [${it.size}]" },
): DataFrameHtmlData = toHTML(configuration, cellRenderer, getFooter).withTableDefinitions()

Expand All @@ -197,7 +262,7 @@ public fun <T> DataFrame<T>.toStandaloneHTML(
*/
public fun <T> DataFrame<T>.toHTML(
configuration: DisplayConfiguration = DisplayConfiguration.DEFAULT,
cellRenderer: CellRenderer = org.jetbrains.kotlinx.dataframe.jupyter.DefaultCellRenderer,
cellRenderer: CellRenderer = DefaultCellRenderer,
getFooter: (DataFrame<T>) -> String? = { "DataFrame [${it.size}]" },
): DataFrameHtmlData {
val limit = configuration.rowsLimit ?: Int.MAX_VALUE
Expand All @@ -218,6 +283,8 @@ public fun <T> DataFrame<T>.toHTML(

var tableHtml = toHtmlData(configuration, cellRenderer)

tableHtml += toStaticHtml(configuration, DefaultCellRenderer)

if (bodyFooter != null) {
tableHtml += DataFrameHtmlData("", bodyFooter, "")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,24 @@ class RenderingTests {
df.toHTML().script shouldContain rendered
}
}

@Test
fun `static rendering should be present`() {
val df = dataFrameOf("a", "b")(listOf(1, 1), listOf(2, 4))
val actualHtml = df.toHTML()

actualHtml.body shouldContain """
<thead>
<tr>
<th>a</th><th>b</th>
</tr>
</thead>
<tbody>
<tr>
<td>[1, 1]</td><td>[2, 4]</td>
</tr>
</tbody>
</table>
""".trimIndent().replace("\n", "")
}
}