Skip to content

Commit

Permalink
Update JupyterHtmlRenderer for image view support
Browse files Browse the repository at this point in the history
Developed a mechanism to enable or disable ImageViewer based on the current IDE version in the JupyterHtmlRenderer.
  • Loading branch information
ermolenkodev committed May 15, 2024
1 parent a22d891 commit a6f8e9f
Showing 1 changed file with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.jetbrains.kotlinx.jupyter.api.renderHtmlAsIFrameIfNeeded
/** Starting from this version, dataframe integration will respond with additional data for rendering in Kotlin Notebooks plugin. */
private const val MIN_KERNEL_VERSION_FOR_NEW_TABLES_UI = "0.11.0.311"
private const val MIN_IDE_VERSION_SUPPORT_JSON_WITH_METADATA = 241
private const val MIN_IDE_VERSION_SUPPORT_IMAGE_VIEWER = 242

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

val jsonEncodedDf =
if (ideBuildNumber == null || ideBuildNumber.majorVersion < MIN_IDE_VERSION_SUPPORT_JSON_WITH_METADATA) {
val jsonEncodedDf = when {
!ideBuildNumber.supportsDynamicNestedTables() -> {
json {
obj(
"nrow" to df.size.nrow,
Expand All @@ -74,19 +75,32 @@ internal inline fun <reified T : Any> JupyterHtmlRenderer.render(
"kotlin_dataframe" to encodeFrame(df.take(limit)),
)
}.toJsonString()
} else {
}

else -> {
val imageEncodingOptions =
if (ideBuildNumber.supportsImageViewer()) Base64ImageEncodingOptions() else null

df.toJsonWithMetadata(
limit,
reifiedDisplayConfiguration.rowsLimit,
imageEncodingOptions = Base64ImageEncodingOptions()
imageEncodingOptions = imageEncodingOptions
)
}
}

notebook.renderAsIFrameAsNeeded(html, staticHtml, jsonEncodedDf)
} else {
notebook.renderHtmlAsIFrameIfNeeded(html)
}
}

private fun KotlinNotebookPluginUtils.IdeBuildNumber?.supportsDynamicNestedTables() =
this != null && majorVersion >= MIN_IDE_VERSION_SUPPORT_JSON_WITH_METADATA

private fun KotlinNotebookPluginUtils.IdeBuildNumber?.supportsImageViewer() =
this != null && majorVersion >= MIN_IDE_VERSION_SUPPORT_IMAGE_VIEWER

internal fun Notebook.renderAsIFrameAsNeeded(
data: HtmlData,
staticData: HtmlData,
Expand Down

0 comments on commit a6f8e9f

Please sign in to comment.