Skip to content

Commit 54d2c74

Browse files
authored
Merge pull request #511 from Kotlin/static-examples2
Static notebook fixes and examples
2 parents 7232b07 + 47723fc commit 54d2c74

File tree

11 files changed

+6198
-40669
lines changed

11 files changed

+6198
-40669
lines changed

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/io/html.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,17 @@ public fun <T> DataFrame<T>.html(): String = toStandaloneHTML().toString()
254254
public fun <T> DataFrame<T>.toStandaloneHTML(
255255
configuration: DisplayConfiguration = DisplayConfiguration.DEFAULT,
256256
cellRenderer: CellRenderer = DefaultCellRenderer,
257+
includeStatic: Boolean = true,
257258
getFooter: (DataFrame<T>) -> String? = { "DataFrame [${it.size}]" },
258-
): DataFrameHtmlData = toHTML(configuration, cellRenderer, getFooter).withTableDefinitions()
259+
): DataFrameHtmlData = toHTML(configuration, cellRenderer, includeStatic, getFooter).withTableDefinitions()
259260

260261
/**
261262
* @return DataFrameHtmlData without additional definitions. Can be rendered in Jupyter kernel environments
262263
*/
263264
public fun <T> DataFrame<T>.toHTML(
264265
configuration: DisplayConfiguration = DisplayConfiguration.DEFAULT,
265266
cellRenderer: CellRenderer = DefaultCellRenderer,
267+
includeStatic: Boolean = true,
266268
getFooter: (DataFrame<T>) -> String? = { "DataFrame [${it.size}]" },
267269
): DataFrameHtmlData {
268270
val limit = configuration.rowsLimit ?: Int.MAX_VALUE
@@ -283,7 +285,9 @@ public fun <T> DataFrame<T>.toHTML(
283285

284286
var tableHtml = toHtmlData(configuration, cellRenderer)
285287

286-
tableHtml += toStaticHtml(configuration, DefaultCellRenderer)
288+
if (includeStatic) {
289+
tableHtml += toStaticHtml(configuration, DefaultCellRenderer)
290+
}
287291

288292
if (bodyFooter != null) {
289293
tableHtml += DataFrameHtmlData("", bodyFooter, "")

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/jupyter/JupyterHtmlRenderer.kt

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.jetbrains.kotlinx.dataframe.io.DataFrameHtmlData
77
import org.jetbrains.kotlinx.dataframe.io.DisplayConfiguration
88
import org.jetbrains.kotlinx.dataframe.io.encodeFrame
99
import org.jetbrains.kotlinx.dataframe.io.toHTML
10+
import org.jetbrains.kotlinx.dataframe.io.toStaticHtml
1011
import org.jetbrains.kotlinx.dataframe.jupyter.KotlinNotebookPluginUtils.convertToDataFrame
1112
import org.jetbrains.kotlinx.dataframe.nrow
1213
import org.jetbrains.kotlinx.dataframe.size
@@ -30,7 +31,7 @@ internal class JupyterHtmlRenderer(
3031
internal inline fun <reified T : Any> JupyterHtmlRenderer.render(
3132
noinline getFooter: (T) -> String,
3233
crossinline modifyConfig: T.(DisplayConfiguration) -> DisplayConfiguration = { it },
33-
applyRowsLimit: Boolean = true
34+
applyRowsLimit: Boolean = true,
3435
) = builder.renderWithHost<T> { host, value ->
3536
val contextRenderer = JupyterCellRenderer(this.notebook, host)
3637
val reifiedDisplayConfiguration = value.modifyConfig(display)
@@ -44,36 +45,41 @@ internal inline fun <reified T : Any> JupyterHtmlRenderer.render(
4445
df.nrow
4546
}
4647

47-
val html = (
48-
DataFrameHtmlData.tableDefinitions(
49-
includeJs = reifiedDisplayConfiguration.isolatedOutputs,
50-
includeCss = true
51-
) + df.toHTML(
52-
reifiedDisplayConfiguration,
53-
contextRenderer
48+
val html = DataFrameHtmlData.tableDefinitions(
49+
includeJs = reifiedDisplayConfiguration.isolatedOutputs,
50+
includeCss = true,
51+
).plus(
52+
df.toHTML(
53+
configuration = reifiedDisplayConfiguration,
54+
cellRenderer = contextRenderer,
55+
includeStatic = false, // is added later to make sure it's put outside of potential iFrames
5456
) { footer }
55-
).toJupyterHtmlData()
57+
).toJupyterHtmlData()
58+
59+
// Generates a static version of the table which can be displayed in GitHub previews etc.
60+
val staticHtml = df.toStaticHtml(reifiedDisplayConfiguration, DefaultCellRenderer).toJupyterHtmlData()
5661

5762
if (notebook.kernelVersion >= KotlinKernelVersion.from(MIN_KERNEL_VERSION_FOR_NEW_TABLES_UI)!!) {
5863
val jsonEncodedDf = json {
5964
obj(
6065
"nrow" to df.size.nrow,
6166
"ncol" to df.size.ncol,
6267
"columns" to df.columnNames(),
63-
"kotlin_dataframe" to encodeFrame(df.rows().take(limit).toDataFrame())
68+
"kotlin_dataframe" to encodeFrame(df.rows().take(limit).toDataFrame()),
6469
)
6570
}.toJsonString()
66-
notebook.renderAsIFrameAsNeeded(html, jsonEncodedDf)
71+
notebook.renderAsIFrameAsNeeded(html, staticHtml, jsonEncodedDf)
6772
} else {
6873
notebook.renderHtmlAsIFrameIfNeeded(html)
6974
}
7075
}
7176

72-
internal fun Notebook.renderAsIFrameAsNeeded(data: HtmlData, jsonEncodedDf: String): MimeTypedResult {
77+
internal fun Notebook.renderAsIFrameAsNeeded(data: HtmlData, staticData: HtmlData, jsonEncodedDf: String): MimeTypedResult {
7378
val textHtml = if (jupyterClientType == JupyterClientType.KOTLIN_NOTEBOOK) {
74-
data.generateIframePlaneText(currentColorScheme)
79+
data.generateIframePlaneText(currentColorScheme) +
80+
staticData.toString(currentColorScheme)
7581
} else {
76-
data.toString(currentColorScheme)
82+
(data + staticData).toString(currentColorScheme)
7783
}
7884

7985
return mimeResult(

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/io/html.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,17 @@ public fun <T> DataFrame<T>.html(): String = toStandaloneHTML().toString()
254254
public fun <T> DataFrame<T>.toStandaloneHTML(
255255
configuration: DisplayConfiguration = DisplayConfiguration.DEFAULT,
256256
cellRenderer: CellRenderer = DefaultCellRenderer,
257+
includeStatic: Boolean = true,
257258
getFooter: (DataFrame<T>) -> String? = { "DataFrame [${it.size}]" },
258-
): DataFrameHtmlData = toHTML(configuration, cellRenderer, getFooter).withTableDefinitions()
259+
): DataFrameHtmlData = toHTML(configuration, cellRenderer, includeStatic, getFooter).withTableDefinitions()
259260

260261
/**
261262
* @return DataFrameHtmlData without additional definitions. Can be rendered in Jupyter kernel environments
262263
*/
263264
public fun <T> DataFrame<T>.toHTML(
264265
configuration: DisplayConfiguration = DisplayConfiguration.DEFAULT,
265266
cellRenderer: CellRenderer = DefaultCellRenderer,
267+
includeStatic: Boolean = true,
266268
getFooter: (DataFrame<T>) -> String? = { "DataFrame [${it.size}]" },
267269
): DataFrameHtmlData {
268270
val limit = configuration.rowsLimit ?: Int.MAX_VALUE
@@ -283,7 +285,9 @@ public fun <T> DataFrame<T>.toHTML(
283285

284286
var tableHtml = toHtmlData(configuration, cellRenderer)
285287

286-
tableHtml += toStaticHtml(configuration, DefaultCellRenderer)
288+
if (includeStatic) {
289+
tableHtml += toStaticHtml(configuration, DefaultCellRenderer)
290+
}
287291

288292
if (bodyFooter != null) {
289293
tableHtml += DataFrameHtmlData("", bodyFooter, "")

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/jupyter/JupyterHtmlRenderer.kt

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.jetbrains.kotlinx.dataframe.io.DataFrameHtmlData
77
import org.jetbrains.kotlinx.dataframe.io.DisplayConfiguration
88
import org.jetbrains.kotlinx.dataframe.io.encodeFrame
99
import org.jetbrains.kotlinx.dataframe.io.toHTML
10+
import org.jetbrains.kotlinx.dataframe.io.toStaticHtml
1011
import org.jetbrains.kotlinx.dataframe.jupyter.KotlinNotebookPluginUtils.convertToDataFrame
1112
import org.jetbrains.kotlinx.dataframe.nrow
1213
import org.jetbrains.kotlinx.dataframe.size
@@ -30,7 +31,7 @@ internal class JupyterHtmlRenderer(
3031
internal inline fun <reified T : Any> JupyterHtmlRenderer.render(
3132
noinline getFooter: (T) -> String,
3233
crossinline modifyConfig: T.(DisplayConfiguration) -> DisplayConfiguration = { it },
33-
applyRowsLimit: Boolean = true
34+
applyRowsLimit: Boolean = true,
3435
) = builder.renderWithHost<T> { host, value ->
3536
val contextRenderer = JupyterCellRenderer(this.notebook, host)
3637
val reifiedDisplayConfiguration = value.modifyConfig(display)
@@ -44,36 +45,41 @@ internal inline fun <reified T : Any> JupyterHtmlRenderer.render(
4445
df.nrow
4546
}
4647

47-
val html = (
48-
DataFrameHtmlData.tableDefinitions(
49-
includeJs = reifiedDisplayConfiguration.isolatedOutputs,
50-
includeCss = true
51-
) + df.toHTML(
52-
reifiedDisplayConfiguration,
53-
contextRenderer
48+
val html = DataFrameHtmlData.tableDefinitions(
49+
includeJs = reifiedDisplayConfiguration.isolatedOutputs,
50+
includeCss = true,
51+
).plus(
52+
df.toHTML(
53+
configuration = reifiedDisplayConfiguration,
54+
cellRenderer = contextRenderer,
55+
includeStatic = false, // is added later to make sure it's put outside of potential iFrames
5456
) { footer }
55-
).toJupyterHtmlData()
57+
).toJupyterHtmlData()
58+
59+
// Generates a static version of the table which can be displayed in GitHub previews etc.
60+
val staticHtml = df.toStaticHtml(reifiedDisplayConfiguration, DefaultCellRenderer).toJupyterHtmlData()
5661

5762
if (notebook.kernelVersion >= KotlinKernelVersion.from(MIN_KERNEL_VERSION_FOR_NEW_TABLES_UI)!!) {
5863
val jsonEncodedDf = json {
5964
obj(
6065
"nrow" to df.size.nrow,
6166
"ncol" to df.size.ncol,
6267
"columns" to df.columnNames(),
63-
"kotlin_dataframe" to encodeFrame(df.rows().take(limit).toDataFrame())
68+
"kotlin_dataframe" to encodeFrame(df.rows().take(limit).toDataFrame()),
6469
)
6570
}.toJsonString()
66-
notebook.renderAsIFrameAsNeeded(html, jsonEncodedDf)
71+
notebook.renderAsIFrameAsNeeded(html, staticHtml, jsonEncodedDf)
6772
} else {
6873
notebook.renderHtmlAsIFrameIfNeeded(html)
6974
}
7075
}
7176

72-
internal fun Notebook.renderAsIFrameAsNeeded(data: HtmlData, jsonEncodedDf: String): MimeTypedResult {
77+
internal fun Notebook.renderAsIFrameAsNeeded(data: HtmlData, staticData: HtmlData, jsonEncodedDf: String): MimeTypedResult {
7378
val textHtml = if (jupyterClientType == JupyterClientType.KOTLIN_NOTEBOOK) {
74-
data.generateIframePlaneText(currentColorScheme)
79+
data.generateIframePlaneText(currentColorScheme) +
80+
staticData.toString(currentColorScheme)
7581
} else {
76-
data.toString(currentColorScheme)
82+
(data + staticData).toString(currentColorScheme)
7783
}
7884

7985
return mimeResult(

0 commit comments

Comments
 (0)