From 9b5c34311f2c79bed12620920a1b81d6e115edb6 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Wed, 31 Mar 2021 19:39:34 +0700 Subject: [PATCH] Add Jupyter integration with kmath-ast --- build.gradle.kts | 2 ++ .../space/kscience/kmath/ast/MstAlgebra.kt | 1 + kmath-jupyter/build.gradle.kts | 15 +++++++++ .../kscience/kmath/jupyter/KMathJupyter.kt | 33 +++++++++++++++++++ settings.gradle.kts | 2 ++ 5 files changed, 53 insertions(+) create mode 100644 kmath-jupyter/build.gradle.kts create mode 100644 kmath-jupyter/src/main/kotlin/space/kscience/kmath/jupyter/KMathJupyter.kt diff --git a/build.gradle.kts b/build.gradle.kts index cc863a957..aae4d8de4 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,6 +4,7 @@ import java.net.URL plugins { id("ru.mipt.npm.gradle.project") + kotlin("jupyter.api") apply false } allprojects { @@ -14,6 +15,7 @@ allprojects { maven("https://dl.bintray.com/hotkeytlt/maven") maven("https://jitpack.io") maven("http://logicrunch.research.it.uu.se/maven/") + maven("https://maven.pkg.jetbrains.space/public/p/kotlinx-html/maven") mavenCentral() } diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstAlgebra.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstAlgebra.kt index c1aeae90e..aa3953c4e 100644 --- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstAlgebra.kt +++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstAlgebra.kt @@ -127,6 +127,7 @@ public object MstExtendedField : ExtendedField, NumericAlgebra { public override fun acosh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.ACOSH_OPERATION)(arg) public override fun atanh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.ATANH_OPERATION)(arg) public override fun add(a: MST, b: MST): MST.Binary = MstField.add(a, b) + public override fun sqrt(arg: MST): MST = unaryOperationFunction(PowerOperations.SQRT_OPERATION)(arg) public override fun scale(a: MST, value: Double): MST = binaryOperation(GroupOperations.PLUS_OPERATION, a, number(value)) diff --git a/kmath-jupyter/build.gradle.kts b/kmath-jupyter/build.gradle.kts new file mode 100644 index 000000000..9c5c8c107 --- /dev/null +++ b/kmath-jupyter/build.gradle.kts @@ -0,0 +1,15 @@ +import ru.mipt.npm.gradle.Maturity + +plugins { + id("ru.mipt.npm.gradle.jvm") + kotlin("jupyter.api") +} + +dependencies { + api(project(":kmath-ast")) + implementation("org.jetbrains.kotlinx:kotlinx-html-jvm:0.7.3") +} + +readme { + maturity = Maturity.PROTOTYPE +} 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 new file mode 100644 index 000000000..972b1b0f6 --- /dev/null +++ b/kmath-jupyter/src/main/kotlin/space/kscience/kmath/jupyter/KMathJupyter.kt @@ -0,0 +1,33 @@ +package space.kscience.kmath.jupyter + +import kotlinx.html.div +import kotlinx.html.stream.createHTML +import kotlinx.html.unsafe +import org.jetbrains.kotlinx.jupyter.api.HTML +import org.jetbrains.kotlinx.jupyter.api.annotations.JupyterLibrary +import org.jetbrains.kotlinx.jupyter.api.libraries.JupyterIntegration +import space.kscience.kmath.ast.MST +import space.kscience.kmath.ast.rendering.FeaturedMathRendererWithPostProcess +import space.kscience.kmath.ast.rendering.MathMLSyntaxRenderer +import space.kscience.kmath.ast.rendering.renderWithStringBuilder + +@JupyterLibrary +internal class KMathJupyter : JupyterIntegration() { + override fun Builder.onLoaded() { + import( + "space.kscience.kmath.ast.*", + "space.kscience.kmath.ast.rendering.*", + "space.kscience.kmath.operations.*", + "space.kscience.kmath.expressions.*", + "space.kscience.kmath.misc.*", + ) + + render { mst -> + HTML(createHTML().div { + unsafe { + +MathMLSyntaxRenderer.renderWithStringBuilder(FeaturedMathRendererWithPostProcess.Default.render(mst)) + } + }) + } + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 4467d5ed6..3d86de0e3 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -16,6 +16,7 @@ pluginManagement { id("ru.mipt.npm.gradle.mpp") version toolsVersion id("ru.mipt.npm.gradle.jvm") version toolsVersion id("ru.mipt.npm.gradle.publish") version toolsVersion + kotlin("jupyter.api") version "0.8.3.279" kotlin("jvm") version kotlinVersion kotlin("plugin.allopen") version kotlinVersion } @@ -40,5 +41,6 @@ include( ":kmath-ast", ":kmath-ejml", ":kmath-kotlingrad", + ":kmath-jupyter", ":examples" )