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
internal class VisionForgePlayGroundForJupyter : JupyterIntegration() {
val jsBundle = ResourceFallbacksBundle(listOf(ResourceLocation("js/visionforge-playground.js",
ResourcePathType.CLASSPATH_PATH)))
val jsResource = LibraryResource(name = "VisionForge", type = ResourceType.JS, bundles = listOf(jsBundle))
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))
private var counter = 0
@ -43,7 +44,7 @@ internal class VisionForgePlayGroundForJupyter : JupyterIntegration() {
}
script {
type = "text/javascript"
unsafe { +"window.renderVisionsAt(\"$id\");" }
unsafe { +"renderVisionsAt(\"$id\");" }
}
}
@ -54,17 +55,20 @@ internal class VisionForgePlayGroundForJupyter : JupyterIntegration() {
VisionForge.withPlotly().withSolids()
}
import("space.kscience.gdml.*",
import(
"space.kscience.gdml.*",
"kscience.plotly.*",
"kscience.plotly.models.*",
"kotlinx.html.*",
"hep.dataforge.vision.solid.*")
"hep.dataforge.vision.solid.*"
)
import("hep.dataforge.vision.VisionForge")
render<Gdml> { gdmlModel ->
val fragment = VisionManager.fragment {
vision(gdmlModel.toVision())
}
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.renderVisionsAt
import hep.dataforge.vision.plotly.usePlotly
import hep.dataforge.vision.renderVisionsInWindow
import hep.dataforge.vision.solid.three.withThreeJs
import kotlinx.browser.window
import hep.dataforge.vision.solid.three.useThreeJs
fun main() {
withPlotly()
withThreeJs()
usePlotly()
useThreeJs()
renderVisionsInWindow()
window.asDynamic()["renderVisionsInWindow"] = ::renderVisionsInWindow
window.asDynamic()["renderVisionsAt"] = ::renderVisionsAt
}

View File

@ -61,8 +61,6 @@ internal val cubes = Gdml {
@DFExperimental
fun main() {
println(cubes.encodeToString())
val content = VisionManager.fragment {
vision("canvas") {
cubes.toVision()

View File

@ -3,8 +3,10 @@ package hep.dataforge.vision.examples
import com.github.ricky12awesome.jss.encodeToSchema
import hep.dataforge.vision.solid.SolidGroup
import hep.dataforge.vision.solid.SolidManager
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
@ExperimentalSerializationApi
fun main() {
val schema = Json {
serializersModule = SolidManager.serializersModuleForSolids
@ -16,213 +18,4 @@ fun main() {
encodeDefaults = true
}.encodeToSchema(SolidGroup.serializer(), generateDefinitions = false)
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
/**
* A drop-in replacement for [Global] for VisionForge activities
*/
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
public fun renderVisionsAt(id: String) {
val element = document.getElementById(id)

View File

@ -1,6 +1,8 @@
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.names.Name
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
*/
@JsExport
public fun withPlotly() {
public fun usePlotly() {
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
*/
@JsExport
public fun withThreeJs() {
public fun useThreeJs() {
VisionForge.plugins.fetch(ThreePlugin)
}

View File

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