Each cell output is a mapping from MIME-type such as text/plain
, text/html
or image/png
, to the actual value. For example:
MIME("text/plain" to "Plain text")
Plain text
To refer MIME type, you can refer MimeTypes
object:
MIME(MimeTypes.PLAIN_TEXT to "Plain text")
Plain text
All the objects that have no defined renderers, are rendered to the plain text automatically.
"Plain text"
Plain text
Cell can have several outputs, use DISPLAY
to render them
DISPLAY("Plain text 1")
DISPLAY(MIME(MimeTypes.PLAIN_TEXT to "Plain text 2"))
Plain text 1
Plain text 2
HTML can be rendered this way:
MIME(MimeTypes.HTML to "<b>HTML</b> text")
There is also a shortcut for rendering HTML:
HTML("<b>HTML</b> text")
As it was said above, output is a map, thus it can have several key-value pairs. Only value with the highest priority will be rendered. Priorities are client-specific, and might be found in this StackOverflow answer.
MIME(
MimeTypes.PLAIN_TEXT to "Plain text",
MimeTypes.HTML to "<b>HTML</b> text",
)
Images could be rendered as HTML using an <img>
tag. But there is a separate MIME type for this purpose, use it in Kotlin Notebook as it doesn't utilize web rendering, and renders natively
MIME(MimeTypes.PNG to "iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAYAAAB5fY51AAAEcUlEQVR4Xu3SMY4cMQADwf3/p+1cHWvAE4pAJR3z9/v9/gH8EQkAqxIAViUArEoAWJUAsCoBYFUCwKoEgFUJAKsSAFYlAKxKAFiVALAqAWBVAsCqBIBVCQCrEgBWJQCsSgBYlQCwKgFgVQLAqgSAVQkAqxIAViUArEoAWJUAsCoBYFUCwKoEgFUJAKsSAFYlAKxKAFiVALAqAWBVAsCqBIBVCQCrEgBWJQCsSgBYlQCwKgFgVQLAqgSAVQkAqxIAViUArEoAWJUAsCoBYFUCwKoEgFUJAKsSAFYlAKxKAFiVALAqAWBVAsCqBIBVCQCrEgBWJQCsSgBYlQCwKgFgVQLAqgSAVQkAqxIAViUArEoAWJUAsCoBYFUCwKoEgFUJAKsSAFYlAKxKAFiVALAqAWBVAsCqBIBVCQCrEgBWJQCsSgBYlQCwKgFgVQLAqgSAVQkAqxIAViUArEoAW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KoEgFUJAKsSAFYlAEz6D5tIz0MBx/WUAAAAAElFTkSuQmCC")
Kandy library renders plots to both HTML and JSON spec that is rendered natively to a Swing component.
You can render this spec even without using Kandy library. Ensure that lets.plot.swing.outputs.enabled
registry key is enabled.
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.buildJsonObject
val jsonSpec = Json.decodeFromString<JsonElement>(
"""
{
"output_type": "lets_plot_spec",
"output": {
"mapping": {},
"data": {
"x": [
1.0,
2.0,
3.0
],
"y": [
1.0,
2.0,
3.0
]
},
"kind": "plot",
"scales": [
{
"aesthetic": "x",
"limits": [
null,
null
]
},
{
"aesthetic": "y",
"limits": [
null,
null
]
}
],
"layers": [
{
"mapping": {
"x": "x",
"y": "y"
},
"stat": "identity",
"sampling": "none",
"position": "dodge",
"geom": "bar"
}
]
},
"apply_color_scheme": true,
"swing_enabled": true
}
"""
)
MimeTypedResultEx(buildJsonObject {
put("application/plot+json", jsonSpec)
})