Skip to content

Commit a486778

Browse files
committed
Add helpful methods to work with displays updating
Relates to #318
1 parent aa84a23 commit a486778

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

jupyter-lib/api/src/main/kotlin/org/jetbrains/kotlinx/jupyter/api/results.kt

+8
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ class MimeTypedResult(
127127
result.setDisplayId(id)
128128
return Json.encodeToJsonElement(result) as JsonObject
129129
}
130+
131+
/**
132+
* Adds an [id] to this [MimeTypedResult] object
133+
*/
134+
@Suppress("unused")
135+
fun withId(id: String?): MimeTypedResult {
136+
return MimeTypedResult(mimeData, isolatedHtml, id)
137+
}
130138
}
131139

132140
// Convenience methods for displaying results

jupyter-lib/lib/src/main/kotlin/jupyter/kotlin/ScriptTemplateWithDisplayHelpers.kt

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package jupyter.kotlin
22

3+
import org.jetbrains.kotlinx.jupyter.api.KotlinKernelHost
34
import org.jetbrains.kotlinx.jupyter.api.Notebook
45
import org.jetbrains.kotlinx.jupyter.api.ResultsAccessor
56
import org.jetbrains.kotlinx.jupyter.api.libraries.CodeExecution
@@ -10,11 +11,15 @@ abstract class ScriptTemplateWithDisplayHelpers(
1011
val notebook: Notebook,
1112
private val hostProvider: KotlinKernelHostProvider
1213
) {
13-
fun DISPLAY(value: Any) = hostProvider.host!!.display(value)
14+
private val host: KotlinKernelHost get() = hostProvider.host!!
1415

15-
fun EXECUTE(code: String) = hostProvider.host!!.scheduleExecution(CodeExecution(code).toExecutionCallback())
16+
fun DISPLAY(value: Any) = host.display(value)
1617

17-
fun USE(library: LibraryDefinition) = hostProvider.host!!.addLibrary(library)
18+
fun UPDATE_DISPLAY(value: Any, id: String?) = host.updateDisplay(value, id)
19+
20+
fun EXECUTE(code: String) = host.scheduleExecution(CodeExecution(code).toExecutionCallback())
21+
22+
fun USE(library: LibraryDefinition) = host.addLibrary(library)
1823

1924
fun USE(builder: JupyterIntegration.Builder.() -> Unit) {
2025
val o = object : JupyterIntegration() {

0 commit comments

Comments
 (0)