Complete playfround jupyter demo

This commit is contained in:
Alexander Nozik 2021-02-24 22:48:17 +03:00
parent 27fe492ab7
commit a5bfa0f147
9 changed files with 31 additions and 232 deletions

View File

@ -29,9 +29,10 @@ import space.kscience.gdml.Gdml
@DFExperimental @DFExperimental
internal class VisionForgePlayGroundForJupyter : JupyterIntegration() { internal class VisionForgePlayGroundForJupyter : JupyterIntegration() {
val jsBundle = ResourceFallbacksBundle(listOf(ResourceLocation("js/visionforge-playground.js", private val jsBundle = ResourceFallbacksBundle(listOf(
ResourcePathType.CLASSPATH_PATH))) ResourceLocation("js/visionforge-playground.js", ResourcePathType.CLASSPATH_PATH))
val jsResource = LibraryResource(name = "VisionForge", type = ResourceType.JS, bundles = listOf(jsBundle)) )
private val jsResource = LibraryResource(name = "VisionForge", type = ResourceType.JS, bundles = listOf(jsBundle))
private var counter = 0 private var counter = 0
@ -43,7 +44,7 @@ internal class VisionForgePlayGroundForJupyter : JupyterIntegration() {
} }
script { script {
type = "text/javascript" type = "text/javascript"
unsafe { +"window.renderVisionsAt(\"$id\");" } unsafe { +"renderVisionsAt(\"$id\");" }
} }
} }
@ -54,17 +55,20 @@ internal class VisionForgePlayGroundForJupyter : JupyterIntegration() {
VisionForge.withPlotly().withSolids() VisionForge.withPlotly().withSolids()
} }
import("space.kscience.gdml.*", import(
"space.kscience.gdml.*",
"kscience.plotly.*", "kscience.plotly.*",
"kscience.plotly.models.*", "kscience.plotly.models.*",
"kotlinx.html.*", "kotlinx.html.*",
"hep.dataforge.vision.solid.*") "hep.dataforge.vision.solid.*"
)
import("hep.dataforge.vision.VisionForge") import("hep.dataforge.vision.VisionForge")
render<Gdml> { gdmlModel -> render<Gdml> { gdmlModel ->
val fragment = VisionManager.fragment { val fragment = VisionManager.fragment {
vision(gdmlModel.toVision()) vision(gdmlModel.toVision())
} }
HTML(produceHtmlVisionString(fragment)) HTML(produceHtmlVisionString(fragment))
} }

View File

@ -1,14 +1,10 @@
//import hep.dataforge.vision.plotly.withPlotly //import hep.dataforge.vision.plotly.withPlotly
import hep.dataforge.vision.plotly.withPlotly import hep.dataforge.vision.plotly.usePlotly
import hep.dataforge.vision.renderVisionsAt
import hep.dataforge.vision.renderVisionsInWindow import hep.dataforge.vision.renderVisionsInWindow
import hep.dataforge.vision.solid.three.withThreeJs import hep.dataforge.vision.solid.three.useThreeJs
import kotlinx.browser.window
fun main() { fun main() {
withPlotly() usePlotly()
withThreeJs() useThreeJs()
renderVisionsInWindow() renderVisionsInWindow()
window.asDynamic()["renderVisionsInWindow"] = ::renderVisionsInWindow
window.asDynamic()["renderVisionsAt"] = ::renderVisionsAt
} }

View File

@ -61,8 +61,6 @@ 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") {
cubes.toVision() cubes.toVision()

View File

