visionforge/jupyter/jupyter-base/src/jvmMain/kotlin/JupyterPluginBase.kt

71 lines
2.0 KiB
Kotlin
Raw Normal View History

2022-01-02 14:28:24 +03:00
package space.kscience.visionforge.jupyter
2022-01-02 22:02:55 +03:00
import kotlinx.html.p
2022-01-02 14:28:24 +03:00
import kotlinx.html.stream.createHTML
2022-01-02 22:02:55 +03:00
import kotlinx.html.style
2022-01-02 14:28:24 +03:00
import org.jetbrains.kotlinx.jupyter.api.HTML
import org.jetbrains.kotlinx.jupyter.api.declare
import org.jetbrains.kotlinx.jupyter.api.libraries.JupyterIntegration
import space.kscience.dataforge.context.Context
import space.kscience.dataforge.context.ContextAware
import space.kscience.dataforge.misc.DFExperimental
import space.kscience.visionforge.Vision
2022-01-02 22:02:55 +03:00
import space.kscience.visionforge.html.*
2022-01-02 14:28:24 +03:00
@DFExperimental
public abstract class JupyterPluginBase(final override val context: Context) : JupyterIntegration(), ContextAware {
2022-01-02 22:02:55 +03:00
protected val handler: VisionForgeForNotebook = VisionForgeForNotebook(context)
2022-01-02 14:28:24 +03:00
protected abstract fun Builder.afterLoaded()
final override fun Builder.onLoaded() {
onLoaded {
2022-01-02 22:02:55 +03:00
declare("VisionForge" to handler, "vf" to handler)
2022-01-02 14:28:24 +03:00
}
onShutdown {
handler.stopServer()
}
import(
"kotlinx.html.*",
"space.kscience.visionforge.html.*"
)
2022-01-02 22:02:55 +03:00
render<HtmlFragment> { fragment ->
handler.produceHtml(fragment = fragment)
}
2022-01-02 14:28:24 +03:00
render<HtmlVisionFragment> { fragment ->
handler.produceHtml(fragment = fragment)
}
render<Vision> { vision ->
handler.produceHtml {
vision(vision)
}
}
render<Page> { page ->
HTML(page.render(createHTML()), true)
}
render<HtmlFormFragment> { fragment ->
handler.produceHtml {
2022-01-02 22:02:55 +03:00
if (!handler.isServerRunning()) {
p {
style = "color: red;"
+"The server is not running. Forms are not interactive. Start server with `VisionForge.startServer()."
}
}
2022-01-02 14:28:24 +03:00
fragment(fragment.formBody)
vision(fragment.vision)
}
}
2022-01-02 22:02:55 +03:00
2022-01-02 14:28:24 +03:00
afterLoaded()
}
2022-01-02 22:02:55 +03:00
}