Skip to content

Commit aa62538

Browse files
committed
Update JupyterHtmlRenderer for image view support
Developed a mechanism to enable or disable ImageViewer based on the current IDE version in the JupyterHtmlRenderer.
1 parent a22d891 commit aa62538

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.jetbrains.kotlinx.jupyter.api.renderHtmlAsIFrameIfNeeded
2424
/** Starting from this version, dataframe integration will respond with additional data for rendering in Kotlin Notebooks plugin. */
2525
private const val MIN_KERNEL_VERSION_FOR_NEW_TABLES_UI = "0.11.0.311"
2626
private const val MIN_IDE_VERSION_SUPPORT_JSON_WITH_METADATA = 241
27+
private const val MIN_IDE_VERSION_SUPPORT_IMAGE_VIEWER = 242
2728

2829
internal class JupyterHtmlRenderer(
2930
val display: DisplayConfiguration,
@@ -64,8 +65,8 @@ internal inline fun <reified T : Any> JupyterHtmlRenderer.render(
6465
if (notebook.kernelVersion >= KotlinKernelVersion.from(MIN_KERNEL_VERSION_FOR_NEW_TABLES_UI)!!) {
6566
val ideBuildNumber = KotlinNotebookPluginUtils.getKotlinNotebookIDEBuildNumber()
6667

67-
val jsonEncodedDf =
68-
if (ideBuildNumber == null || ideBuildNumber.majorVersion < MIN_IDE_VERSION_SUPPORT_JSON_WITH_METADATA) {
68+
val jsonEncodedDf = when {
69+
!ideBuildNumber.supportsDynamicNestedTables() -> {
6970
json {
7071
obj(
7172
"nrow" to df.size.nrow,
@@ -74,19 +75,32 @@ internal inline fun <reified T : Any> JupyterHtmlRenderer.render(
7475
"kotlin_dataframe" to encodeFrame(df.take(limit)),
7576
)
7677
}.toJsonString()
77-
} else {
78+
}
79+
80+
else -> {
81+
val imageEncodingOptions =
82+
if (ideBuildNumber.supportsImageViewer()) Base64ImageEncodingOptions() else null
83+
7884
df.toJsonWithMetadata(
7985
limit,
8086
reifiedDisplayConfiguration.rowsLimit,
81-
imageEncodingOptions = Base64ImageEncodingOptions()
87+
imageEncodingOptions = imageEncodingOptions
8288
)
8389
}
90+
}
91+
8492
notebook.renderAsIFrameAsNeeded(html, staticHtml, jsonEncodedDf)
8593
} else {
8694
notebook.renderHtmlAsIFrameIfNeeded(html)
8795
}
8896
}
8997

98+
private fun KotlinNotebookPluginUtils.IdeBuildNumber?.supportsDynamicNestedTables() =
99+
this != null && majorVersion >= MIN_IDE_VERSION_SUPPORT_JSON_WITH_METADATA
100+
101+
private fun KotlinNotebookPluginUtils.IdeBuildNumber?.supportsImageViewer() =
102+
this != null && majorVersion >= MIN_IDE_VERSION_SUPPORT_IMAGE_VIEWER
103+
90104
internal fun Notebook.renderAsIFrameAsNeeded(
91105
data: HtmlData,
92106
staticData: HtmlData,

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.jetbrains.kotlinx.jupyter.api.renderHtmlAsIFrameIfNeeded
2424
/** Starting from this version, dataframe integration will respond with additional data for rendering in Kotlin Notebooks plugin. */
2525
private const val MIN_KERNEL_VERSION_FOR_NEW_TABLES_UI = "0.11.0.311"
2626
private const val MIN_IDE_VERSION_SUPPORT_JSON_WITH_METADATA = 241
27+
private const val MIN_IDE_VERSION_SUPPORT_IMAGE_VIEWER = 242
2728

2829
internal class JupyterHtmlRenderer(
2930
val display: DisplayConfiguration,
@@ -64,8 +65,8 @@ internal inline fun <reified T : Any> JupyterHtmlRenderer.render(
6465
if (notebook.kernelVersion >= KotlinKernelVersion.from(MIN_KERNEL_VERSION_FOR_NEW_TABLES_UI)!!) {
6566
val ideBuildNumber = KotlinNotebookPluginUtils.getKotlinNotebookIDEBuildNumber()
6667

67-
val jsonEncodedDf =
68-
if (ideBuildNumber == null || ideBuildNumber.majorVersion < MIN_IDE_VERSION_SUPPORT_JSON_WITH_METADATA) {
68+
val jsonEncodedDf = when {
69+
!ideBuildNumber.supportsDynamicNestedTables() -> {
6970
json {
7071
obj(
7172
"nrow" to df.size.nrow,
@@ -74,19 +75,32 @@ internal inline fun <reified T : Any> JupyterHtmlRenderer.render(
7475
"kotlin_dataframe" to encodeFrame(df.take(limit)),
7576
)
7677
}.toJsonString()
77-
} else {
78+
}
79+
80+
else -> {
81+
val imageEncodingOptions =
82+
if (ideBuildNumber.supportsImageViewer()) Base64ImageEncodingOptions() else null
83+
7884
df.toJsonWithMetadata(
7985
limit,
8086
reifiedDisplayConfiguration.rowsLimit,
81-
imageEncodingOptions = Base64ImageEncodingOptions()
87+
imageEncodingOptions = imageEncodingOptions
8288
)
8389
}
90+
}
91+
8492
notebook.renderAsIFrameAsNeeded(html, staticHtml, jsonEncodedDf)
8593
} else {
8694
notebook.renderHtmlAsIFrameIfNeeded(html)
8795
}
8896
}
8997

98+
private fun KotlinNotebookPluginUtils.IdeBuildNumber?.supportsDynamicNestedTables() =
99+
this != null && majorVersion >= MIN_IDE_VERSION_SUPPORT_JSON_WITH_METADATA
100+
101+
private fun KotlinNotebookPluginUtils.IdeBuildNumber?.supportsImageViewer() =
102+
this != null && majorVersion >= MIN_IDE_VERSION_SUPPORT_IMAGE_VIEWER
103+
90104
internal fun Notebook.renderAsIFrameAsNeeded(
91105
data: HtmlData,
92106
staticData: HtmlData,

0 commit comments

Comments
 (0)