From e4e661a3bf4946eabd364ac93f62c9a8998e24c3 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 8 May 2021 14:10:57 +0300 Subject: [PATCH] Update jupyter integration --- .../kscience/kmath/structures/NDField.kt | 2 + kmath-jupyter/build.gradle.kts | 5 +- .../kscience/kmath/jupyter/KMathJupyter.kt | 61 +++++++++++-------- settings.gradle.kts | 16 +++-- 4 files changed, 48 insertions(+), 36 deletions(-) diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/NDField.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/NDField.kt index cc1f5f680..501bf98db 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/NDField.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/NDField.kt @@ -5,6 +5,7 @@ package space.kscience.kmath.structures +import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.GlobalScope import org.nd4j.linalg.factory.Nd4j import space.kscience.kmath.nd.* @@ -22,6 +23,7 @@ internal inline fun measureAndPrint(title: String, block: () -> Unit) { println("$title completed in $time millis") } +@OptIn(DelicateCoroutinesApi::class) fun main() { // initializing Nd4j Nd4j.zeros(0) diff --git a/kmath-jupyter/build.gradle.kts b/kmath-jupyter/build.gradle.kts index 815cb9b8c..83a6a771a 100644 --- a/kmath-jupyter/build.gradle.kts +++ b/kmath-jupyter/build.gradle.kts @@ -7,7 +7,10 @@ dependencies { api(project(":kmath-ast")) api(project(":kmath-complex")) api(project(":kmath-for-real")) - implementation("org.jetbrains.kotlinx:kotlinx-html-jvm:0.7.3") +} + +kscience{ + useHtml() } readme { diff --git a/kmath-jupyter/src/main/kotlin/space/kscience/kmath/jupyter/KMathJupyter.kt b/kmath-jupyter/src/main/kotlin/space/kscience/kmath/jupyter/KMathJupyter.kt index e13c92f75..78b25af47 100644 --- a/kmath-jupyter/src/main/kotlin/space/kscience/kmath/jupyter/KMathJupyter.kt +++ b/kmath-jupyter/src/main/kotlin/space/kscience/kmath/jupyter/KMathJupyter.kt @@ -19,49 +19,58 @@ import space.kscience.kmath.operations.RingOperations import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.asSequence +/** + * A function for conversion of number to MST for pretty print + */ +public fun Number.toMst(): MST.Numeric = MST.Numeric(this) + @JupyterLibrary internal class KMathJupyter : JupyterIntegration() { private val mathRender = FeaturedMathRendererWithPostProcess.Default private val syntaxRender = MathMLSyntaxRenderer + private fun MST.toDisplayResult(): DisplayResult = HTML(createHTML().div { + unsafe { + +syntaxRender.renderWithStringBuilder(mathRender.render(this@toDisplayResult)) + } + }) + + private fun Unsafe.appendCellValue(it: Any?) { + when (it) { + is Number -> { + val s = StringBuilder() + syntaxRender.renderPart(mathRender.render(MST.Numeric(it)), s) + +s.toString() + } + is MST -> { + val s = StringBuilder() + syntaxRender.renderPart(mathRender.render(it), s) + +s.toString() + } + else -> { + +"" + +it.toString() + +"" + } + } + } + override fun Builder.onLoaded() { import( "space.kscience.kmath.ast.*", "space.kscience.kmath.ast.rendering.*", + "space.kscience.kmath.structures.*", "space.kscience.kmath.operations.*", "space.kscience.kmath.expressions.*", + "space.kscience.kmath.nd.*", "space.kscience.kmath.misc.*", "space.kscience.kmath.real.*", ) - fun MST.toDisplayResult(): DisplayResult = HTML(createHTML().div { - unsafe { - +syntaxRender.renderWithStringBuilder(mathRender.render(this@toDisplayResult)) - } - }) + import("space.kscience.kmath.jupyter.toMst") render { it.toDisplayResult() } - render { MST.Numeric(it).toDisplayResult() } - - fun Unsafe.appendCellValue(it: Any?) { - when (it) { - is Number -> { - val s = StringBuilder() - syntaxRender.renderPart(mathRender.render(MST.Numeric(it)), s) - +s.toString() - } - is MST -> { - val s = StringBuilder() - syntaxRender.renderPart(mathRender.render(it), s) - +s.toString() - } - else -> { - +"" - +it.toString() - +"" - } - } - } + //render { MST.Numeric(it).toDisplayResult() } render> { structure -> HTML(createHTML().div { diff --git a/settings.gradle.kts b/settings.gradle.kts index 90cc4aef1..27e74c902 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,25 +1,23 @@ pluginManagement { repositories { - mavenLocal() mavenCentral() gradlePluginPortal() maven("https://repo.kotlin.link") } - val toolsVersion = "0.9.5-dev-2" - val kotlinVersion = "1.5.0-RC" + val toolsVersion = "0.9.6" + val kotlinVersion = "1.5.0" plugins { + id("ru.mipt.npm.gradle.project") version toolsVersion + id("ru.mipt.npm.gradle.mpp") version toolsVersion + id("ru.mipt.npm.gradle.jvm") version toolsVersion kotlin("multiplatform") version kotlinVersion kotlin("jvm") version kotlinVersion kotlin("plugin.allopen") version kotlinVersion id("org.jetbrains.kotlinx.benchmark") version "0.3.0" - id("ru.mipt.npm.gradle.project") version toolsVersion - id("ru.mipt.npm.gradle.mpp") version toolsVersion - id("ru.mipt.npm.gradle.jvm") version toolsVersion - kotlin("jupyter.api") version "0.9.0.12" - kotlin("jvm") version kotlinVersion - kotlin("plugin.allopen") version kotlinVersion + kotlin("jupyter.api") version "0.9.1-61" + } }