Complete playfround jupyter demo

This commit is contained in:
Alexander Nozik 2021-02-24 18:26:15 +03:00
parent 5d0ceb8e50
commit 27fe492ab7
9 changed files with 86 additions and 46 deletions

View File

@ -1,13 +1,17 @@
package hep.dataforge.playground
import hep.dataforge.context.Context
import hep.dataforge.misc.DFExperimental
import hep.dataforge.vision.Vision
import hep.dataforge.vision.VisionForge
import hep.dataforge.vision.VisionManager
import hep.dataforge.vision.gdml.gdml
import hep.dataforge.vision.html.*
import hep.dataforge.vision.plotly.PlotlyPlugin
import hep.dataforge.vision.plotly.VisionOfPlotly
import hep.dataforge.vision.solid.SolidManager
import hep.dataforge.vision.solid.solid
import hep.dataforge.vision.gdml.toVision
import hep.dataforge.vision.html.HtmlVisionFragment
import hep.dataforge.vision.html.Page
import hep.dataforge.vision.html.embedVisionFragment
import hep.dataforge.vision.html.fragment
import hep.dataforge.vision.plotly.toVision
import hep.dataforge.vision.plotly.withPlotly
import hep.dataforge.vision.solid.withSolids
import hep.dataforge.vision.visionManager
import kotlinx.html.div
import kotlinx.html.id
@ -15,22 +19,16 @@ import kotlinx.html.script
import kotlinx.html.stream.createHTML
import kotlinx.html.unsafe
import kscience.plotly.Plot
import kscience.plotly.PlotlyFragment
import org.jetbrains.kotlinx.jupyter.api.HTML
import org.jetbrains.kotlinx.jupyter.api.Notebook
import org.jetbrains.kotlinx.jupyter.api.annotations.JupyterLibrary
import org.jetbrains.kotlinx.jupyter.api.libraries.*
import org.jetbrains.kotlinx.jupyter.api.libraries.ResourceLocation
import space.kscience.gdml.Gdml
@JupyterLibrary
@DFExperimental
internal class VisionForgePlayGroundForJupyter : JupyterIntegration() {
private val context = Context("Playground") {
plugin(SolidManager)
plugin(PlotlyPlugin)
}
val jsBundle = ResourceFallbacksBundle(listOf(ResourceLocation("js/visionforge-playground.js",
ResourcePathType.CLASSPATH_PATH)))
val jsResource = LibraryResource(name = "VisionForge", type = ResourceType.JS, bundles = listOf(jsBundle))
@ -41,7 +39,7 @@ internal class VisionForgePlayGroundForJupyter : JupyterIntegration() {
val id = "visionforge.vision[${counter++}]"
div {
this.id = id
embedVisionFragment(context.visionManager, fragment = fragment)
embedVisionFragment(VisionForge.visionManager, fragment = fragment)
}
script {
type = "text/javascript"
@ -52,15 +50,27 @@ internal class VisionForgePlayGroundForJupyter : JupyterIntegration() {
override fun Builder.onLoaded(notebook: Notebook?) {
resource(jsResource)
import("space.kscience.gdml.*", "kscience.plotly.*", "kscience.plotly.models.*")
onLoaded {
VisionForge.withPlotly().withSolids()
}
import("space.kscience.gdml.*",
"kscience.plotly.*",
"kscience.plotly.models.*",
"kotlinx.html.*",
"hep.dataforge.vision.solid.*")
import("hep.dataforge.vision.VisionForge")
render<Gdml> { gdmlModel ->
val fragment = VisionManager.fragment {
vision {
solid {
gdml(gdmlModel)
}
}
vision(gdmlModel.toVision())
}
HTML(produceHtmlVisionString(fragment))
}
render<Vision> { vision ->
val fragment = VisionManager.fragment {
vision(vision)
}
HTML(produceHtmlVisionString(fragment))
@ -68,12 +78,10 @@ internal class VisionForgePlayGroundForJupyter : JupyterIntegration() {
render<Plot> { plot ->
val fragment = VisionManager.fragment {
vision {
VisionOfPlotly(plot.config)
}
vision(plot.toVision())
}
HTML( produceHtmlVisionString(fragment))
HTML(produceHtmlVisionString(fragment))
}
render<kscience.plotly.HtmlFragment> { fragment ->
@ -83,8 +91,6 @@ internal class VisionForgePlayGroundForJupyter : JupyterIntegration() {
render<Page> { page ->
HTML(page.render(createHTML()), true)
}
}
}

View File

@ -3,10 +3,9 @@ package hep.dataforge.vision.examples
import hep.dataforge.misc.DFExperimental
import hep.dataforge.vision.VisionForge
import hep.dataforge.vision.VisionManager
import hep.dataforge.vision.gdml.gdml
import hep.dataforge.vision.gdml.toVision
import hep.dataforge.vision.html.ResourceLocation
import hep.dataforge.vision.html.fragment
import hep.dataforge.vision.solid.solid
import hep.dataforge.vision.solid.withSolids
import space.kscience.gdml.*
@ -62,11 +61,11 @@ internal val cubes = Gdml {
@DFExperimental
fun main() {
println(cubes.encodeToString())
val content = VisionManager.fragment {
vision("canvas") {
solid {
gdml(cubes)
}
cubes.toVision()
}
}

View File

@ -2,6 +2,7 @@ package hep.dataforge.vision.examples
import hep.dataforge.context.Context
import hep.dataforge.misc.DFExperimental
import hep.dataforge.vision.Vision
import hep.dataforge.vision.html.HtmlVisionFragment
import hep.dataforge.vision.html.ResourceLocation
import hep.dataforge.vision.html.page
@ -30,6 +31,12 @@ public fun Context.makeVisionFile(
}
if (show) Desktop.getDesktop().browse(actualPath.toFile().toURI())
}
// makeVisionFile(fragment, path = path, title = title, show = show) { actualPath ->
// scriptHeader("js/visionforge-playground.js", actualPath, resourceLocation)
//}
@DFExperimental
public fun Context.makeVisionFile(
vision: Vision,
path: Path? = null,
title: String = "VisionForge page",
resourceLocation: ResourceLocation = ResourceLocation.SYSTEM,
show: Boolean = true,
): Unit = makeVisionFile({ vision(vision) }, path, title, resourceLocation, show)

View File

@ -11,7 +11,6 @@ import hep.dataforge.vision.Vision
import hep.dataforge.vision.VisionManager
import kotlinx.html.*
import kotlin.collections.set
import kotlin.collections.setOf
/**
@ -46,6 +45,7 @@ public abstract class VisionTagConsumer<R>(
/**
* Create a placeholder for a vision output with optional [Vision] in it
* TODO with multi-receivers could be replaced by [VisionTagConsumer, TagConsumer] extension
*/
public fun <T> TagConsumer<T>.vision(
name: Name,

View File

@ -332,7 +332,7 @@ private class GdmlTransformer(val settings: GdmlTransformerSettings) {
private fun finalize(final: SolidGroup): SolidGroup {
//final.prototypes = proto
final.useStyle("Gdml") {
final.useStyle("gdml") {
Solid.ROTATION_ORDER_KEY put RotationOrder.ZXY
}

View File

@ -2,8 +2,8 @@ package hep.dataforge.vision.gdml
import hep.dataforge.names.toName
import hep.dataforge.vision.solid.SolidManager
import space.kscience.gdml.Gdml
import org.junit.jupiter.api.Test
import space.kscience.gdml.Gdml
import space.kscience.gdml.decodeFromStream
import kotlin.test.assertNotNull
@ -11,7 +11,7 @@ class TestConvertor {
@Test
fun testBMNGeometry() {
val stream = javaClass.getResourceAsStream("/gdml/BM@N.gdml")
val stream = javaClass.getResourceAsStream("/gdml/BM@N.gdml")!!
val gdml = Gdml.decodeFromStream(stream)
val vision = gdml.toVision()
//println(SolidManager.encodeToString(vision))
@ -19,16 +19,16 @@ class TestConvertor {
@Test
fun testCubes() {
val stream = javaClass.getResourceAsStream("/gdml/cubes.gdml")
val stream = javaClass.getResourceAsStream("/gdml/cubes.gdml")!!
val gdml = Gdml.decodeFromStream(stream)
val vision = gdml.toVision()
assertNotNull(vision.getPrototype("solids.box".toName()))
//println(SolidManager.encodeToString(vision))
println(SolidManager.encodeToString(vision))
}
@Test
fun testSimple() {
val stream = javaClass.getResourceAsStream("/gdml/simple1.gdml")
val stream = javaClass.getResourceAsStream("/gdml/simple1.gdml")!!
val gdml = Gdml.decodeFromStream(stream)
val vision = gdml.toVision()
//println(SolidManager.encodeToString(vision))

View File

@ -1,13 +1,23 @@
package hep.dataforge.vision.gdml
import hep.dataforge.context.Context
import hep.dataforge.names.toName
import hep.dataforge.vision.Vision
import hep.dataforge.vision.solid.SolidGroup
import hep.dataforge.vision.solid.SolidManager
import hep.dataforge.vision.solid.SolidReference
import hep.dataforge.vision.visionManager
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertDoesNotThrow
import space.kscience.gdml.*
import kotlin.test.assertNotNull
internal val testContext = Context("TEST"){
plugin(SolidManager)
}
class TestCubes {
internal val cubes = Gdml {
val center = define.position("center")
structure {
@ -68,7 +78,19 @@ class TestCubes {
fun testCubesReSerialize(){
val vision = cubes.toVision()
val serialized = SolidManager.encodeToString(vision)
val deserialized = SolidManager.decodeFromString(serialized) as SolidGroup
val deserialized = testContext.visionManager.decodeFromString(serialized) as SolidGroup
assertNotNull(deserialized.getPrototype("solids.smallBox".toName()))
//println(testContext.visionManager.encodeToString(deserialized))
fun Vision.checkPrototypes(){
if(this is SolidReference){
assertDoesNotThrow { this.prototype }
}
if(this is SolidGroup){
children.forEach {
it.value.checkPrototypes()
}
}
}
deserialized.checkPrototypes()
}
}

View File

@ -13,5 +13,7 @@ public class VisionOfPlotly(private val plotConfig: Config) : VisionBase(plotCon
public val plot: Plot get() = Plot(plotConfig)
}
public fun Plot.toVision(): VisionOfPlotly = VisionOfPlotly(config)
@DFExperimental
public inline fun VisionOutput.plotly(block: Plot.() -> Unit): VisionOfPlotly = VisionOfPlotly(Plotly.plot(block).config)

View File

@ -44,8 +44,12 @@ public class SolidReferenceGroup(
* Recursively search for defined template in the parent
*/
override val prototype: Solid
get() = (parent as? SolidGroup)?.getPrototype(refName)
?: error("Prototype with name $refName not found in $parent")
get() {
if(parent == null) error("No parent is present for SolidReferenceGroup")
if(parent !is SolidGroup) error("Reference parent is not a group")
return (parent as? SolidGroup)?.getPrototype(refName)
?: error("Prototype with name $refName not found")
}
override val children: Map<NameToken, Vision>
get() = (prototype as? VisionGroup)?.children
@ -71,7 +75,7 @@ public class SolidReferenceGroup(
private fun prototypeFor(name: Name): Solid {
return if (name.isEmpty()) prototype else {
(prototype as? SolidGroup)?.get(name) as? Solid
?: error("Prototype with name $name not found in $this")
?: error("Prototype with name $name not found in SolidReferenceGroup $refName")
}
}