{
"cells": [
{
"cell_type": "markdown",
"source": [
"# Outputs"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"Each cell output is a mapping from MIME-type such as `text/plain`, `text/html` or `image/png`, to the actual value. For example:"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2023-05-31T18:49:29.894359400Z",
"start_time": "2023-05-31T18:49:29.201944100Z"
}
},
"outputs": [
{
"data": {
"text/plain": "Plain text"
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"MIME(\"text/plain\" to \"Plain text\")"
]
},
{
"cell_type": "markdown",
"source": [
"To refer MIME type, you can refer `MimeTypes` object:"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 2,
"outputs": [
{
"data": {
"text/plain": "Plain text"
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"MIME(MimeTypes.PLAIN_TEXT to \"Plain text\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-05-31T18:49:29.936067Z",
"start_time": "2023-05-31T18:49:29.466218Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"All the objects that have no defined renderers, are rendered to the plain text automatically."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 3,
"outputs": [
{
"data": {
"text/plain": "Plain text"
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"Plain text\""
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-05-31T18:49:29.957010Z",
"start_time": "2023-05-31T18:49:29.703337500Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"Cell can have several outputs, use `DISPLAY` to render them"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 4,
"outputs": [
{
"data": {
"text/plain": "Plain text 1"
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": "Plain text 2"
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"DISPLAY(\"Plain text 1\")\n",
"DISPLAY(MIME(MimeTypes.PLAIN_TEXT to \"Plain text 2\"))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-05-31T18:49:30.410203800Z",
"start_time": "2023-05-31T18:49:29.823883600Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"HTML can be rendered this way:"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 5,
"outputs": [
{
"data": {
"text/html": "HTML text"
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"MIME(MimeTypes.HTML to \"HTML text\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-05-31T18:49:30.539822900Z",
"start_time": "2023-05-31T18:49:30.039789100Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"There is also a shortcut for rendering HTML:"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 6,
"outputs": [
{
"data": {
"text/html": "HTML text"
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"HTML(\"HTML text\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-05-31T18:49:30.579440100Z",
"start_time": "2023-05-31T18:49:30.185425100Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"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.\n",
"Priorities are client-specific, and might be found in [this StackOverflow answer](https://stackoverflow.com/a/67276689)."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 7,
"outputs": [
{
"data": {
"text/plain": "Plain text",
"text/html": "HTML text"
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"MIME(\n",
" MimeTypes.PLAIN_TEXT to \"Plain text\",\n",
" MimeTypes.HTML to \"HTML text\",\n",
")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-05-31T18:49:30.798651300Z",
"start_time": "2023-05-31T18:49:30.314809800Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"Images could be rendered as HTML using an `
` 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"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 8,
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAYAAAB5fY51AAAEcUlEQVR4Xu3SMY4cMQADwf3/p+1cHWvAE4pAJR3z9/v9/gH8EQkAqxIAViUArEoAWJUAsCoBYFUCwKoEgFUJAKsSAFYlAKxKAFiVALAqAWBVAsCqBIBVCQCrEgBWJQCsSgBYlQCwKgFgVQLAqgSAVQkAqxIAViUArEoAWJUAsCoBYFUCwKoEgFUJAKsSAFYlAKxKAFiVALAqAWBVAsCqBIBVCQCrEgBWJQCsSgBYlQCwKgFgVQLAqgSAVQkAqxIAViUArEoAWJUAsCoBYFUCwKoEgFUJAKsSAFYlAKxKAFiVALAqAWBVAsCqBIBVCQCrEgBWJQCsSgBYlQCwKgFgVQLAqgSAVQkAqxIAViUArEoAWJUAsCoBYFUCwKoEgFUJAKsSAFYlAKxKAFiVALAqAWBVAsCqBIBVCQCrEgBWJQCsSgBYlQCwKgFgVQLAqgSAVQkAqxIAViUArEoAWJUAsCoBYFUCwKoEgFUJAKsSAFYlAKxKAFiVALAqAWBVAsCqBIBVCQCrEgBWJQCsSgBYlQCwKgFgVQLAqgSAVQkAqxIAViUArEoAWJUAsCoBYFUCwKoEgFUJAKsSAFYlAKxKAFiVALAqAWBVAsCqBIBVCQCrEgBWJQCsSgBYlQCwKgFgVQLAqgSAVQkAqxIAViUArEoAWJUAsCoBYFUCwKoEgFUJAKsSAFYlAKxKAFiVALAqAWBVAsCqBIBVCQCrEgBWJQCsSgBYlQCwKgFgVQLAqgSAVQkAqxIAViUArEoAWJUAsCoBYFUCwKoEgFUJAKsSAFYl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M5yR8wuzGzp/xnIRPmN3Y+TOek/AJsxs7f8ZzEj5hdmPnz3hOwifMbuz8Gc9J+ITZjZ0/4zkJnzC7sfNnPCfhE2Y3dv6M5yR8wuzGzp/xnIRPmN3Y+TOek/AJsxs7f8ZzEj5hdmPnz3hOwifMbuz8Gc9J+ITZjZ0/4zkJnzC7sfNnPCcBYFUCwKoEgFUJAKsSAFYlAKxKAFiVALAqAWBVAsCqBIBVCQCrEgBWJQCsSgBYlQCwKgFgVQLAqgSAVQkAqxIAViUArEoAWJUAsCoBYFUCwKoEgFUJAKsSAFYlAEz6D5tIz0MBx/WUAAAAAElFTkSuQmCC\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-05-31T18:49:30.844164500Z",
"start_time": "2023-05-31T18:49:30.517882100Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"[Kandy](https://github.com/Kotlin/kandy) library renders plots to both HTML and JSON spec that is rendered natively to a Swing component.\n",
"You can render this spec even without using Kandy library. Ensure that `lets.plot.swing.outputs.enabled` registry key is enabled."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 9,
"outputs": [
{
"data": {
"application/plot+json": {
"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
}
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import kotlinx.serialization.decodeFromString\n",
"import kotlinx.serialization.json.Json\n",
"import kotlinx.serialization.json.JsonElement\n",
"import kotlinx.serialization.json.buildJsonObject\n",
"\n",
"val jsonSpec = Json.decodeFromString(\n",
" \"\"\"\n",
" {\n",
" \"output_type\": \"lets_plot_spec\",\n",
" \"output\": {\n",
" \"mapping\": {},\n",
" \"data\": {\n",
" \"x\": [\n",
" 1.0,\n",
" 2.0,\n",
" 3.0\n",
" ],\n",
" \"y\": [\n",
" 1.0,\n",
" 2.0,\n",
" 3.0\n",
" ]\n",
" },\n",
" \"kind\": \"plot\",\n",
" \"scales\": [\n",
" {\n",
" \"aesthetic\": \"x\",\n",
" \"limits\": [\n",
" null,\n",
" null\n",
" ]\n",
" },\n",
" {\n",
" \"aesthetic\": \"y\",\n",
" \"limits\": [\n",
" null,\n",
" null\n",
" ]\n",
" }\n",
" ],\n",
" \"layers\": [\n",
" {\n",
" \"mapping\": {\n",
" \"x\": \"x\",\n",
" \"y\": \"y\"\n",
" },\n",
" \"stat\": \"identity\",\n",
" \"sampling\": \"none\",\n",
" \"position\": \"dodge\",\n",
" \"geom\": \"bar\"\n",
" }\n",
" ]\n",
" },\n",
" \"apply_color_scheme\": true,\n",
" \"swing_enabled\": true\n",
" }\n",
"\"\"\"\n",
")\n",
"\n",
"MimeTypedResultEx(buildJsonObject {\n",
" put(\"application/plot+json\", jsonSpec)\n",
"})"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-05-31T18:49:31.232043500Z",
"start_time": "2023-05-31T18:49:30.666374Z"
}
}
},
{
"cell_type": "code",
"execution_count": 10,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-05-31T18:49:31.236032500Z",
"start_time": "2023-05-31T18:49:31.225061700Z"
}
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Kotlin",
"language": "kotlin",
"name": "kotlin"
},
"language_info": {
"name": "kotlin",
"version": "1.8.20",
"mimetype": "text/x-kotlin",
"file_extension": ".kt",
"pygments_lexer": "kotlin",
"codemirror_mode": "text/x-kotlin",
"nbconvert_exporter": ""
},
"ktnbPluginMetadata": {
"isAddProjectLibrariesToClasspath": false
}
},
"nbformat": 4,
"nbformat_minor": 0
}