0.2.0 #71

Merged
altavir merged 139 commits from dev into master 2022-01-24 09:44:18 +03:00
3 changed files with 40 additions and 29 deletions
Showing only changes of commit 59e939a175 - Show all commits

View File

@ -1,6 +1,8 @@
package space.kscience.visionforge.jupyter package space.kscience.visionforge.jupyter
import kotlinx.html.p
import kotlinx.html.stream.createHTML import kotlinx.html.stream.createHTML
import kotlinx.html.style
import org.jetbrains.kotlinx.jupyter.api.HTML import org.jetbrains.kotlinx.jupyter.api.HTML
import org.jetbrains.kotlinx.jupyter.api.declare import org.jetbrains.kotlinx.jupyter.api.declare
import org.jetbrains.kotlinx.jupyter.api.libraries.JupyterIntegration import org.jetbrains.kotlinx.jupyter.api.libraries.JupyterIntegration
@ -8,22 +10,19 @@ import space.kscience.dataforge.context.Context
import space.kscience.dataforge.context.ContextAware import space.kscience.dataforge.context.ContextAware
import space.kscience.dataforge.misc.DFExperimental import space.kscience.dataforge.misc.DFExperimental
import space.kscience.visionforge.Vision import space.kscience.visionforge.Vision
import space.kscience.visionforge.html.HtmlFormFragment import space.kscience.visionforge.html.*
import space.kscience.visionforge.html.HtmlVisionFragment
import space.kscience.visionforge.html.Page
import space.kscience.visionforge.html.fragment
@DFExperimental @DFExperimental
public abstract class JupyterPluginBase(final override val context: Context) : JupyterIntegration(), ContextAware { public abstract class JupyterPluginBase(final override val context: Context) : JupyterIntegration(), ContextAware {
protected val handler: VisionForgeServerHandler = VisionForgeServerHandler(context) protected val handler: VisionForgeForNotebook = VisionForgeForNotebook(context)
protected abstract fun Builder.afterLoaded() protected abstract fun Builder.afterLoaded()
final override fun Builder.onLoaded() { final override fun Builder.onLoaded() {
onLoaded { onLoaded {
declare("visionForge" to handler) declare("VisionForge" to handler, "vf" to handler)
} }
onShutdown { onShutdown {
@ -35,6 +34,9 @@ public abstract class JupyterPluginBase(final override val context: Context) : J
"space.kscience.visionforge.html.*" "space.kscience.visionforge.html.*"
) )
render<HtmlFragment> { fragment ->
handler.produceHtml(fragment = fragment)
}
render<HtmlVisionFragment> { fragment -> render<HtmlVisionFragment> { fragment ->
handler.produceHtml(fragment = fragment) handler.produceHtml(fragment = fragment)
@ -53,10 +55,17 @@ public abstract class JupyterPluginBase(final override val context: Context) : J
render<HtmlFormFragment> { fragment -> render<HtmlFormFragment> { fragment ->
handler.produceHtml { handler.produceHtml {
if (!handler.isServerRunning()) {
p {
style = "color: red;"
+"The server is not running. Forms are not interactive. Start server with `VisionForge.startServer()."
}
}
fragment(fragment.formBody) fragment(fragment.formBody)
vision(fragment.vision) vision(fragment.vision)
} }
} }
afterLoaded() afterLoaded()
} }
} }

View File

@ -2,6 +2,7 @@ package space.kscience.visionforge.jupyter
import io.ktor.server.engine.ApplicationEngine import io.ktor.server.engine.ApplicationEngine
import kotlinx.html.FORM import kotlinx.html.FORM
import kotlinx.html.TagConsumer
import kotlinx.html.p import kotlinx.html.p
import kotlinx.html.stream.createHTML import kotlinx.html.stream.createHTML
import kotlinx.html.style import kotlinx.html.style
@ -15,14 +16,16 @@ import space.kscience.dataforge.meta.get
import space.kscience.dataforge.meta.int import space.kscience.dataforge.meta.int
import space.kscience.dataforge.meta.string import space.kscience.dataforge.meta.string
import space.kscience.visionforge.html.HtmlFormFragment import space.kscience.visionforge.html.HtmlFormFragment
import space.kscience.visionforge.html.HtmlFragment
import space.kscience.visionforge.html.HtmlVisionFragment import space.kscience.visionforge.html.HtmlVisionFragment
import space.kscience.visionforge.html.visionFragment import space.kscience.visionforge.html.visionFragment
import space.kscience.visionforge.three.server.VisionServer import space.kscience.visionforge.three.server.VisionServer
import space.kscience.visionforge.three.server.serve import space.kscience.visionforge.three.server.serve
import space.kscience.visionforge.visionManager import space.kscience.visionforge.visionManager
public class VisionForgeServerHandler(override val context: Context) : ContextAware { /**
* A handler class that includes a server and common utilities
*/
public class VisionForgeForNotebook(override val context: Context) : ContextAware {
private var counter = 0 private var counter = 0
private var engine: ApplicationEngine? = null private var engine: ApplicationEngine? = null
@ -34,29 +37,33 @@ public class VisionForgeServerHandler(override val context: Context) : ContextAw
isolateFragments = true isolateFragments = true
} }
public fun isServerRunning(): Boolean = server != null
public fun html(block: TagConsumer<*>.() -> Unit): MimeTypedResult = HTML(createHTML().apply(block).finalize())
public fun startServer( public fun startServer(
host: String = context.properties["visionforge.host"].string ?: "localhost", host: String = context.properties["visionforge.host"].string ?: "localhost",
port: Int = context.properties["visionforge.port"].int ?: VisionServer.DEFAULT_PORT, port: Int = context.properties["visionforge.port"].int ?: VisionServer.DEFAULT_PORT,
configuration: VisionServer.() -> Unit = {}, configuration: VisionServer.() -> Unit = {},
): HtmlFragment { ): MimeTypedResult = html {
engine?.stop(1000, 2000)
engine = context.visionManager.serve(host, port) {
configuration()
server = this
}.start()
return {
if (server != null) { if (server != null) {
p { p {
style = "color: red;" style = "color: red;"
+"Stopping current VisionForge server" +"Stopping current VisionForge server"
} }
} }
engine?.stop(1000, 2000)
engine = context.visionManager.serve(host, port) {
configuration()
server = this
}.start()
p { p {
style = "color: blue;" style = "color: blue;"
+"Starting VisionForge server on http://$host:$port" +"Starting VisionForge server on http://$host:$port"
} }
} }
}
public fun stopServer() { public fun stopServer() {
engine?.apply { engine?.apply {
@ -77,5 +84,6 @@ public class VisionForgeServerHandler(override val context: Context) : ContextAw
public fun fragment(body: HtmlVisionFragment): MimeTypedResult = produceHtml(fragment = body) public fun fragment(body: HtmlVisionFragment): MimeTypedResult = produceHtml(fragment = body)
public fun page(body: HtmlVisionFragment): MimeTypedResult = produceHtml(true, body) public fun page(body: HtmlVisionFragment): MimeTypedResult = produceHtml(true, body)
public fun form(builder: FORM.() -> Unit): HtmlFormFragment = HtmlFormFragment("form[${counter++}]", builder = builder) public fun form(builder: FORM.() -> Unit): HtmlFormFragment =
HtmlFormFragment("form[${counter++}]", builder = builder)
} }

View File

@ -15,9 +15,3 @@ kotlin {
} }
} }
} }
//tasks{
// val jsBrowserWebpack by getting(KotlinWebpack::class) {
// sourceMaps = false
// }
//}