Update jupyter integration

This commit is contained in:
Alexander Nozik 2021-05-08 14:10:57 +03:00
parent 0622be2494
commit e4e661a3bf
4 changed files with 48 additions and 36 deletions

View File

@ -5,6 +5,7 @@
package space.kscience.kmath.structures package space.kscience.kmath.structures
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import org.nd4j.linalg.factory.Nd4j import org.nd4j.linalg.factory.Nd4j
import space.kscience.kmath.nd.* import space.kscience.kmath.nd.*
@ -22,6 +23,7 @@ internal inline fun measureAndPrint(title: String, block: () -> Unit) {
println("$title completed in $time millis") println("$title completed in $time millis")
} }
@OptIn(DelicateCoroutinesApi::class)
fun main() { fun main() {
// initializing Nd4j // initializing Nd4j
Nd4j.zeros(0) Nd4j.zeros(0)

View File

@ -7,7 +7,10 @@ dependencies {
api(project(":kmath-ast")) api(project(":kmath-ast"))
api(project(":kmath-complex")) api(project(":kmath-complex"))
api(project(":kmath-for-real")) api(project(":kmath-for-real"))
implementation("org.jetbrains.kotlinx:kotlinx-html-jvm:0.7.3") }
kscience{
useHtml()
} }
readme { readme {

View File

@ -19,49 +19,58 @@ import space.kscience.kmath.operations.RingOperations
import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.structures.asSequence 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 @JupyterLibrary
internal class KMathJupyter : JupyterIntegration() { internal class KMathJupyter : JupyterIntegration() {
private val mathRender = FeaturedMathRendererWithPostProcess.Default private val mathRender = FeaturedMathRendererWithPostProcess.Default
private val syntaxRender = MathMLSyntaxRenderer 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 -> {
+"<ms>"
+it.toString()
+"</ms>"
}
}
}
override fun Builder.onLoaded() { override fun Builder.onLoaded() {
import( import(
"space.kscience.kmath.ast.*", "space.kscience.kmath.ast.*",
"space.kscience.kmath.ast.rendering.*", "space.kscience.kmath.ast.rendering.*",
"space.kscience.kmath.structures.*",
"space.kscience.kmath.operations.*", "space.kscience.kmath.operations.*",
"space.kscience.kmath.expressions.*", "space.kscience.kmath.expressions.*",
"space.kscience.kmath.nd.*",
"space.kscience.kmath.misc.*", "space.kscience.kmath.misc.*",
"space.kscience.kmath.real.*", "space.kscience.kmath.real.*",
) )
fun MST.toDisplayResult(): DisplayResult = HTML(createHTML().div { import("space.kscience.kmath.jupyter.toMst")
unsafe {
+syntaxRender.renderWithStringBuilder(mathRender.render(this@toDisplayResult))
}
})
render<MST> { it.toDisplayResult() } render<MST> { it.toDisplayResult() }
render<Number> { MST.Numeric(it).toDisplayResult() } //render<Number> { 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 -> {
+"<ms>"
+it.toString()
+"</ms>"
}
}
}
render<Structure2D<*>> { structure -> render<Structure2D<*>> { structure ->
HTML(createHTML().div { HTML(createHTML().div {

View File

@ -1,25 +1,23 @@
pluginManagement { pluginManagement {
repositories { repositories {
mavenLocal()
mavenCentral() mavenCentral()
gradlePluginPortal() gradlePluginPortal()
maven("https://repo.kotlin.link") maven("https://repo.kotlin.link")
} }
val toolsVersion = "0.9.5-dev-2" val toolsVersion = "0.9.6"
val kotlinVersion = "1.5.0-RC" val kotlinVersion = "1.5.0"
plugins { 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("multiplatform") version kotlinVersion
kotlin("jvm") version kotlinVersion kotlin("jvm") version kotlinVersion
kotlin("plugin.allopen") version kotlinVersion kotlin("plugin.allopen") version kotlinVersion
id("org.jetbrains.kotlinx.benchmark") version "0.3.0" id("org.jetbrains.kotlinx.benchmark") version "0.3.0"
id("ru.mipt.npm.gradle.project") version toolsVersion kotlin("jupyter.api") version "0.9.1-61"
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
} }
} }