Skip to content

Commit 2892683

Browse files
committed
Avoid sending HTML tables in internal Kotlin Notebook <-> Kernel table widget data exchange
1 parent 457e9de commit 2892683

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import org.jetbrains.kotlinx.dataframe.annotations.RequiredByIntellijPlugin
55

66
/**
77
* Allows for disabling the rows limit when generating a DISPLAY output in Jupyter.
8+
* Used in code that Kotlin Notebook sends to Jupyter Kernel / KotlinNotebookPluginUtils
9+
* @param [addHtml] needed to avoid sending HTML in these internal requests, because they won't be displayed anyway, only JSON part of these payloads is used to update "live" state of the widget
810
*/
911
@RequiredByIntellijPlugin
10-
public data class DisableRowsLimitWrapper(public val value: AnyFrame)
12+
public data class DisableRowsLimitWrapper(public val value: AnyFrame, public val addHtml: Boolean = true)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public object KotlinNotebookPluginUtils {
6363
* It's used for example for dynamic pagination in Kotlin Notebook Plugin.
6464
*/
6565
public fun getRowsSubsetForRendering(df: AnyFrame, startIdx: Int, endIdx: Int): DisableRowsLimitWrapper =
66-
DisableRowsLimitWrapper(df[startIdx..<endIdx])
66+
DisableRowsLimitWrapper(df[startIdx..<endIdx], addHtml = false)
6767

6868
/**
6969
* Sorts a dataframe-like object by multiple columns.

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ internal inline fun <reified T : Any> JupyterHtmlRenderer.render(
3838
crossinline modifyConfig: T.(DisplayConfiguration) -> DisplayConfiguration = { it },
3939
applyRowsLimit: Boolean = true,
4040
) = builder.renderWithHost<T> { host, value ->
41+
val addHtml = (value as? DisableRowsLimitWrapper)?.addHtml ?: true
4142
val contextRenderer = JupyterCellRenderer(this.notebook, host)
4243
val reifiedDisplayConfiguration = value.modifyConfig(display)
4344
val footer = getFooter(value)
@@ -53,20 +54,22 @@ internal inline fun <reified T : Any> JupyterHtmlRenderer.render(
5354
df.rowsCount()
5455
}
5556

56-
val html = DataFrameHtmlData
57-
.tableDefinitions(
58-
includeJs = reifiedDisplayConfiguration.isolatedOutputs,
59-
includeCss = true,
60-
).plus(
61-
df.toHtml(
62-
// is added later to make sure it's put outside of potential iFrames
63-
configuration = reifiedDisplayConfiguration.copy(enableFallbackStaticTables = false),
64-
cellRenderer = contextRenderer,
65-
) { footer },
66-
).toJupyterHtmlData()
57+
val html by lazy {
58+
DataFrameHtmlData
59+
.tableDefinitions(
60+
includeJs = reifiedDisplayConfiguration.isolatedOutputs,
61+
includeCss = true,
62+
).plus(
63+
df.toHtml(
64+
// is added later to make sure it's put outside of potential iFrames
65+
configuration = reifiedDisplayConfiguration.copy(enableFallbackStaticTables = false),
66+
cellRenderer = contextRenderer,
67+
) { footer },
68+
).toJupyterHtmlData()
69+
}
6770

6871
// Generates a static version of the table which can be displayed in GitHub previews etc.
69-
val staticHtml = df.toStaticHtml(reifiedDisplayConfiguration, DefaultCellRenderer).toJupyterHtmlData()
72+
val staticHtml by lazy { df.toStaticHtml(reifiedDisplayConfiguration, DefaultCellRenderer).toJupyterHtmlData() }
7073

7174
if (notebook.kernelVersion >= KotlinKernelVersion.from(MIN_KERNEL_VERSION_FOR_NEW_TABLES_UI)!!) {
7275
val ideBuildNumber = KotlinNotebookPluginUtils.getKotlinNotebookIDEBuildNumber()
@@ -94,8 +97,13 @@ internal inline fun <reified T : Any> JupyterHtmlRenderer.render(
9497
)
9598
}
9699
}
97-
98-
notebook.renderAsIFrameAsNeeded(data = html, staticData = staticHtml, jsonEncodedDf = jsonEncodedDf)
100+
if (!addHtml) {
101+
mimeResult(
102+
"application/kotlindataframe+json" to jsonEncodedDf,
103+
)
104+
} else {
105+
notebook.renderAsIFrameAsNeeded(data = html, staticData = staticHtml, jsonEncodedDf = jsonEncodedDf)
106+
}
99107
} else {
100108
notebook.renderHtmlAsIFrameIfNeeded(data = html)
101109
}

0 commit comments

Comments
 (0)