visionforge/demo/playground/src/jvmMain/kotlin/VisionForgePlayGroundForJupyter.kt

86 lines
2.6 KiB
Kotlin
Raw Normal View History

2021-12-31 13:59:27 +03:00
package space.kscience.visionforge.examples
2021-03-07 16:19:43 +03:00
2021-02-23 21:47:18 +03:00
import kotlinx.html.stream.createHTML
import org.jetbrains.kotlinx.jupyter.api.HTML
2021-12-31 13:59:27 +03:00
import org.jetbrains.kotlinx.jupyter.api.libraries.JupyterIntegration
import org.jetbrains.kotlinx.jupyter.api.libraries.resources
2021-05-05 15:28:06 +03:00
import space.kscience.dataforge.context.Context
2021-03-07 16:19:43 +03:00
import space.kscience.dataforge.misc.DFExperimental
2021-02-23 21:47:18 +03:00
import space.kscience.gdml.Gdml
2021-03-07 16:19:43 +03:00
import space.kscience.plotly.Plot
import space.kscience.visionforge.Vision
import space.kscience.visionforge.gdml.toVision
import space.kscience.visionforge.html.HtmlVisionFragment
import space.kscience.visionforge.html.Page
2021-12-31 13:59:27 +03:00
import space.kscience.visionforge.html.embedAndRenderVisionFragment
2021-05-05 15:28:06 +03:00
import space.kscience.visionforge.plotly.PlotlyPlugin
2021-06-05 22:31:16 +03:00
import space.kscience.visionforge.plotly.asVision
2021-03-07 16:19:43 +03:00
import space.kscience.visionforge.solid.Solids
import space.kscience.visionforge.visionManager
2021-02-23 21:47:18 +03:00
2021-02-24 18:26:15 +03:00
@DFExperimental
2021-08-20 20:24:41 +03:00
public class VisionForgePlayGroundForJupyter : JupyterIntegration() {
2021-02-23 21:47:18 +03:00
2021-05-05 15:28:06 +03:00
private val context = Context("VisionForge") {
plugin(Solids)
plugin(PlotlyPlugin)
}
private var counter = 0
2021-12-31 13:59:27 +03:00
private fun produceHtmlVisionString(fragment: HtmlVisionFragment) = createHTML().apply {
embedAndRenderVisionFragment(context.visionManager, counter++, fragment = fragment)
}.finalize()
override fun Builder.onLoaded() {
2021-12-31 13:59:27 +03:00
resources {
js("VisionForge"){
classPath("js/visionforge-playground.js")
}
}
2021-02-23 21:47:18 +03:00
2021-02-24 22:48:17 +03:00
import(
"space.kscience.gdml.*",
2021-03-07 16:19:43 +03:00
"space.kscience.plotly.*",
"space.kscience.plotly.models.*",
2021-02-24 18:26:15 +03:00
"kotlinx.html.*",
2021-03-07 16:19:43 +03:00
"space.kscience.visionforge.solid.*",
"space.kscience.visionforge.html.Page",
"space.kscience.visionforge.html.page"
2021-02-24 22:48:17 +03:00
)
2021-02-23 21:47:18 +03:00
render<Gdml> { gdmlModel ->
2021-05-05 15:28:06 +03:00
val fragment = HtmlVisionFragment {
2021-02-24 18:26:15 +03:00
vision(gdmlModel.toVision())
}
HTML(produceHtmlVisionString(fragment))
}
render<Vision> { vision ->
2021-05-05 15:28:06 +03:00
val fragment = HtmlVisionFragment {
2021-02-24 18:26:15 +03:00
vision(vision)
2021-02-23 21:47:18 +03:00
}
HTML(produceHtmlVisionString(fragment))
2021-02-23 21:47:18 +03:00
}
render<Plot> { plot ->
2021-05-05 15:28:06 +03:00
val fragment = HtmlVisionFragment {
2021-06-05 22:31:16 +03:00
vision(plot.asVision())
2021-02-23 21:47:18 +03:00
}
2021-02-24 18:26:15 +03:00
HTML(produceHtmlVisionString(fragment))
2021-02-23 21:47:18 +03:00
}
2021-03-07 16:19:43 +03:00
render<space.kscience.plotly.PlotlyHtmlFragment> { fragment ->
2021-02-23 21:47:18 +03:00
HTML(createHTML().apply(fragment.visit).finalize())
}
render<Page> { page ->
HTML(page.render(createHTML()), true)
}
}
}