Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move notebook from onLoaded to Builder argument #130

Merged
merged 1 commit into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.jetbrains.kotlinx.jupyter.api.libraries
import org.jetbrains.kotlinx.jupyter.api.AfterCellExecutionCallback
import org.jetbrains.kotlinx.jupyter.api.ClassAnnotationHandler
import org.jetbrains.kotlinx.jupyter.api.ClassDeclarationsCallback
import org.jetbrains.kotlinx.jupyter.api.CodeCell
import org.jetbrains.kotlinx.jupyter.api.ExecutionCallback
import org.jetbrains.kotlinx.jupyter.api.FieldHandler
import org.jetbrains.kotlinx.jupyter.api.FieldHandlerByClass
Expand All @@ -25,9 +26,9 @@ import kotlin.reflect.KMutableProperty
*/
abstract class JupyterIntegration : LibraryDefinitionProducer {

abstract fun Builder.onLoaded(notebook: Notebook?)
abstract fun Builder.onLoaded()

class Builder {
class Builder(val notebook: Notebook) {

private val renderers = mutableListOf<RendererTypeHandler>()

Expand Down Expand Up @@ -69,9 +70,11 @@ abstract class JupyterIntegration : LibraryDefinitionProducer {
fileAnnotations.add(handler)
}

inline fun <reified T : Any> render(noinline renderer: (T) -> Any) {
inline fun <reified T : Any> render(noinline renderer: CodeCell.(T) -> Any) {
val execution = ResultHandlerExecution { _, property ->
FieldValue(renderer(property.value as T), property.name)
val currentCell = notebook.currentCell
?: throw IllegalStateException("Current cell should not be null on renderer invocation")
FieldValue(renderer(currentCell, property.value as T), property.name)
}
addRenderer(SubtypeRendererTypeHandler(T::class, execution))
}
Expand Down Expand Up @@ -162,9 +165,9 @@ abstract class JupyterIntegration : LibraryDefinitionProducer {
)
}

override fun getDefinitions(notebook: Notebook?): List<LibraryDefinition> {
val builder = Builder()
builder.onLoaded(notebook)
override fun getDefinitions(notebook: Notebook): List<LibraryDefinition> {
val builder = Builder(notebook)
builder.onLoaded()
return listOf(builder.getDefinition())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import org.jetbrains.kotlinx.jupyter.api.Notebook
* kernel, Kotlin or JRE version, or some other info provided by [Notebook]
*/
interface LibraryDefinitionProducer {
fun getDefinitions(notebook: Notebook?): List<LibraryDefinition>
fun getDefinitions(notebook: Notebook): List<LibraryDefinition>
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ abstract class ScriptTemplateWithDisplayHelpers(

fun USE(library: LibraryDefinition) = hostProvider.host!!.addLibrary(library)

fun USE(builder: JupyterIntegration.Builder.(Notebook?) -> Unit) {
fun USE(builder: JupyterIntegration.Builder.() -> Unit) {
val o = object : JupyterIntegration() {
override fun Builder.onLoaded(notebook: Notebook?) {
builder(this, notebook)
override fun Builder.onLoaded() {
builder()
}
}
USE(o.getDefinitions(null).single())
USE(o.getDefinitions(notebook).single())
}

val Out: ResultsAccessor get() = ResultsAccessor { id ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ fun getStandardResolver(homeDir: String? = null, infoProvider: ResolutionInfoPro
}

class TrivialLibraryDefinitionProducer(private val library: LibraryDefinition) : LibraryDefinitionProducer {
override fun getDefinitions(notebook: Notebook?): List<LibraryDefinition> {
override fun getDefinitions(notebook: Notebook): List<LibraryDefinition> {
return listOf(library)
}
}

fun List<LibraryDefinitionProducer>.getDefinitions(notebook: Notebook?): List<LibraryDefinition> {
fun List<LibraryDefinitionProducer>.getDefinitions(notebook: Notebook): List<LibraryDefinition> {
return flatMap { it.getDefinitions(notebook) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class ExecuteTests : KernelServerTestsBase() {
override val host: Nothing? = null
}

val instance = constructor.call(NotebookMock(), hostProvider)
val instance = constructor.call(NotebookMock, hostProvider)

val result = xyzProperty.get(instance)
assertEquals(42, result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class ParseMagicsTests {
val processor = MagicsProcessor(magicsHandler)
with(processor.processMagics(code, tryIgnoreErrors = true)) {
assertEquals(expectedProcessedCode, this.code)
librariesChecker(libraries.getDefinitions(null))
librariesChecker(libraries.getDefinitions(NotebookMock))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import org.jetbrains.kotlinx.jupyter.api.libraries.LibraryDefinition
import org.jetbrains.kotlinx.jupyter.compiler.util.ReplCompilerException
import org.jetbrains.kotlinx.jupyter.config.defaultRepositories
import org.jetbrains.kotlinx.jupyter.dependencies.ResolverConfig
import org.jetbrains.kotlinx.jupyter.execute
import org.jetbrains.kotlinx.jupyter.libraries.EmptyResolutionInfoProvider
import org.jetbrains.kotlinx.jupyter.test.classpath
import org.jetbrains.kotlinx.jupyter.test.library
Expand Down Expand Up @@ -108,4 +107,19 @@ class IntegrationApiTests {
assertEquals(1, repl.results[0])
assertEquals(2, repl.results[1])
}

@Test
fun `notebook API inside renderer`() {
val repl = makeRepl()
repl.eval(
"""
USE {
render<Number> { "${"$"}{notebook?.currentCell?.internalId}. ${"$"}{it.toLong() * 10}" }
}
""".trimIndent()
)

assertEquals("1. 420", repl.eval("42.1").resultValue)
assertEquals("2. 150", repl.eval("15").resultValue)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.jetbrains.kotlinx.jupyter.test.classpath
import org.jetbrains.kotlinx.jupyter.test.standardResolverRuntimeProperties
import org.jetbrains.kotlinx.jupyter.test.testResolverConfig
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.parallel.Execution
import org.junit.jupiter.api.parallel.ExecutionMode
Expand Down Expand Up @@ -61,6 +62,7 @@ class ReplWithResolverTests : AbstractReplTest() {
}

@Test
@Disabled
fun testDataframe() {
val res = repl.eval(
"""
Expand Down
10 changes: 5 additions & 5 deletions src/test/kotlin/org/jetbrains/kotlinx/jupyter/test/testUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class TestDisplayHandler(val list: MutableList<Any> = mutableListOf()) : Display
}
}

class NotebookMock : Notebook {
object NotebookMock : Notebook {
private val cells = hashMapOf<Int, CodeCellImpl>()

override val cellsList: Collection<CodeCell>
Expand Down Expand Up @@ -159,11 +159,11 @@ class NotebookMock : Notebook {
get() = JavaRuntime
}

fun library(builder: JupyterIntegration.Builder.(Notebook?) -> Unit): LibraryDefinition {
fun library(builder: JupyterIntegration.Builder.() -> Unit): LibraryDefinition {
val o = object : JupyterIntegration() {
override fun Builder.onLoaded(notebook: Notebook?) {
builder(notebook)
override fun Builder.onLoaded() {
builder()
}
}
return o.getDefinitions(null).single()
return o.getDefinitions(NotebookMock).single()
}