visionforge/demo/jupyter-playground/src/main/kotlin/VisionForgePlayGroundForJupyter.kt

97 lines
2.9 KiB
Kotlin
Raw Normal View History

2021-08-20 20:24:41 +03:00
package playground
2021-03-07 16:19:43 +03:00
2021-02-23 21:47:18 +03:00
import kotlinx.html.div
import kotlinx.html.id
import kotlinx.html.script
2021-02-23 21:47:18 +03:00
import kotlinx.html.stream.createHTML
import kotlinx.html.unsafe
2021-02-23 21:47:18 +03:00
import org.jetbrains.kotlinx.jupyter.api.HTML
import org.jetbrains.kotlinx.jupyter.api.libraries.*
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
import space.kscience.visionforge.html.embedVisionFragment
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)
}
2021-02-24 22:48:17 +03:00
private val jsBundle = ResourceFallbacksBundle(listOf(
ResourceLocation("js/visionforge-playground.js", ResourcePathType.CLASSPATH_PATH))
)
private val jsResource = LibraryResource(name = "VisionForge", type = ResourceType.JS, bundles = listOf(jsBundle))
2021-02-23 21:47:18 +03:00
private var counter = 0
private fun produceHtmlVisionString(fragment: HtmlVisionFragment) = createHTML().div {
val id = "visionforge.vision[${counter++}]"
div {
this.id = id
2021-05-05 15:28:06 +03:00
embedVisionFragment(context.visionManager, fragment = fragment)
}
script {
type = "text/javascript"
2021-05-05 15:28:06 +03:00
unsafe { +"window.renderAllVisionsById(\"$id\");" }
}
}
override fun Builder.onLoaded() {
2021-02-23 21:47:18 +03:00
resource(jsResource)
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)
}
}
}