Skip to content

Commit c2c4251

Browse files
committed
Override Default Renderer for BufferedImage
The default kotlin kernel renderer in the Kotlin Notebook plugin serializes images in their original dimensions, potentially causing an OOM exception. This commit overrides the default renderer to prevent such issues.
1 parent 1bd5a3d commit c2c4251

File tree

2 files changed

+38
-0
lines changed
  • core
    • generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/jupyter
    • src/main/kotlin/org/jetbrains/kotlinx/dataframe/jupyter

2 files changed

+38
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,16 @@ import org.jetbrains.kotlinx.dataframe.dataTypes.IMG
4141
import org.jetbrains.kotlinx.dataframe.impl.codeGen.CodeGenerationReadResult
4242
import org.jetbrains.kotlinx.dataframe.impl.codeGen.urlCodeGenReader
4343
import org.jetbrains.kotlinx.dataframe.impl.createStarProjectedType
44+
import org.jetbrains.kotlinx.dataframe.impl.io.resizeKeepingAspectRatio
45+
import org.jetbrains.kotlinx.dataframe.impl.io.toBase64
46+
import org.jetbrains.kotlinx.dataframe.impl.io.toByteArray
4447
import org.jetbrains.kotlinx.dataframe.impl.renderType
4548
import org.jetbrains.kotlinx.dataframe.io.DataFrameHtmlData
4649
import org.jetbrains.kotlinx.dataframe.io.SupportedCodeGenerationFormat
4750
import org.jetbrains.kotlinx.dataframe.io.supportedFormats
4851
import org.jetbrains.kotlinx.jupyter.api.*
4952
import org.jetbrains.kotlinx.jupyter.api.libraries.*
53+
import java.awt.image.BufferedImage
5054
import kotlin.reflect.KClass
5155
import kotlin.reflect.KProperty
5256
import kotlin.reflect.KType
@@ -55,6 +59,8 @@ import kotlin.reflect.full.isSubtypeOf
5559
/** Users will get an error if their Kotlin Jupyter kernel is older than this version. */
5660
private const val MIN_KERNEL_VERSION = "0.11.0.198"
5761

62+
private const val DEFAULT_HTML_IMG_SIZE = 100
63+
5864
internal val newDataSchemas = mutableListOf<KClass<*>>()
5965

6066
internal class Integration(
@@ -203,6 +209,19 @@ internal class Integration(
203209
}
204210
}
205211

212+
notebook.renderersProcessor.registerWithoutOptimizing(
213+
createRenderer<BufferedImage> {
214+
val src = buildString {
215+
append("""data:image/$DEFAULT_HTML_IMG_SIZE;base64,""")
216+
append(
217+
it.resizeKeepingAspectRatio(DEFAULT_HTML_IMG_SIZE).toByteArray().toBase64()
218+
)
219+
}
220+
HTML("""<img src="$src"/>""")
221+
},
222+
ProcessingPriority.HIGHER
223+
)
224+
206225
with(JupyterHtmlRenderer(config.display, this)) {
207226
render<DisableRowsLimitWrapper>(
208227
{ "DataRow: index = ${it.value.rowsCount()}, columnsCount = ${it.value.columnsCount()}" },

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,16 @@ import org.jetbrains.kotlinx.dataframe.dataTypes.IMG
4141
import org.jetbrains.kotlinx.dataframe.impl.codeGen.CodeGenerationReadResult
4242
import org.jetbrains.kotlinx.dataframe.impl.codeGen.urlCodeGenReader
4343
import org.jetbrains.kotlinx.dataframe.impl.createStarProjectedType
44+
import org.jetbrains.kotlinx.dataframe.impl.io.resizeKeepingAspectRatio
45+
import org.jetbrains.kotlinx.dataframe.impl.io.toBase64
46+
import org.jetbrains.kotlinx.dataframe.impl.io.toByteArray
4447
import org.jetbrains.kotlinx.dataframe.impl.renderType
4548
import org.jetbrains.kotlinx.dataframe.io.DataFrameHtmlData
4649
import org.jetbrains.kotlinx.dataframe.io.SupportedCodeGenerationFormat
4750
import org.jetbrains.kotlinx.dataframe.io.supportedFormats
4851
import org.jetbrains.kotlinx.jupyter.api.*
4952
import org.jetbrains.kotlinx.jupyter.api.libraries.*
53+
import java.awt.image.BufferedImage
5054
import kotlin.reflect.KClass
5155
import kotlin.reflect.KProperty
5256
import kotlin.reflect.KType
@@ -55,6 +59,8 @@ import kotlin.reflect.full.isSubtypeOf
5559
/** Users will get an error if their Kotlin Jupyter kernel is older than this version. */
5660
private const val MIN_KERNEL_VERSION = "0.11.0.198"
5761

62+
private const val DEFAULT_HTML_IMG_SIZE = 100
63+
5864
internal val newDataSchemas = mutableListOf<KClass<*>>()
5965

6066
internal class Integration(
@@ -203,6 +209,19 @@ internal class Integration(
203209
}
204210
}
205211

212+
notebook.renderersProcessor.registerWithoutOptimizing(
213+
createRenderer<BufferedImage> {
214+
val src = buildString {
215+
append("""data:image/$DEFAULT_HTML_IMG_SIZE;base64,""")
216+
append(
217+
it.resizeKeepingAspectRatio(DEFAULT_HTML_IMG_SIZE).toByteArray().toBase64()
218+
)
219+
}
220+
HTML("""<img src="$src"/>""")
221+
},
222+
ProcessingPriority.HIGHER
223+
)
224+
206225
with(JupyterHtmlRenderer(config.display, this)) {
207226
render<DisableRowsLimitWrapper>(
208227
{ "DataRow: index = ${it.value.rowsCount()}, columnsCount = ${it.value.columnsCount()}" },

0 commit comments

Comments
 (0)