@ -3,8 +3,10 @@ package hep.dataforge.vision.examples
import com.github.ricky12awesome.jss.encodeToSchema import com.github.ricky12awesome.jss.encodeToSchema
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 kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
@ExperimentalSerializationApi
fun main() { fun main() {
val schema = Json { val schema = Json {
serializersModule = SolidManager.serializersModuleForSolids serializersModule = SolidManager.serializersModuleForSolids
@ -17,212 +19,3 @@ fun main() {
}.encodeToSchema(SolidGroup.serializer(), generateDefinitions = false) }.encodeToSchema(SolidGroup.serializer(), generateDefinitions = false)
println(schema) println(schema)
} }
//
//import hep.dataforge.meta.JSON_PRETTY
//import kotlinx.serialization.*
//import kotlinx.serialization.descriptors.PrimitiveKind
//import kotlinx.serialization.descriptors.SerialDescriptor
//import kotlinx.serialization.descriptors.StructureKind
//import kotlinx.serialization.descriptors.UnionKind
//import kotlinx.serialization.json.*
//import kotlinx.serialization.modules.SerialModule
//import kotlin.reflect.KClass
//
//private fun SerialDescriptor.getJsonType() = when (this.kind) {
// StructureKind.LIST -> "array"
// PrimitiveKind.BYTE, PrimitiveKind.SHORT, PrimitiveKind.INT, PrimitiveKind.LONG,
// PrimitiveKind.FLOAT, PrimitiveKind.DOUBLE -> "number"
// PrimitiveKind.STRING, PrimitiveKind.CHAR, UnionKind.ENUM_KIND -> "string"
// PrimitiveKind.BOOLEAN -> "boolean"
// else -> "object"
//}
//
//private fun SerialDescriptor.isVisualObject() = serialName.startsWith("solid")||serialName.startsWith("group.solid")
//
//private const val definitionNode = "\$defs"
//
//private fun SerialModule.enumerate(type: KClass<*>): Sequence<SerialDescriptor> {
// val list = ArrayList<SerialDescriptor>()
// fun send(descriptor: SerialDescriptor) = list.add(descriptor)
//
// val enumerator = object : SerialModuleCollector {
// override fun <T : Any> contextual(kClass: KClass<T>, serializer: KSerializer<T>) {
// if (kClass == type) {
// send(serializer.descriptor)
// }
// }
//
// override fun <Base : Any, Sub : Base> polymorphic(
// baseClass: KClass<Base>,
// actualClass: KClass<Sub>,
// actualSerializer: KSerializer<Sub>
// ) {
// if (baseClass == type) {
// send(actualSerializer.descriptor)
// }
// }
//
// }
// dumpTo(enumerator)
// return list.asSequence()
//}
//
///**
// * Creates an [JsonObject] which contains Json Schema of given [descriptor].
// *
// * Schema can contain following fields:
// * `description`, `type` for all descriptors;
// * `properties` and `required` for objects;
// * `enum` for enums;
// * `items` for arrays.
// *
// * User can modify this schema to add additional validation keywords
// * (as per [https://json-schema.org/latest/json-schema-validation.html])
// * if they want.
// */
//private fun jsonSchema(descriptor: SerialDescriptor, context: SerialModule): JsonObject {
//
// if (descriptor.serialName in arrayOf(
// "hep.dataforge.vision.solid.Point3D",
// "hep.dataforge.vision.solid.Point3D?",
// "hep.dataforge.vision.solid.Point2D",
// "hep.dataforge.vision.solid.Point2D?",
// "hep.dataforge.meta.Meta",
// "hep.dataforge.meta.Meta?"
// )
// ) return json {
// "\$ref" to "#/$definitionNode/${descriptor.serialName.replace("?", "")}"
// }
//
//
// val properties: MutableMap<String, JsonObject> = mutableMapOf()
// val requiredProperties: MutableSet<String> = mutableSetOf()
// val isEnum = descriptor.kind == UnionKind.ENUM_KIND
// val isPolymorphic = descriptor.kind is PolymorphicKind
//
//
// if (!isEnum && !isPolymorphic) descriptor.elementDescriptors().forEachIndexed { index, child ->
// val elementName = descriptor.getElementName(index)
//
// val elementSchema = when (elementName) {
// "properties" -> buildJsonObject {
// put("\$ref", "#/$definitionNode/hep.dataforge.meta.Meta")
// }
// "first", "second" -> buildJsonObject {
// put("\$ref", "#/$definitionNode/children")
// }
// "styleSheet" -> buildJsonObject {
// put("type", "object")
// put("additionalProperties", buildJsonObject {
// put("\$ref", "#/$definitionNode/hep.dataforge.meta.Meta")
// })
// }
// in arrayOf("children", "prototypes") -> buildJsonObject {
// put("type", "object")
// put("additionalProperties", buildJsonObject {
// put("\$ref", "#/$definitionNode/children")
// })
// }
// else -> jsonSchema(child, context)
// }
// properties[elementName] = elementSchema
//
// if (!descriptor.isElementOptional(index)) requiredProperties.add(elementName)
// }
//
// val jsonType = descriptor.getJsonType()
// val objectData: MutableMap<String, JsonElement> = mutableMapOf(
// "description" to JsonLiteral(descriptor.serialName),
// "type" to JsonLiteral(jsonType)
// )
// if (isEnum) {
// val allElementNames = (0 until descriptor.elementsCount).map(descriptor::getElementName)
// objectData += "enum" to JsonArray(allElementNames.map(::JsonLiteral))
// }
// when (jsonType) {
// "object" -> {
// if(descriptor.isVisualObject()) {
// properties["type"] = json {
// "const" to descriptor.serialName
// }
// }
// objectData["properties"] = JsonObject(properties)
// val required = requiredProperties.map { JsonLiteral(it) }
// if (required.isNotEmpty()) {
// objectData["required"] = JsonArray(required)
// }
// }
// "array" -> objectData["items"] = properties.values.let {
// check(it.size == 1) { "Array descriptor has returned inconsistent number of elements: expected 1, found ${it.size}" }
// it.first()
// }
// else -> { /* no-op */
// }
// }
// return JsonObject(objectData)
//}
//
//fun main() {
// val context = SolidManager.serialModule
// val definitions = json {
// "children" to json {
// "anyOf" to jsonArray {
// context.enumerate(Solid::class).forEach {
// if (it.serialName == "hep.dataforge.vis.spatial.SolidGroup") {
// +json {
// "\$ref" to "#/$definitionNode/${it.serialName}"
// }
// } else {
// +jsonSchema(it, context)
// }
// }
// }
// }
// "hep.dataforge.meta.Meta" to json {
// "type" to "object"
// }
// "hep.dataforge.vision.solid.Point3D" to json {
// "type" to "object"
// "properties" to json {
// "x" to json {
// "type" to "number"
// }
// "y" to json {
// "type" to "number"
// }
// "z" to json {
// "type" to "number"
// }
// }
// }
// "hep.dataforge.vision.solid.Point2D" to json {
// "type" to "object"
// "properties" to json {
// "x" to json {
// "type" to "number"
// }
// "y" to json {
// "type" to "number"
// }
// }
// }
// "hep.dataforge.vision.solid.SolidGroup" to jsonSchema(
// SolidGroup.serializer().descriptor,
// context
// )
//
// }
//
// println(
// JSON_PRETTY.stringify(
// JsonObjectSerializer,
// json {
// "\$defs" to definitions
// "\$ref" to "#/$definitionNode/hep.dataforge.vision.solid.SolidGroup"
// }
// )
// )
//}
//

View File

@ -2,4 +2,7 @@ package hep.dataforge.vision
import hep.dataforge.context.Context import hep.dataforge.context.Context
/**
* A drop-in replacement for [Global] for VisionForge activities
*/
public expect val VisionForge: Context public expect val VisionForge: Context

View File

@ -24,6 +24,9 @@ public fun renderVisionsInWindow() {
} }
} }
/**
* Render all visions in an element with a given [id]
*/
@JsExport @JsExport
public fun renderVisionsAt(id: String) { public fun renderVisionsAt(id: String) {
val element = document.getElementById(id) val element = document.getElementById(id)

View File

@ -1,6 +1,8 @@
package hep.dataforge.vision.plotly package hep.dataforge.vision.plotly
import hep.dataforge.context.* import hep.dataforge.context.Context
import hep.dataforge.context.PluginFactory
import hep.dataforge.context.PluginTag
import hep.dataforge.meta.Meta import hep.dataforge.meta.Meta
import hep.dataforge.names.Name import hep.dataforge.names.Name
import hep.dataforge.names.asName import hep.dataforge.names.asName
@ -51,6 +53,6 @@ public actual class PlotlyPlugin : VisionPlugin(), ElementVisionRenderer {
* Ensure that [PlotlyPlugin] is loaded in the global [VisionForge] context * Ensure that [PlotlyPlugin] is loaded in the global [VisionForge] context
*/ */
@JsExport @JsExport
public fun withPlotly() { public fun usePlotly() {
VisionForge.plugins.fetch(PlotlyPlugin) VisionForge.plugins.fetch(PlotlyPlugin)
} }

View File

@ -156,7 +156,7 @@ public class ThreePlugin : AbstractPlugin(), ElementVisionRenderer {
* Ensure that [ThreePlugin] is loaded in the global [VisionForge] context * Ensure that [ThreePlugin] is loaded in the global [VisionForge] context
*/ */
@JsExport @JsExport
public fun withThreeJs() { public fun useThreeJs() {
VisionForge.plugins.fetch(ThreePlugin) VisionForge.plugins.fetch(ThreePlugin)
} }

View File

@ -1,9 +1,9 @@
package hep.dataforge.vision.three.server package hep.dataforge.vision.three.server
import hep.dataforge.vision.renderVisionsInWindow import hep.dataforge.vision.renderVisionsInWindow
import hep.dataforge.vision.solid.three.withThreeJs import hep.dataforge.vision.solid.three.useThreeJs
public fun main() { public fun main() {
withThreeJs() useThreeJs()
renderVisionsInWindow() renderVisionsInWindow()
} }