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

View File

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

View File

@ -2,6 +2,7 @@ package hep.dataforge.vision.examples
import hep.dataforge.context.Context import hep.dataforge.context.Context
import hep.dataforge.misc.DFExperimental import hep.dataforge.misc.DFExperimental
import hep.dataforge.vision.Vision
import hep.dataforge.vision.html.HtmlVisionFragment import hep.dataforge.vision.html.HtmlVisionFragment
import hep.dataforge.vision.html.ResourceLocation import hep.dataforge.vision.html.ResourceLocation
import hep.dataforge.vision.html.page import hep.dataforge.vision.html.page
@ -30,6 +31,12 @@ public fun Context.makeVisionFile(
} }
if (show) Desktop.getDesktop().browse(actualPath.toFile().toURI()) 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 hep.dataforge.vision.VisionManager
import kotlinx.html.* import kotlinx.html.*
import kotlin.collections.set 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 * 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( public fun <T> TagConsumer<T>.vision(
name: Name, name: Name,

View File

@ -332,7 +332,7 @@ private class GdmlTransformer(val settings: GdmlTransformerSettings) {
private fun finalize(final: SolidGroup): SolidGroup { private fun finalize(final: SolidGroup): SolidGroup {
//final.prototypes = proto //final.prototypes = proto
final.useStyle("Gdml") { final.useStyle("gdml") {
Solid.ROTATION_ORDER_KEY put RotationOrder.ZXY 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.names.toName
import hep.dataforge.vision.solid.SolidManager import hep.dataforge.vision.solid.SolidManager
import space.kscience.gdml.Gdml
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import space.kscience.gdml.Gdml
import space.kscience.gdml.decodeFromStream import space.kscience.gdml.decodeFromStream
import kotlin.test.assertNotNull import kotlin.test.assertNotNull
@ -11,7 +11,7 @@ class TestConvertor {
@Test @Test
fun testBMNGeometry() { fun testBMNGeometry() {
val stream = javaClass.getResourceAsStream("/gdml/BM@N.gdml") val stream = javaClass.getResourceAsStream("/gdml/BM@N.gdml")!!
val gdml = Gdml.decodeFromStream(stream) val gdml = Gdml.decodeFromStream(stream)
val vision = gdml.toVision() val vision = gdml.toVision()
//println(SolidManager.encodeToString(vision)) //println(SolidManager.encodeToString(vision))
@ -19,16 +19,16 @@ class TestConvertor {
@Test @Test
fun testCubes() { fun testCubes() {
val stream = javaClass.getResourceAsStream("/gdml/cubes.gdml") val stream = javaClass.getResourceAsStream("/gdml/cubes.gdml")!!
val gdml = Gdml.decodeFromStream(stream) val gdml = Gdml.decodeFromStream(stream)
val vision = gdml.toVision() val vision = gdml.toVision()
assertNotNull(vision.getPrototype("solids.box".toName())) assertNotNull(vision.getPrototype("solids.box".toName()))
//println(SolidManager.encodeToString(vision)) println(SolidManager.encodeToString(vision))
} }
@Test @Test
fun testSimple() { fun testSimple() {
val stream = javaClass.getResourceAsStream("/gdml/simple1.gdml") val stream = javaClass.getResourceAsStream("/gdml/simple1.gdml")!!
val gdml = Gdml.decodeFromStream(stream) val gdml = Gdml.decodeFromStream(stream)
val vision = gdml.toVision() val vision = gdml.toVision()
//println(SolidManager.encodeToString(vision)) //println(SolidManager.encodeToString(vision))

View File

@ -1,13 +1,23 @@
package hep.dataforge.vision.gdml package hep.dataforge.vision.gdml
import hep.dataforge.context.Context
import hep.dataforge.names.toName import hep.dataforge.names.toName
import hep.dataforge.vision.Vision
import hep.dataforge.vision.solid.SolidGroup import hep.dataforge.vision.solid.SolidGroup
import hep.dataforge.vision.solid.SolidManager 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.Test
import org.junit.jupiter.api.assertDoesNotThrow
import space.kscience.gdml.* import space.kscience.gdml.*
import kotlin.test.assertNotNull import kotlin.test.assertNotNull
internal val testContext = Context("TEST"){
plugin(SolidManager)
}
class TestCubes { class TestCubes {
internal val cubes = Gdml { internal val cubes = Gdml {
val center = define.position("center") val center = define.position("center")
structure { structure {
@ -68,7 +78,19 @@ class TestCubes {
fun testCubesReSerialize(){ fun testCubesReSerialize(){
val vision = cubes.toVision() val vision = cubes.toVision()
val serialized = SolidManager.encodeToString(vision) 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())) 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 val plot: Plot get() = Plot(plotConfig)
} }
public fun Plot.toVision(): VisionOfPlotly = VisionOfPlotly(config)
@DFExperimental @DFExperimental
public inline fun VisionOutput.plotly(block: Plot.() -> Unit): VisionOfPlotly = VisionOfPlotly(Plotly.plot(block).config) 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 * Recursively search for defined template in the parent
*/ */
override val prototype: Solid override val prototype: Solid
get() = (parent as? SolidGroup)?.getPrototype(refName) get() {
?: error("Prototype with name $refName not found in $parent") 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> override val children: Map<NameToken, Vision>
get() = (prototype as? VisionGroup)?.children get() = (prototype as? VisionGroup)?.children
@ -71,7 +75,7 @@ public class SolidReferenceGroup(
private fun prototypeFor(name: Name): Solid { private fun prototypeFor(name: Name): Solid {
return if (name.isEmpty()) prototype else { return if (name.isEmpty()) prototype else {
(prototype as? SolidGroup)?.get(name) as? Solid (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")
} }
} }